2015-09-15 04:06:51 +03:00
|
|
|
import io
|
|
|
|
import struct
|
|
|
|
|
2020-02-03 12:11:32 +03:00
|
|
|
import pytest
|
2013-10-03 09:06:17 +04:00
|
|
|
from PIL import Image, TiffImagePlugin, TiffTags
|
2019-07-20 00:12:16 +03:00
|
|
|
from PIL.TiffImagePlugin import IFDRational
|
2019-07-06 23:40:53 +03:00
|
|
|
|
2020-01-30 17:56:07 +03:00
|
|
|
from .helper import PillowTestCase, assert_deep_equal, hopper
|
2013-10-03 09:06:17 +04:00
|
|
|
|
2016-11-07 15:33:46 +03:00
|
|
|
tag_ids = {info.name: info.value for info in TiffTags.TAGS_V2.values()}
|
2013-10-03 09:06:17 +04:00
|
|
|
|
2014-06-03 14:02:44 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
class TestFileTiffMetadata(PillowTestCase):
|
|
|
|
def test_rt_metadata(self):
|
2014-08-26 16:51:37 +04:00
|
|
|
""" Test writing arbitrary metadata into the tiff image directory
|
2014-06-10 13:10:47 +04:00
|
|
|
Use case is ImageJ private tags, one numeric, one arbitrary
|
|
|
|
data. https://github.com/python-pillow/Pillow/issues/291
|
|
|
|
"""
|
|
|
|
|
2014-09-05 13:14:45 +04:00
|
|
|
img = hopper()
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2015-09-13 16:08:49 +03:00
|
|
|
# Behaviour change: re #1416
|
2015-09-12 12:11:10 +03:00
|
|
|
# Pre ifd rewrite, ImageJMetaData was being written as a string(2),
|
|
|
|
# Post ifd rewrite, it's defined as arbitrary bytes(7). It should
|
|
|
|
# roundtrip with the actual bytes, rather than stripped text
|
|
|
|
# of the premerge tests.
|
|
|
|
#
|
|
|
|
# For text items, we still have to decode('ascii','replace') because
|
|
|
|
# the tiff file format can't take 8 bit bytes in that field.
|
2015-12-10 01:35:35 +03:00
|
|
|
|
2015-06-27 19:35:36 +03:00
|
|
|
basetextdata = "This is some arbitrary metadata for a text field"
|
2019-06-13 18:54:11 +03:00
|
|
|
bindata = basetextdata.encode("ascii") + b" \xff"
|
2015-09-12 12:11:10 +03:00
|
|
|
textdata = basetextdata + " " + chr(255)
|
|
|
|
reloaded_textdata = basetextdata + " ?"
|
2015-02-23 12:51:42 +03:00
|
|
|
floatdata = 12.345
|
|
|
|
doubledata = 67.89
|
2014-06-10 13:10:47 +04:00
|
|
|
info = TiffImagePlugin.ImageFileDirectory()
|
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
ImageJMetaData = tag_ids["ImageJMetaData"]
|
|
|
|
ImageJMetaDataByteCounts = tag_ids["ImageJMetaDataByteCounts"]
|
|
|
|
ImageDescription = tag_ids["ImageDescription"]
|
2015-12-10 01:35:35 +03:00
|
|
|
|
2015-09-12 12:11:10 +03:00
|
|
|
info[ImageJMetaDataByteCounts] = len(bindata)
|
|
|
|
info[ImageJMetaData] = bindata
|
2019-06-13 18:54:11 +03:00
|
|
|
info[tag_ids["RollAngle"]] = floatdata
|
|
|
|
info.tagtype[tag_ids["RollAngle"]] = 11
|
|
|
|
info[tag_ids["YawAngle"]] = doubledata
|
|
|
|
info.tagtype[tag_ids["YawAngle"]] = 12
|
2013-10-03 09:06:17 +04:00
|
|
|
|
2015-09-12 12:11:10 +03:00
|
|
|
info[ImageDescription] = textdata
|
2015-12-10 01:35:35 +03:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
f = self.tempfile("temp.tif")
|
2013-10-03 09:06:17 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
img.save(f, tiffinfo=info)
|
2013-10-03 09:06:17 +04:00
|
|
|
|
Improve handling of file resources
Follow Python's file object semantics. User code is responsible for
closing resources (usually through a context manager) in a deterministic
way.
To achieve this, remove __del__ functions. These functions used to
closed open file handlers in an attempt to silence Python
ResourceWarnings. However, using __del__ has the following drawbacks:
- __del__ isn't called until the object's reference count reaches 0.
Therefore, resource handlers remain open or in use longer than
necessary.
- The __del__ method isn't guaranteed to execute on system exit. See the
Python documentation:
https://docs.python.org/3/reference/datamodel.html#object.__del__
> It is not guaranteed that __del__() methods are called for objects
> that still exist when the interpreter exits.
- Exceptions that occur inside __del__ are ignored instead of raised.
This has the potential of hiding bugs. This is also in the Python
documentation:
> Warning: Due to the precarious circumstances under which __del__()
> methods are invoked, exceptions that occur during their execution
> are ignored, and a warning is printed to sys.stderr instead.
Instead, always close resource handlers when they are no longer in use.
This will close the file handler at a specified point in the user's code
and not wait until the interpreter chooses to. It is always guaranteed
to run. And, if an exception occurs while closing the file handler, the
bug will not be ignored.
Now, when code receives a ResourceWarning, it will highlight an area
that is mishandling resources. It should not simply be silenced, but
fixed by closing resources with a context manager.
All warnings that were emitted during tests have been cleaned up. To
enable warnings, I passed the `-Wa` CLI option to Python. This exposed
some mishandling of resources in ImageFile.__init__() and
SpiderImagePlugin.loadImageSeries(), they too were fixed.
2019-05-25 19:30:58 +03:00
|
|
|
with Image.open(f) as loaded:
|
2015-09-12 00:24:35 +03:00
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
assert loaded.tag[ImageJMetaDataByteCounts] == (len(bindata),)
|
|
|
|
assert loaded.tag_v2[ImageJMetaDataByteCounts] == (len(bindata),)
|
2015-09-12 00:24:35 +03:00
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
assert loaded.tag[ImageJMetaData] == bindata
|
|
|
|
assert loaded.tag_v2[ImageJMetaData] == bindata
|
2015-09-12 00:24:35 +03:00
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
assert loaded.tag[ImageDescription] == (reloaded_textdata,)
|
|
|
|
assert loaded.tag_v2[ImageDescription] == reloaded_textdata
|
2015-09-12 00:24:35 +03:00
|
|
|
|
Improve handling of file resources
Follow Python's file object semantics. User code is responsible for
closing resources (usually through a context manager) in a deterministic
way.
To achieve this, remove __del__ functions. These functions used to
closed open file handlers in an attempt to silence Python
ResourceWarnings. However, using __del__ has the following drawbacks:
- __del__ isn't called until the object's reference count reaches 0.
Therefore, resource handlers remain open or in use longer than
necessary.
- The __del__ method isn't guaranteed to execute on system exit. See the
Python documentation:
https://docs.python.org/3/reference/datamodel.html#object.__del__
> It is not guaranteed that __del__() methods are called for objects
> that still exist when the interpreter exits.
- Exceptions that occur inside __del__ are ignored instead of raised.
This has the potential of hiding bugs. This is also in the Python
documentation:
> Warning: Due to the precarious circumstances under which __del__()
> methods are invoked, exceptions that occur during their execution
> are ignored, and a warning is printed to sys.stderr instead.
Instead, always close resource handlers when they are no longer in use.
This will close the file handler at a specified point in the user's code
and not wait until the interpreter chooses to. It is always guaranteed
to run. And, if an exception occurs while closing the file handler, the
bug will not be ignored.
Now, when code receives a ResourceWarning, it will highlight an area
that is mishandling resources. It should not simply be silenced, but
fixed by closing resources with a context manager.
All warnings that were emitted during tests have been cleaned up. To
enable warnings, I passed the `-Wa` CLI option to Python. This exposed
some mishandling of resources in ImageFile.__init__() and
SpiderImagePlugin.loadImageSeries(), they too were fixed.
2019-05-25 19:30:58 +03:00
|
|
|
loaded_float = loaded.tag[tag_ids["RollAngle"]][0]
|
|
|
|
self.assertAlmostEqual(loaded_float, floatdata, places=5)
|
|
|
|
loaded_double = loaded.tag[tag_ids["YawAngle"]][0]
|
|
|
|
self.assertAlmostEqual(loaded_double, doubledata)
|
2013-10-03 09:06:17 +04:00
|
|
|
|
2017-09-14 19:11:56 +03:00
|
|
|
# check with 2 element ImageJMetaDataByteCounts, issue #2006
|
2018-01-27 09:07:24 +03:00
|
|
|
|
2017-09-14 19:11:56 +03:00
|
|
|
info[ImageJMetaDataByteCounts] = (8, len(bindata) - 8)
|
|
|
|
img.save(f, tiffinfo=info)
|
Improve handling of file resources
Follow Python's file object semantics. User code is responsible for
closing resources (usually through a context manager) in a deterministic
way.
To achieve this, remove __del__ functions. These functions used to
closed open file handlers in an attempt to silence Python
ResourceWarnings. However, using __del__ has the following drawbacks:
- __del__ isn't called until the object's reference count reaches 0.
Therefore, resource handlers remain open or in use longer than
necessary.
- The __del__ method isn't guaranteed to execute on system exit. See the
Python documentation:
https://docs.python.org/3/reference/datamodel.html#object.__del__
> It is not guaranteed that __del__() methods are called for objects
> that still exist when the interpreter exits.
- Exceptions that occur inside __del__ are ignored instead of raised.
This has the potential of hiding bugs. This is also in the Python
documentation:
> Warning: Due to the precarious circumstances under which __del__()
> methods are invoked, exceptions that occur during their execution
> are ignored, and a warning is printed to sys.stderr instead.
Instead, always close resource handlers when they are no longer in use.
This will close the file handler at a specified point in the user's code
and not wait until the interpreter chooses to. It is always guaranteed
to run. And, if an exception occurs while closing the file handler, the
bug will not be ignored.
Now, when code receives a ResourceWarning, it will highlight an area
that is mishandling resources. It should not simply be silenced, but
fixed by closing resources with a context manager.
All warnings that were emitted during tests have been cleaned up. To
enable warnings, I passed the `-Wa` CLI option to Python. This exposed
some mishandling of resources in ImageFile.__init__() and
SpiderImagePlugin.loadImageSeries(), they too were fixed.
2019-05-25 19:30:58 +03:00
|
|
|
with Image.open(f) as loaded:
|
2017-09-14 19:11:56 +03:00
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
assert loaded.tag[ImageJMetaDataByteCounts] == (8, len(bindata) - 8)
|
|
|
|
assert loaded.tag_v2[ImageJMetaDataByteCounts] == (8, len(bindata) - 8)
|
2017-09-14 19:11:56 +03:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_read_metadata(self):
|
Improve handling of file resources
Follow Python's file object semantics. User code is responsible for
closing resources (usually through a context manager) in a deterministic
way.
To achieve this, remove __del__ functions. These functions used to
closed open file handlers in an attempt to silence Python
ResourceWarnings. However, using __del__ has the following drawbacks:
- __del__ isn't called until the object's reference count reaches 0.
Therefore, resource handlers remain open or in use longer than
necessary.
- The __del__ method isn't guaranteed to execute on system exit. See the
Python documentation:
https://docs.python.org/3/reference/datamodel.html#object.__del__
> It is not guaranteed that __del__() methods are called for objects
> that still exist when the interpreter exits.
- Exceptions that occur inside __del__ are ignored instead of raised.
This has the potential of hiding bugs. This is also in the Python
documentation:
> Warning: Due to the precarious circumstances under which __del__()
> methods are invoked, exceptions that occur during their execution
> are ignored, and a warning is printed to sys.stderr instead.
Instead, always close resource handlers when they are no longer in use.
This will close the file handler at a specified point in the user's code
and not wait until the interpreter chooses to. It is always guaranteed
to run. And, if an exception occurs while closing the file handler, the
bug will not be ignored.
Now, when code receives a ResourceWarning, it will highlight an area
that is mishandling resources. It should not simply be silenced, but
fixed by closing resources with a context manager.
All warnings that were emitted during tests have been cleaned up. To
enable warnings, I passed the `-Wa` CLI option to Python. This exposed
some mishandling of resources in ImageFile.__init__() and
SpiderImagePlugin.loadImageSeries(), they too were fixed.
2019-05-25 19:30:58 +03:00
|
|
|
with Image.open("Tests/images/hopper_g4.tif") as img:
|
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
assert {
|
|
|
|
"YResolution": IFDRational(4294967295, 113653537),
|
|
|
|
"PlanarConfiguration": 1,
|
|
|
|
"BitsPerSample": (1,),
|
|
|
|
"ImageLength": 128,
|
|
|
|
"Compression": 4,
|
|
|
|
"FillOrder": 1,
|
|
|
|
"RowsPerStrip": 128,
|
|
|
|
"ResolutionUnit": 3,
|
|
|
|
"PhotometricInterpretation": 0,
|
|
|
|
"PageNumber": (0, 1),
|
|
|
|
"XResolution": IFDRational(4294967295, 113653537),
|
|
|
|
"ImageWidth": 128,
|
|
|
|
"Orientation": 1,
|
|
|
|
"StripByteCounts": (1968,),
|
|
|
|
"SamplesPerPixel": 1,
|
|
|
|
"StripOffsets": (8,),
|
|
|
|
} == img.tag_v2.named()
|
|
|
|
|
|
|
|
assert {
|
|
|
|
"YResolution": ((4294967295, 113653537),),
|
|
|
|
"PlanarConfiguration": (1,),
|
|
|
|
"BitsPerSample": (1,),
|
|
|
|
"ImageLength": (128,),
|
|
|
|
"Compression": (4,),
|
|
|
|
"FillOrder": (1,),
|
|
|
|
"RowsPerStrip": (128,),
|
|
|
|
"ResolutionUnit": (3,),
|
|
|
|
"PhotometricInterpretation": (0,),
|
|
|
|
"PageNumber": (0, 1),
|
|
|
|
"XResolution": ((4294967295, 113653537),),
|
|
|
|
"ImageWidth": (128,),
|
|
|
|
"Orientation": (1,),
|
|
|
|
"StripByteCounts": (1968,),
|
|
|
|
"SamplesPerPixel": (1,),
|
|
|
|
"StripOffsets": (8,),
|
|
|
|
} == img.tag.named()
|
2013-10-08 10:01:15 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_write_metadata(self):
|
|
|
|
""" Test metadata writing through the python code """
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open("Tests/images/hopper.tif") as img:
|
|
|
|
f = self.tempfile("temp.tiff")
|
|
|
|
img.save(f, tiffinfo=img.tag)
|
2013-10-08 10:01:15 +04:00
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
original = img.tag_v2.named()
|
2013-10-08 10:01:15 +04:00
|
|
|
|
Improve handling of file resources
Follow Python's file object semantics. User code is responsible for
closing resources (usually through a context manager) in a deterministic
way.
To achieve this, remove __del__ functions. These functions used to
closed open file handlers in an attempt to silence Python
ResourceWarnings. However, using __del__ has the following drawbacks:
- __del__ isn't called until the object's reference count reaches 0.
Therefore, resource handlers remain open or in use longer than
necessary.
- The __del__ method isn't guaranteed to execute on system exit. See the
Python documentation:
https://docs.python.org/3/reference/datamodel.html#object.__del__
> It is not guaranteed that __del__() methods are called for objects
> that still exist when the interpreter exits.
- Exceptions that occur inside __del__ are ignored instead of raised.
This has the potential of hiding bugs. This is also in the Python
documentation:
> Warning: Due to the precarious circumstances under which __del__()
> methods are invoked, exceptions that occur during their execution
> are ignored, and a warning is printed to sys.stderr instead.
Instead, always close resource handlers when they are no longer in use.
This will close the file handler at a specified point in the user's code
and not wait until the interpreter chooses to. It is always guaranteed
to run. And, if an exception occurs while closing the file handler, the
bug will not be ignored.
Now, when code receives a ResourceWarning, it will highlight an area
that is mishandling resources. It should not simply be silenced, but
fixed by closing resources with a context manager.
All warnings that were emitted during tests have been cleaned up. To
enable warnings, I passed the `-Wa` CLI option to Python. This exposed
some mishandling of resources in ImageFile.__init__() and
SpiderImagePlugin.loadImageSeries(), they too were fixed.
2019-05-25 19:30:58 +03:00
|
|
|
with Image.open(f) as loaded:
|
|
|
|
reloaded = loaded.tag_v2.named()
|
2013-10-08 10:01:15 +04:00
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
ignored = ["StripByteCounts", "RowsPerStrip", "PageNumber", "StripOffsets"]
|
2013-10-08 10:01:15 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
for tag, value in reloaded.items():
|
2016-02-05 01:57:13 +03:00
|
|
|
if tag in ignored:
|
|
|
|
continue
|
2019-06-13 18:54:11 +03:00
|
|
|
if isinstance(original[tag], tuple) and isinstance(
|
|
|
|
original[tag][0], IFDRational
|
|
|
|
):
|
2015-12-27 14:27:18 +03:00
|
|
|
# Need to compare element by element in the tuple,
|
|
|
|
# not comparing tuples of object references
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_deep_equal(
|
2019-06-13 18:54:11 +03:00
|
|
|
original[tag],
|
|
|
|
value,
|
2019-09-30 17:56:31 +03:00
|
|
|
"{} didn't roundtrip, {}, {}".format(tag, original[tag], value),
|
2019-06-13 18:54:11 +03:00
|
|
|
)
|
2015-12-27 14:27:18 +03:00
|
|
|
else:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert original[tag] == value, "{} didn't roundtrip, {}, {}".format(
|
|
|
|
tag, original[tag], value
|
2019-06-13 18:54:11 +03:00
|
|
|
)
|
2013-10-08 10:01:15 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
for tag, value in original.items():
|
|
|
|
if tag not in ignored:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert value == reloaded[tag], "%s didn't roundtrip" % tag
|
2013-10-08 10:01:15 +04:00
|
|
|
|
2014-08-26 16:51:37 +04:00
|
|
|
def test_no_duplicate_50741_tag(self):
|
2020-02-22 16:06:21 +03:00
|
|
|
assert tag_ids["MakerNoteSafety"] == 50741
|
|
|
|
assert tag_ids["BestQualityScale"] == 50780
|
2014-08-26 16:51:37 +04:00
|
|
|
|
2015-09-15 04:06:51 +03:00
|
|
|
def test_empty_metadata(self):
|
2019-06-13 18:54:11 +03:00
|
|
|
f = io.BytesIO(b"II*\x00\x08\x00\x00\x00")
|
2015-09-15 04:06:51 +03:00
|
|
|
head = f.read(8)
|
|
|
|
info = TiffImagePlugin.ImageFileDirectory(head)
|
2016-11-06 19:58:45 +03:00
|
|
|
# Should not raise struct.error.
|
2020-02-03 12:11:32 +03:00
|
|
|
pytest.warns(UserWarning, info.load, f)
|
2015-09-15 04:06:51 +03:00
|
|
|
|
2015-10-03 18:35:53 +03:00
|
|
|
def test_iccprofile(self):
|
|
|
|
# https://github.com/python-pillow/Pillow/issues/1462
|
2019-06-13 18:54:11 +03:00
|
|
|
out = self.tempfile("temp.tiff")
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open("Tests/images/hopper.iccprofile.tif") as im:
|
|
|
|
im.save(out)
|
2015-10-03 18:35:53 +03:00
|
|
|
|
Improve handling of file resources
Follow Python's file object semantics. User code is responsible for
closing resources (usually through a context manager) in a deterministic
way.
To achieve this, remove __del__ functions. These functions used to
closed open file handlers in an attempt to silence Python
ResourceWarnings. However, using __del__ has the following drawbacks:
- __del__ isn't called until the object's reference count reaches 0.
Therefore, resource handlers remain open or in use longer than
necessary.
- The __del__ method isn't guaranteed to execute on system exit. See the
Python documentation:
https://docs.python.org/3/reference/datamodel.html#object.__del__
> It is not guaranteed that __del__() methods are called for objects
> that still exist when the interpreter exits.
- Exceptions that occur inside __del__ are ignored instead of raised.
This has the potential of hiding bugs. This is also in the Python
documentation:
> Warning: Due to the precarious circumstances under which __del__()
> methods are invoked, exceptions that occur during their execution
> are ignored, and a warning is printed to sys.stderr instead.
Instead, always close resource handlers when they are no longer in use.
This will close the file handler at a specified point in the user's code
and not wait until the interpreter chooses to. It is always guaranteed
to run. And, if an exception occurs while closing the file handler, the
bug will not be ignored.
Now, when code receives a ResourceWarning, it will highlight an area
that is mishandling resources. It should not simply be silenced, but
fixed by closing resources with a context manager.
All warnings that were emitted during tests have been cleaned up. To
enable warnings, I passed the `-Wa` CLI option to Python. This exposed
some mishandling of resources in ImageFile.__init__() and
SpiderImagePlugin.loadImageSeries(), they too were fixed.
2019-05-25 19:30:58 +03:00
|
|
|
with Image.open(out) as reloaded:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert not isinstance(im.info["icc_profile"], tuple)
|
|
|
|
assert im.info["icc_profile"] == reloaded.info["icc_profile"]
|
2015-10-03 18:35:53 +03:00
|
|
|
|
2015-11-15 19:41:49 +03:00
|
|
|
def test_iccprofile_binary(self):
|
|
|
|
# https://github.com/python-pillow/Pillow/issues/1526
|
2018-09-27 13:35:00 +03:00
|
|
|
# We should be able to load this,
|
|
|
|
# but probably won't be able to save it.
|
2015-11-15 19:41:49 +03:00
|
|
|
|
Improve handling of file resources
Follow Python's file object semantics. User code is responsible for
closing resources (usually through a context manager) in a deterministic
way.
To achieve this, remove __del__ functions. These functions used to
closed open file handlers in an attempt to silence Python
ResourceWarnings. However, using __del__ has the following drawbacks:
- __del__ isn't called until the object's reference count reaches 0.
Therefore, resource handlers remain open or in use longer than
necessary.
- The __del__ method isn't guaranteed to execute on system exit. See the
Python documentation:
https://docs.python.org/3/reference/datamodel.html#object.__del__
> It is not guaranteed that __del__() methods are called for objects
> that still exist when the interpreter exits.
- Exceptions that occur inside __del__ are ignored instead of raised.
This has the potential of hiding bugs. This is also in the Python
documentation:
> Warning: Due to the precarious circumstances under which __del__()
> methods are invoked, exceptions that occur during their execution
> are ignored, and a warning is printed to sys.stderr instead.
Instead, always close resource handlers when they are no longer in use.
This will close the file handler at a specified point in the user's code
and not wait until the interpreter chooses to. It is always guaranteed
to run. And, if an exception occurs while closing the file handler, the
bug will not be ignored.
Now, when code receives a ResourceWarning, it will highlight an area
that is mishandling resources. It should not simply be silenced, but
fixed by closing resources with a context manager.
All warnings that were emitted during tests have been cleaned up. To
enable warnings, I passed the `-Wa` CLI option to Python. This exposed
some mishandling of resources in ImageFile.__init__() and
SpiderImagePlugin.loadImageSeries(), they too were fixed.
2019-05-25 19:30:58 +03:00
|
|
|
with Image.open("Tests/images/hopper.iccprofile_binary.tif") as im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.tag_v2.tagtype[34675] == 1
|
|
|
|
assert im.info["icc_profile"]
|
2015-11-15 19:41:49 +03:00
|
|
|
|
2016-06-26 14:05:05 +03:00
|
|
|
def test_iccprofile_save_png(self):
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open("Tests/images/hopper.iccprofile.tif") as im:
|
|
|
|
outfile = self.tempfile("temp.png")
|
|
|
|
im.save(outfile)
|
2016-06-26 14:05:05 +03:00
|
|
|
|
|
|
|
def test_iccprofile_binary_save_png(self):
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open("Tests/images/hopper.iccprofile_binary.tif") as im:
|
|
|
|
outfile = self.tempfile("temp.png")
|
|
|
|
im.save(outfile)
|
2016-06-26 14:05:05 +03:00
|
|
|
|
2015-12-30 01:00:36 +03:00
|
|
|
def test_exif_div_zero(self):
|
|
|
|
im = hopper()
|
|
|
|
info = TiffImagePlugin.ImageFileDirectory_v2()
|
2016-02-05 01:57:13 +03:00
|
|
|
info[41988] = TiffImagePlugin.IFDRational(0, 0)
|
2015-12-30 01:00:36 +03:00
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
out = self.tempfile("temp.tiff")
|
|
|
|
im.save(out, tiffinfo=info, compression="raw")
|
2015-12-30 01:00:36 +03:00
|
|
|
|
Improve handling of file resources
Follow Python's file object semantics. User code is responsible for
closing resources (usually through a context manager) in a deterministic
way.
To achieve this, remove __del__ functions. These functions used to
closed open file handlers in an attempt to silence Python
ResourceWarnings. However, using __del__ has the following drawbacks:
- __del__ isn't called until the object's reference count reaches 0.
Therefore, resource handlers remain open or in use longer than
necessary.
- The __del__ method isn't guaranteed to execute on system exit. See the
Python documentation:
https://docs.python.org/3/reference/datamodel.html#object.__del__
> It is not guaranteed that __del__() methods are called for objects
> that still exist when the interpreter exits.
- Exceptions that occur inside __del__ are ignored instead of raised.
This has the potential of hiding bugs. This is also in the Python
documentation:
> Warning: Due to the precarious circumstances under which __del__()
> methods are invoked, exceptions that occur during their execution
> are ignored, and a warning is printed to sys.stderr instead.
Instead, always close resource handlers when they are no longer in use.
This will close the file handler at a specified point in the user's code
and not wait until the interpreter chooses to. It is always guaranteed
to run. And, if an exception occurs while closing the file handler, the
bug will not be ignored.
Now, when code receives a ResourceWarning, it will highlight an area
that is mishandling resources. It should not simply be silenced, but
fixed by closing resources with a context manager.
All warnings that were emitted during tests have been cleaned up. To
enable warnings, I passed the `-Wa` CLI option to Python. This exposed
some mishandling of resources in ImageFile.__init__() and
SpiderImagePlugin.loadImageSeries(), they too were fixed.
2019-05-25 19:30:58 +03:00
|
|
|
with Image.open(out) as reloaded:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert 0 == reloaded.tag_v2[41988].numerator
|
|
|
|
assert 0 == reloaded.tag_v2[41988].denominator
|
2015-12-30 01:00:36 +03:00
|
|
|
|
2019-07-20 00:12:16 +03:00
|
|
|
def test_ifd_unsigned_rational(self):
|
|
|
|
im = hopper()
|
|
|
|
info = TiffImagePlugin.ImageFileDirectory_v2()
|
|
|
|
|
|
|
|
max_long = 2 ** 32 - 1
|
|
|
|
|
|
|
|
# 4 bytes unsigned long
|
|
|
|
numerator = max_long
|
|
|
|
|
|
|
|
info[41493] = TiffImagePlugin.IFDRational(numerator, 1)
|
|
|
|
|
|
|
|
out = self.tempfile("temp.tiff")
|
|
|
|
im.save(out, tiffinfo=info, compression="raw")
|
|
|
|
|
2020-01-27 02:59:20 +03:00
|
|
|
with Image.open(out) as reloaded:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert max_long == reloaded.tag_v2[41493].numerator
|
|
|
|
assert 1 == reloaded.tag_v2[41493].denominator
|
2019-07-20 00:12:16 +03:00
|
|
|
|
|
|
|
# out of bounds of 4 byte unsigned long
|
|
|
|
numerator = max_long + 1
|
|
|
|
|
|
|
|
info[41493] = TiffImagePlugin.IFDRational(numerator, 1)
|
|
|
|
|
|
|
|
out = self.tempfile("temp.tiff")
|
|
|
|
im.save(out, tiffinfo=info, compression="raw")
|
|
|
|
|
2020-01-27 02:59:20 +03:00
|
|
|
with Image.open(out) as reloaded:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert max_long == reloaded.tag_v2[41493].numerator
|
|
|
|
assert 1 == reloaded.tag_v2[41493].denominator
|
2019-07-20 00:12:16 +03:00
|
|
|
|
|
|
|
def test_ifd_signed_rational(self):
|
|
|
|
im = hopper()
|
|
|
|
info = TiffImagePlugin.ImageFileDirectory_v2()
|
|
|
|
|
|
|
|
# pair of 4 byte signed longs
|
|
|
|
numerator = 2 ** 31 - 1
|
2019-12-31 03:58:39 +03:00
|
|
|
denominator = -(2 ** 31)
|
2019-07-20 00:12:16 +03:00
|
|
|
|
|
|
|
info[37380] = TiffImagePlugin.IFDRational(numerator, denominator)
|
|
|
|
|
|
|
|
out = self.tempfile("temp.tiff")
|
|
|
|
im.save(out, tiffinfo=info, compression="raw")
|
|
|
|
|
2020-01-27 02:59:20 +03:00
|
|
|
with Image.open(out) as reloaded:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert numerator == reloaded.tag_v2[37380].numerator
|
|
|
|
assert denominator == reloaded.tag_v2[37380].denominator
|
2019-07-20 00:12:16 +03:00
|
|
|
|
2019-12-31 03:58:39 +03:00
|
|
|
numerator = -(2 ** 31)
|
2019-07-20 00:12:16 +03:00
|
|
|
denominator = 2 ** 31 - 1
|
|
|
|
|
|
|
|
info[37380] = TiffImagePlugin.IFDRational(numerator, denominator)
|
|
|
|
|
|
|
|
out = self.tempfile("temp.tiff")
|
|
|
|
im.save(out, tiffinfo=info, compression="raw")
|
|
|
|
|
2020-01-27 02:59:20 +03:00
|
|
|
with Image.open(out) as reloaded:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert numerator == reloaded.tag_v2[37380].numerator
|
|
|
|
assert denominator == reloaded.tag_v2[37380].denominator
|
2019-07-20 00:12:16 +03:00
|
|
|
|
2019-12-31 11:11:03 +03:00
|
|
|
# out of bounds of 4 byte signed long
|
2019-12-31 11:12:33 +03:00
|
|
|
numerator = -(2 ** 31) - 1
|
2019-12-31 11:11:03 +03:00
|
|
|
denominator = 1
|
|
|
|
|
|
|
|
info[37380] = TiffImagePlugin.IFDRational(numerator, denominator)
|
|
|
|
|
|
|
|
out = self.tempfile("temp.tiff")
|
|
|
|
im.save(out, tiffinfo=info, compression="raw")
|
|
|
|
|
2020-01-27 02:59:20 +03:00
|
|
|
with Image.open(out) as reloaded:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert 2 ** 31 - 1 == reloaded.tag_v2[37380].numerator
|
|
|
|
assert -1 == reloaded.tag_v2[37380].denominator
|
2019-12-31 11:11:03 +03:00
|
|
|
|
2019-08-29 12:36:46 +03:00
|
|
|
def test_ifd_signed_long(self):
|
|
|
|
im = hopper()
|
|
|
|
info = TiffImagePlugin.ImageFileDirectory_v2()
|
|
|
|
|
|
|
|
info[37000] = -60000
|
|
|
|
|
|
|
|
out = self.tempfile("temp.tiff")
|
|
|
|
im.save(out, tiffinfo=info, compression="raw")
|
|
|
|
|
2020-01-27 02:59:20 +03:00
|
|
|
with Image.open(out) as reloaded:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert reloaded.tag_v2[37000] == -60000
|
2019-08-29 12:36:46 +03:00
|
|
|
|
2019-08-29 12:02:19 +03:00
|
|
|
def test_empty_values(self):
|
2016-07-12 18:09:02 +03:00
|
|
|
data = io.BytesIO(
|
2019-06-13 18:54:11 +03:00
|
|
|
b"II*\x00\x08\x00\x00\x00\x03\x00\x1a\x01\x05\x00\x00\x00\x00\x00"
|
|
|
|
b"\x00\x00\x00\x00\x1b\x01\x05\x00\x00\x00\x00\x00\x00\x00\x00\x00"
|
|
|
|
b"\x98\x82\x02\x00\x07\x00\x00\x002\x00\x00\x00\x00\x00\x00\x00a "
|
|
|
|
b"text\x00\x00"
|
|
|
|
)
|
2016-07-12 18:09:02 +03:00
|
|
|
head = data.read(8)
|
|
|
|
info = TiffImagePlugin.ImageFileDirectory_v2(head)
|
|
|
|
info.load(data)
|
2016-11-06 19:58:45 +03:00
|
|
|
# Should not raise ValueError.
|
|
|
|
info = dict(info)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert 33432 in info
|
2016-07-12 18:09:02 +03:00
|
|
|
|
2017-09-14 19:25:11 +03:00
|
|
|
def test_PhotoshopInfo(self):
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open("Tests/images/issue_2278.tif") as im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert len(im.tag_v2[34377]) == 1
|
|
|
|
assert isinstance(im.tag_v2[34377][0], bytes)
|
2019-11-25 23:03:23 +03:00
|
|
|
out = self.tempfile("temp.tiff")
|
|
|
|
im.save(out)
|
Improve handling of file resources
Follow Python's file object semantics. User code is responsible for
closing resources (usually through a context manager) in a deterministic
way.
To achieve this, remove __del__ functions. These functions used to
closed open file handlers in an attempt to silence Python
ResourceWarnings. However, using __del__ has the following drawbacks:
- __del__ isn't called until the object's reference count reaches 0.
Therefore, resource handlers remain open or in use longer than
necessary.
- The __del__ method isn't guaranteed to execute on system exit. See the
Python documentation:
https://docs.python.org/3/reference/datamodel.html#object.__del__
> It is not guaranteed that __del__() methods are called for objects
> that still exist when the interpreter exits.
- Exceptions that occur inside __del__ are ignored instead of raised.
This has the potential of hiding bugs. This is also in the Python
documentation:
> Warning: Due to the precarious circumstances under which __del__()
> methods are invoked, exceptions that occur during their execution
> are ignored, and a warning is printed to sys.stderr instead.
Instead, always close resource handlers when they are no longer in use.
This will close the file handler at a specified point in the user's code
and not wait until the interpreter chooses to. It is always guaranteed
to run. And, if an exception occurs while closing the file handler, the
bug will not be ignored.
Now, when code receives a ResourceWarning, it will highlight an area
that is mishandling resources. It should not simply be silenced, but
fixed by closing resources with a context manager.
All warnings that were emitted during tests have been cleaned up. To
enable warnings, I passed the `-Wa` CLI option to Python. This exposed
some mishandling of resources in ImageFile.__init__() and
SpiderImagePlugin.loadImageSeries(), they too were fixed.
2019-05-25 19:30:58 +03:00
|
|
|
with Image.open(out) as reloaded:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert len(reloaded.tag_v2[34377]) == 1
|
|
|
|
assert isinstance(reloaded.tag_v2[34377][0], bytes)
|
2015-12-30 01:00:36 +03:00
|
|
|
|
2017-09-19 13:35:14 +03:00
|
|
|
def test_too_many_entries(self):
|
|
|
|
ifd = TiffImagePlugin.ImageFileDirectory_v2()
|
|
|
|
|
|
|
|
# 277: ("SamplesPerPixel", SHORT, 1),
|
2019-06-13 18:54:11 +03:00
|
|
|
ifd._tagdata[277] = struct.pack("hh", 4, 4)
|
2017-09-19 13:35:14 +03:00
|
|
|
ifd.tagtype[277] = TiffTags.SHORT
|
|
|
|
|
2016-11-06 19:58:45 +03:00
|
|
|
# Should not raise ValueError.
|
2020-02-03 12:11:32 +03:00
|
|
|
pytest.warns(UserWarning, lambda: ifd[277])
|