mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-24 17:06:16 +03:00
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:
commit
6ffe9e1689
|
@ -1068,31 +1068,25 @@ def _save(im, fp, filename):
|
||||||
if "icc_profile" in im.info:
|
if "icc_profile" in im.info:
|
||||||
ifd[ICCPROFILE] = im.info["icc_profile"]
|
ifd[ICCPROFILE] = im.info["icc_profile"]
|
||||||
|
|
||||||
if "description" in im.encoderinfo:
|
for key, name, cvt in [
|
||||||
ifd[IMAGEDESCRIPTION] = im.encoderinfo["description"]
|
(IMAGEDESCRIPTION, "description", lambda x: x),
|
||||||
if "resolution" in im.encoderinfo:
|
(X_RESOLUTION, "resolution", _cvt_res),
|
||||||
ifd[X_RESOLUTION] = ifd[Y_RESOLUTION] \
|
(Y_RESOLUTION, "resolution", _cvt_res),
|
||||||
= _cvt_res(im.encoderinfo["resolution"])
|
(X_RESOLUTION, "x_resolution", _cvt_res),
|
||||||
if "x resolution" in im.encoderinfo:
|
(Y_RESOLUTION, "y_resolution", _cvt_res),
|
||||||
ifd[X_RESOLUTION] = _cvt_res(im.encoderinfo["x resolution"])
|
(RESOLUTION_UNIT, "resolution_unit",
|
||||||
if "y resolution" in im.encoderinfo:
|
lambda x: {"inch": 2, "cm": 3, "centimeter": 3}.get(x, 1)),
|
||||||
ifd[Y_RESOLUTION] = _cvt_res(im.encoderinfo["y resolution"])
|
(SOFTWARE, "software", lambda x: x),
|
||||||
if "resolution unit" in im.encoderinfo:
|
(DATE_TIME, "date_time", lambda x: x),
|
||||||
unit = im.encoderinfo["resolution unit"]
|
(ARTIST, "artist", lambda x: x),
|
||||||
if unit == "inch":
|
(COPYRIGHT, "copyright", lambda x: x)]:
|
||||||
ifd[RESOLUTION_UNIT] = 2
|
name_with_spaces = name.replace("_", " ")
|
||||||
elif unit == "cm" or unit == "centimeter":
|
if "_" in name and name_with_spaces in im.encoderinfo:
|
||||||
ifd[RESOLUTION_UNIT] = 3
|
warnings.warn("%r is deprecated; use %r instead" %
|
||||||
else:
|
(name_with_spaces, name), DeprecationWarning)
|
||||||
ifd[RESOLUTION_UNIT] = 1
|
ifd[key] = cvt(im.encoderinfo[name.replace("_", " ")])
|
||||||
if "software" in im.encoderinfo:
|
if name in im.encoderinfo:
|
||||||
ifd[SOFTWARE] = im.encoderinfo["software"]
|
ifd[key] = cvt(im.encoderinfo[name])
|
||||||
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"]
|
|
||||||
|
|
||||||
dpi = im.encoderinfo.get("dpi")
|
dpi = im.encoderinfo.get("dpi")
|
||||||
if dpi:
|
if dpi:
|
||||||
|
|
|
@ -306,7 +306,6 @@ class TestFileTiff(PillowTestCase):
|
||||||
self.assertEqual(im.mode, "L")
|
self.assertEqual(im.mode, "L")
|
||||||
self.assert_image_similar(im, original, 7.3)
|
self.assert_image_similar(im, original, 7.3)
|
||||||
|
|
||||||
|
|
||||||
def test_page_number_x_0(self):
|
def test_page_number_x_0(self):
|
||||||
# Issue 973
|
# Issue 973
|
||||||
# Test TIFF with tag 297 (Page Number) having value of 0 0.
|
# Test TIFF with tag 297 (Page Number) having value of 0 0.
|
||||||
|
@ -326,7 +325,40 @@ class TestFileTiff(PillowTestCase):
|
||||||
# Should not divide by zero
|
# Should not divide by zero
|
||||||
im.save(outfile)
|
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__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
||||||
|
|
|
@ -470,21 +470,21 @@ These arguments to set the tiff header fields are an alternative to using the ge
|
||||||
|
|
||||||
**software**
|
**software**
|
||||||
|
|
||||||
**date time**
|
**date_time**
|
||||||
|
|
||||||
**artist**
|
**artist**
|
||||||
|
|
||||||
**copyright**
|
**copyright**
|
||||||
Strings
|
Strings
|
||||||
|
|
||||||
**resolution unit**
|
**resolution_unit**
|
||||||
A string of "inch", "centimeter" or "cm"
|
A string of "inch", "centimeter" or "cm"
|
||||||
|
|
||||||
**resolution**
|
**resolution**
|
||||||
|
|
||||||
**x resolution**
|
**x_resolution**
|
||||||
|
|
||||||
**y resolution**
|
**y_resolution**
|
||||||
|
|
||||||
**dpi**
|
**dpi**
|
||||||
Either a Float, Integer, or 2 tuple of (numerator,
|
Either a Float, Integer, or 2 tuple of (numerator,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user