diff --git a/PIL/TiffImagePlugin.py b/PIL/TiffImagePlugin.py index 524d42a345c..c6729c376d4 100644 --- a/PIL/TiffImagePlugin.py +++ b/PIL/TiffImagePlugin.py @@ -1154,9 +1154,9 @@ def _setup(self): if xres and yres: resunit = self.tag_v2.get(RESOLUTION_UNIT, 1) if resunit == 2: # dots per inch - self.info["dpi"] = xres, yres + self.info["dpi"] = int(round(xres)), int(round(yres)) elif resunit == 3: # dots per centimeter. convert to dpi - self.info["dpi"] = xres * 2.54, yres * 2.54 + self.info["dpi"] = int(round(xres * 2.54)), int(round(yres * 2.54)) else: # No absolute unit of measurement self.info["resolution"] = xres, yres diff --git a/Tests/test_file_tiff.py b/Tests/test_file_tiff.py index 5b01de12b82..14c5d398cd2 100644 --- a/Tests/test_file_tiff.py +++ b/Tests/test_file_tiff.py @@ -470,6 +470,20 @@ def test_open_tiff_uint16(self): im = Image.open(infile) self.assertEqual(im.getpixel((0, 0)), pixel_value) + def test_save_tiff_with_dpi(self): + + outfile = self.tempfile("temp.tif") + + infile = "Tests/images/hopper.tif" + im = Image.open(infile) + + im.save(outfile, 'JPEG', dpi=im.info['dpi']) + + reloaded = Image.open(outfile) + reloaded.load() + + self.assertEqual(im.info['dpi'], reloaded.info['dpi']) + if __name__ == '__main__': unittest.main()