Merge pull request #1058 from hugovk/anntzer-tiff-resolution2

Tests for PR #1044: Use underscores, not spaces, in TIFF tag kwargs
This commit is contained in:
wiredfool 2014-12-29 15:23:20 -08:00
commit 6ffe9e1689
3 changed files with 57 additions and 31 deletions

View File

@ -1068,31 +1068,25 @@ def _save(im, fp, filename):
if "icc_profile" in im.info:
ifd[ICCPROFILE] = im.info["icc_profile"]
if "description" in im.encoderinfo:
ifd[IMAGEDESCRIPTION] = im.encoderinfo["description"]
if "resolution" in im.encoderinfo:
ifd[X_RESOLUTION] = ifd[Y_RESOLUTION] \
= _cvt_res(im.encoderinfo["resolution"])
if "x resolution" in im.encoderinfo:
ifd[X_RESOLUTION] = _cvt_res(im.encoderinfo["x resolution"])
if "y resolution" in im.encoderinfo:
ifd[Y_RESOLUTION] = _cvt_res(im.encoderinfo["y resolution"])
if "resolution unit" in im.encoderinfo:
unit = im.encoderinfo["resolution unit"]
if unit == "inch":
ifd[RESOLUTION_UNIT] = 2
elif unit == "cm" or unit == "centimeter":
ifd[RESOLUTION_UNIT] = 3
else:
ifd[RESOLUTION_UNIT] = 1
if "software" in im.encoderinfo:
ifd[SOFTWARE] = im.encoderinfo["software"]
if "date time" in im.encoderinfo:
ifd[DATE_TIME] = im.encoderinfo["date time"]
if "artist" in im.encoderinfo:
ifd[ARTIST] = im.encoderinfo["artist"]
if "copyright" in im.encoderinfo:
ifd[COPYRIGHT] = im.encoderinfo["copyright"]
for key, name, cvt in [
(IMAGEDESCRIPTION, "description", lambda x: x),
(X_RESOLUTION, "resolution", _cvt_res),
(Y_RESOLUTION, "resolution", _cvt_res),
(X_RESOLUTION, "x_resolution", _cvt_res),
(Y_RESOLUTION, "y_resolution", _cvt_res),
(RESOLUTION_UNIT, "resolution_unit",
lambda x: {"inch": 2, "cm": 3, "centimeter": 3}.get(x, 1)),
(SOFTWARE, "software", lambda x: x),
(DATE_TIME, "date_time", lambda x: x),
(ARTIST, "artist", lambda x: x),
(COPYRIGHT, "copyright", lambda x: x)]:
name_with_spaces = name.replace("_", " ")
if "_" in name and name_with_spaces in im.encoderinfo:
warnings.warn("%r is deprecated; use %r instead" %
(name_with_spaces, name), DeprecationWarning)
ifd[key] = cvt(im.encoderinfo[name.replace("_", " ")])
if name in im.encoderinfo:
ifd[key] = cvt(im.encoderinfo[name])
dpi = im.encoderinfo.get("dpi")
if dpi:

View File

@ -306,7 +306,6 @@ class TestFileTiff(PillowTestCase):
self.assertEqual(im.mode, "L")
self.assert_image_similar(im, original, 7.3)
def test_page_number_x_0(self):
# Issue 973
# Test TIFF with tag 297 (Page Number) having value of 0 0.
@ -326,7 +325,40 @@ class TestFileTiff(PillowTestCase):
# Should not divide by zero
im.save(outfile)
def test_with_underscores(self):
# Arrange: use underscores
kwargs = {'resolution_unit': 'inch',
'x_resolution': 72,
'y_resolution': 36}
filename = self.tempfile("temp.tif")
# Act
hopper("RGB").save(filename, **kwargs)
# Assert
from PIL.TiffImagePlugin import X_RESOLUTION, Y_RESOLUTION
im = Image.open(filename)
self.assertEqual(im.tag.tags[X_RESOLUTION][0][0], 72)
self.assertEqual(im.tag.tags[Y_RESOLUTION][0][0], 36)
def test_deprecation_warning_with_spaces(self):
# Arrange: use spaces
kwargs = {'resolution unit': 'inch',
'x resolution': 36,
'y resolution': 72}
filename = self.tempfile("temp.tif")
# Act
self.assert_warning(DeprecationWarning,
lambda: hopper("RGB").save(filename, **kwargs))
# Assert
from PIL.TiffImagePlugin import X_RESOLUTION, Y_RESOLUTION
im = Image.open(filename)
self.assertEqual(im.tag.tags[X_RESOLUTION][0][0], 36)
self.assertEqual(im.tag.tags[Y_RESOLUTION][0][0], 72)
if __name__ == '__main__':
unittest.main()

View File

@ -470,21 +470,21 @@ These arguments to set the tiff header fields are an alternative to using the ge
**software**
**date time**
**date_time**
**artist**
**copyright**
Strings
**resolution unit**
**resolution_unit**
A string of "inch", "centimeter" or "cm"
**resolution**
**x resolution**
**x_resolution**
**y resolution**
**y_resolution**
**dpi**
Either a Float, Integer, or 2 tuple of (numerator,