[Image-SIG] dpi read/write support in TiffImagePlugin broken

Markus Kemmerling kemmerling at gmx.at
Thu Jul 20 18:30:21 CEST 2006


It seems to me that the dpi read/write support in TiffImagePlugin.py  
is broken. When reading an image it will set the dpi in the info  
dictionary only if RESOLUTION_UNIT == 1. Similarly it will set  
RESOLUTION_UNIT to 1when saving an image as a TIFF with dpi values  
given.

But according to the TIFF specification (http://partners.adobe.com/ 
public/developer/en/tiff/TIFF6.pdf) this value should be 2 rather  
than 1:

ResolutionUnit
	Tag = 296 (128.H)
	Type = SHORT
	Values:
1 = No absolute unit of measurement. Used for images that may have a  
non-square aspect ratio but no meaningful absolute dimensions.
2 = Inch.
3 = Centimeter.
	Default = 2 (inch).

After applying the following patch (in analogy to the dpi-related  
code in PngImagePlugin.py) to TiffImagePlugin.py (current PIL version  
1.1.6b1) the results are consistent with the specification. I tested  
this by comparing the dpi and size values in Photoshop and  
GraphicConverter.

--- TiffImagePlugin.py.orig	2006-02-13 00:43:27.000000000 +0100
+++ TiffImagePlugin.py	2006-07-20 17:47:28.000000000 +0200
@@ -574,10 +574,16 @@
          xdpi = getscalar(X_RESOLUTION, (1, 1))
          ydpi = getscalar(Y_RESOLUTION, (1, 1))
-        if xdpi and ydpi and getscalar(RESOLUTION_UNIT, 1) == 1:
+        if xdpi and ydpi:
              xdpi = xdpi[0] / (xdpi[1] or 1)
              ydpi = ydpi[0] / (ydpi[1] or 1)
-            self.info["dpi"] = xdpi, ydpi
+            unit = getscalar(RESOLUTION_UNIT, 1)
+            if unit == 1:
+                self.info["aspect"] = xdpi, ydpi
+            elif unit == 2:
+                self.info["dpi"] = xdpi, ydpi
+            elif unit == 3:
+                self.info["dpi"] = (xdpi*.39370079, ydpi*.39370079)
          # build tile descriptors
          x = y = l = 0
@@ -715,7 +721,7 @@
      dpi = im.encoderinfo.get("dpi")
      if dpi:
-        ifd[RESOLUTION_UNIT] = 1
+        ifd[RESOLUTION_UNIT] = 2
          ifd[X_RESOLUTION] = _cvt_res(dpi[0])
          ifd[Y_RESOLUTION] = _cvt_res(dpi[1])

Regards,

Markus Kemmerling
Medical University Vienna



More information about the Image-SIG mailing list