title: With Python`s PIL, how to set DPI before loading an image?
body: <p>I was trying to use PIL to open an (Illustrator) .eps-file, do some changes and save it. I want to set the document to 300 dpi, and color mode to cmyk <strong>before</strong> opening, creating or interpreting the object.</p>
  
  <p>First I tried the same with PythonMagick and it worked like this:</p>
  
  <pre><code>import PythonMagick
  # That's NOT what I want
  img72 = PythonMagick.Image()
  img_file = 'epstest.eps'
  img.read(img_file)
  img_dens = img72.density()
  print 'W: %d, H: %d' % (img.size().width(), img.size().height())
  # W: 403, H: 2475 &lt;-- See here
  print 'Density Width: %r' % img_dens.width() # 72
  print 'Density Height: %r' % img_dens.height() # 72
  
  # THAT is what I want
  img300 = PythonMagick.Image()
  img_file = 'epstest.eps'
  img300.density('300')      # set density / resolution
  img300.read(img_file)      # opens with defined density
  img_dens = img300.density()
  print 'W: %d, H: %d' % (img.size().width(), img.size().height())
  # W: 1679, H: 10312 &lt;-- See here!
  print 'Density Width: %r' % img_dens.width() # 300
  print 'Density Height: %r' % img_dens.height() # 300
  </code></pre>
  
  <p>Problem with PythonMagick: Converting Color Modes is not working, so i tried the same with PIL, which I would prefer:</p>
  
  <pre><code>from PIL import Image
  
  img = Image.open('epstest.eps')
  </code></pre>
  
  <p>I know that it is possible to set dpi while saving.</p>
  
  <p>Stuff that <strong>didn't</strong> work:</p>
  
  <pre><code>img = Image() # TypeError: 'module' object is not callable
  img = Image.new() # TypeError: new() takes at least 2 arguments (0 given)
  # .new() would create a different object anyway..
  img = Image.open('epstest.eps', dpi = 300)
  img = Image.open('epstest.eps', dpi = (300, 300) )
  # After opening an Image
  img.load(dpi=(300,300))
  </code></pre>
  
  <p>Regarding the <strong>input</strong>: My .eps file - if it is interpreted with 72dpi (seems to be PIL default) it ends up with 403x2475 px, with 300dpi it should be 1677x10311 px. Also, the .eps-file does not contain a preview-bitmap, nor any bitmap-data. Only 2 colors (black and white), plain vectors. It would be useful to make a catalogue of a huge amount of color-separated .eps-files.</p>
  
  <p>Regarding the <strong>output</strong>: will be a png.</p>
  
  <h1>Solution:</h1>
  
  <p>Many thanks to Paulo - this is his solution with very small changes:</p>
  
  <pre><code>from PIL import Image
  from PIL import EpsImagePlugin
  import math
  filename = 'epstest.eps'
  def open_eps(filename, dpi=300.0):
      img = Image.open(filename)
      original = [float(d) for d in img.size]
      # scale = width / original[0] # calculated wrong height
      scale = dpi/72.0            # this fixed it
      if dpi is not 0:
          img.load(scale = math.ceil(scale))
      if scale != 1:
          img.thumbnail([round(scale * d) for d in original], Image.ANTIALIAS)
      return img
  
  img = open_eps(filename, dpi=300.0)
  img.save('pil_test.png', dpi=(300.0, 300.0))
  </code></pre>