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

Gary Bloom bloominator at hotmail.com
Wed Oct 10 16:18:11 CEST 2007


Here's a quick fix for the writing of the file, as well...

Before making the fix below, go to line 721 and change the '1' to a '2' !

    721:        ifd[RESOLUTION_UNIT] = 1

        becomes

    721:        ifd[RESOLUTION_UNIT] = 2

Then make the changes listed below.    This is consistent with those changes.  This is a quick and dirty fix that I have not tested extensively, but it quickly allows me to read and write TIFF images with both the PIL and Photoshop, and each can see the DPI as set by the other.  It fixes the problem I've had with PIL reading/writing TIFFs.  Thanks, Florian!

Regards,

Gary Bloom
Sales Engineer
Sefas Innovation
www.sefas.com
============
[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 

  a.. Previous message: [Image-SIG] fromstring image question 
  b.. Next message: [Image-SIG] Image transpose 
  c.. Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] 

--------------------------------------------------------------------------------

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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/image-sig/attachments/20071010/5903312a/attachment.htm 


More information about the Image-SIG mailing list