[Image-SIG] BUG: PIL DPI trouble - fix for TIFF files (PIL 1.1.6)

Florian Höch lists+Image_SIG at hoech.org
Fri Oct 5 00:05:16 CEST 2007


PIL only adds a dpi entry in the info dictionary if no resolution unit 
is specified in the TIFF file. This seems to be because of a 
misinterpretation of resolution unit values: they have a different 
meaning than in a JPEG file. JPEG: 0 = None; 1 = inches; 2 = cm. TIFF: 1 
= None; 2 = inches; 3 = cm (see 
http://www.awaresystems.be/imaging/tiff/tifftags/resolutionunit.html and 
http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/JPEG.html)

Here's a quick fix:

Open TiffImagePlugin.py and go to line 577. Change the code:

xres = getscalar(X_RESOLUTION, (1, 1))
yres = getscalar(Y_RESOLUTION, (1, 1))

if xres and yres:
     xres = xres[0] / (xres[1] or 1)
     yres = yres[0] / (yres[1] or 1)
     resunit = getscalar(RESOLUTION_UNIT, 1)
     if resunit == 2: # Inches
         self.info["dpi"] = xres, yres
     elif resunit == 3: # Centimeters
         self.info["dpi"] = xres * 2.54, yres * 2.54
     else: # No absolute unit of measurement.
         self.info["resolution"] = xres, yres


Regards,

Florian Hoech


More information about the Image-SIG mailing list