From 5ca04bb728b4011119a4191979609399e6dcefbf Mon Sep 17 00:00:00 2001 From: Matti Picus Date: Tue, 7 May 2013 23:23:51 +0300 Subject: [PATCH 1/3] a test that fails for images with integer resolution --- PIL/TiffImagePlugin.py | 20 ++++++++++---------- Tests/test_file_tiff.py | 11 ++++++++++- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/PIL/TiffImagePlugin.py b/PIL/TiffImagePlugin.py index 7fd27f5c3..b137968a8 100644 --- a/PIL/TiffImagePlugin.py +++ b/PIL/TiffImagePlugin.py @@ -615,7 +615,7 @@ class TiffImageFile(ImageFile.ImageFile): return pixel self.load_prepare() - + if not len(self.tile) == 1: raise IOError("Not exactly one tile") @@ -635,7 +635,7 @@ class TiffImageFile(ImageFile.ImageFile): # # Rearranging for supporting byteio items, since they have a fileno # that returns an IOError if there's no underlying fp. Easier to deal - # with here by reordering. + # with here by reordering. if Image.DEBUG: print ("have getvalue. just sending in a string from getvalue") n,e = d.decode(self.fp.getvalue()) @@ -649,10 +649,10 @@ class TiffImageFile(ImageFile.ImageFile): # we have something else. if Image.DEBUG: print ("don't have fileno or getvalue. just reading") - # UNDONE -- so much for that buffer size thing. + # UNDONE -- so much for that buffer size thing. n, e = d.decode(self.fp.read()) - + self.tile = [] self.readonly = 0 self.fp = None # might be shared @@ -750,19 +750,19 @@ class TiffImageFile(ImageFile.ImageFile): # Decoder expects entire file as one tile. # There's a buffer size limit in load (64k) # so large g4 images will fail if we use that - # function. + # function. # # Setup the one tile for the whole image, then # replace the existing load function with our # _load_libtiff function. - + self.load = self._load_libtiff - + # To be nice on memory footprint, if there's a # file descriptor, use that instead of reading # into a string in python. - # libtiff closes the file descriptor, so pass in a dup. + # libtiff closes the file descriptor, so pass in a dup. try: fp = hasattr(self.fp, "fileno") and os.dup(self.fp.fileno()) except IOError: @@ -990,7 +990,7 @@ def _save(im, fp, filename): if type(v) == str: atts[k] = v continue - + except: # if we don't have an ifd here, just punt. pass @@ -1007,7 +1007,7 @@ def _save(im, fp, filename): break if s < 0: raise IOError("encoder error %d when writing image file" % s) - + else: offset = ifd.save(fp) diff --git a/Tests/test_file_tiff.py b/Tests/test_file_tiff.py index 4347bda8b..8b53140bc 100644 --- a/Tests/test_file_tiff.py +++ b/Tests/test_file_tiff.py @@ -60,4 +60,13 @@ def test_gimp_tiff(): ]) assert_no_exception(lambda: im.load()) - +def test_xyres_tiff(): + from PIL.TiffImagePlugin import X_RESOLUTION, Y_RESOLUTION + file = "Tests/images/pil168.tif" + im = Image.open(file) + assert isinstance(im.tag.tags[X_RESOLUTION][0], tuple) + assert isinstance(im.tag.tags[Y_RESOLUTION][0], tuple) + #Try to read a file where X,Y_RESOLUTION are ints + im.tag.tags[X_RESOLUTION] = (72,) + im.tag.tags[Y_RESOLUTION] = (72,) + im._setup() From af94b45cbcae3d808d2b27dda06bfa68678b6c8d Mon Sep 17 00:00:00 2001 From: Matti Picus Date: Tue, 7 May 2013 23:33:21 +0300 Subject: [PATCH 2/3] allow integer image resolution as well as rational --- PIL/TiffImagePlugin.py | 4 ++++ Tests/test_file_tiff.py | 1 + 2 files changed, 5 insertions(+) diff --git a/PIL/TiffImagePlugin.py b/PIL/TiffImagePlugin.py index b137968a8..2c6e3dd9b 100644 --- a/PIL/TiffImagePlugin.py +++ b/PIL/TiffImagePlugin.py @@ -723,6 +723,10 @@ class TiffImageFile(ImageFile.ImageFile): xres = getscalar(X_RESOLUTION, (1, 1)) yres = getscalar(Y_RESOLUTION, (1, 1)) + if xres and not isinstance(xres, tuple): + xres = (xres, 1.) + if yres and not isinstance(yres, tuple): + yres = (yres, 1.) if xres and yres: xres = xres[0] / (xres[1] or 1) yres = yres[0] / (yres[1] or 1) diff --git a/Tests/test_file_tiff.py b/Tests/test_file_tiff.py index 8b53140bc..071b999dc 100644 --- a/Tests/test_file_tiff.py +++ b/Tests/test_file_tiff.py @@ -70,3 +70,4 @@ def test_xyres_tiff(): im.tag.tags[X_RESOLUTION] = (72,) im.tag.tags[Y_RESOLUTION] = (72,) im._setup() + im.info['dpi'] == (72., 72.) From 6a942c306568425f7ee9b274519ad2c8a28bf149 Mon Sep 17 00:00:00 2001 From: Matti Picus Date: Tue, 7 May 2013 23:49:45 +0300 Subject: [PATCH 3/3] add assert to test --- Tests/test_file_tiff.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/test_file_tiff.py b/Tests/test_file_tiff.py index 071b999dc..ddca24876 100644 --- a/Tests/test_file_tiff.py +++ b/Tests/test_file_tiff.py @@ -70,4 +70,4 @@ def test_xyres_tiff(): im.tag.tags[X_RESOLUTION] = (72,) im.tag.tags[Y_RESOLUTION] = (72,) im._setup() - im.info['dpi'] == (72., 72.) + assert_equal(im.info['dpi'], (72., 72.))