With Python PIL, how to set DPI before loading an image?

I tried to use PIL to open a .eps file (Illustrator), make some changes and save it. I want to set the document to 300 dpi and color mode to cmyk to , opening, creating or interpreting an object.

At first I tried the same with PythonMagick, and it worked as follows:

import PythonMagick
# That 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 <-- 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 <-- See here!
print 'Density Width: %r' % img_dens.width() # 300
print 'Density Height: %r' % img_dens.height() # 300

Problem with PythonMagick: color mode conversion does not work, so I tried the same with PIL, which I would prefer:

from PIL import Image

img = Image.open('epstest.eps')

I know that when saving, you can configure dpi.

Material in which it does not work :

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))

: My.eps - 72dpi (-, PIL ), 403x2475 px, 300 dpi 1677x10311 px. , .eps , . 2 (-), . .eps .

: png.

:

- :

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))
+4
1

AFAIK, , EPS - . DPI, .

- eps. (?) .eps ( ). PythonMagick , PIL, . - OP

, EPS PythonMagick EPS (, IM, , " " ) - PIL EPS EPS.

. " " ImageMagick:

? IM " ", , , . , , IM , , ( ), , . , IM . , . ( ) , . , IM . IM , PDF Postscript, "" , "" , . , ... ImageMagick " " " " EG: , : PDF, PS, SVG , . ImageMagick .

. EPS PIL:

PIL EPS, , , ( ImageData). Ghostscript, EPS . EPS EPS.

[ 1]

PIL " " :

Ghostscript, load() , , Ghostscript EPS

. EPS , 100px x 100px, 2 , Ghostscript 200px x 200px. :

im = Image.open(...)
im.size #(100,100)
im.load(scale=2)
im.size #(200,200)

[ 2]

, PIL . EPS, . OP, , -, 72 ppi .

, - 72 ( ), , , - r , s - : 1 : s = 72 : r ergo:

im.load(scale=300.0/72.0)

, - , 1677 :

def open_eps(filename, width=None):
    original_width = float(Image.open(filename).size[0])
    im = Image.open(filename)
    if width is not None:
        im.load(scale=width/original_width)
    return im

im = open_eps('testfile.eps', 1677)

, : ppi EPS , scale . , , PR .

[ 3]

, , , ... 4,166666667 (300,0/72,0) 4.

.

def open_eps(filename, width=None):
    original = [float(d) for d in Image.open(filename).size]
    scale = width / original[0]
    im = Image.open(filename)
    if width is not None:
        im.load(scale=math.ceil(scale))
    if scale != 1:
        im.thumbnail([int(scale * d) for d in original], Image.ANTIALIAS)
    return im

im = open_eps('testfile.eps', 1677)

, math.round int, .

+4

All Articles