2015-01-18 21:56:29 +03:00
|
|
|
import os
|
2020-02-22 19:07:04 +03:00
|
|
|
import re
|
2022-02-21 05:49:01 +03:00
|
|
|
import warnings
|
2019-07-06 23:40:53 +03:00
|
|
|
from io import BytesIO
|
|
|
|
|
2020-02-03 12:11:32 +03:00
|
|
|
import pytest
|
2020-08-07 13:28:33 +03:00
|
|
|
|
2020-06-22 12:20:57 +03:00
|
|
|
from PIL import (
|
|
|
|
ExifTags,
|
|
|
|
Image,
|
|
|
|
ImageFile,
|
2020-09-02 14:14:36 +03:00
|
|
|
ImageOps,
|
2020-06-22 12:20:57 +03:00
|
|
|
JpegImagePlugin,
|
|
|
|
UnidentifiedImageError,
|
|
|
|
features,
|
|
|
|
)
|
2014-01-27 23:27:03 +04:00
|
|
|
|
2019-09-25 12:46:54 +03:00
|
|
|
from .helper import (
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image,
|
|
|
|
assert_image_equal,
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile,
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_similar,
|
2021-02-21 14:22:29 +03:00
|
|
|
assert_image_similar_tofile,
|
2019-09-25 12:46:54 +03:00
|
|
|
cjpeg_available,
|
|
|
|
djpeg_available,
|
|
|
|
hopper,
|
|
|
|
is_win32,
|
2021-04-10 00:33:21 +03:00
|
|
|
mark_if_feature_version,
|
2020-02-18 01:03:32 +03:00
|
|
|
skip_unless_feature,
|
2019-09-25 12:46:54 +03:00
|
|
|
)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2021-06-30 04:28:00 +03:00
|
|
|
try:
|
2022-10-13 05:20:11 +03:00
|
|
|
from defusedxml import ElementTree
|
2021-06-30 04:28:00 +03:00
|
|
|
except ImportError:
|
|
|
|
ElementTree = None
|
|
|
|
|
2014-09-23 20:52:03 +04:00
|
|
|
TEST_FILE = "Tests/images/hopper.jpg"
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
|
2020-02-18 01:03:32 +03:00
|
|
|
@skip_unless_feature("jpg")
|
2020-03-02 17:02:19 +03:00
|
|
|
class TestFileJpeg:
|
2014-06-10 13:10:47 +04:00
|
|
|
def roundtrip(self, im, **options):
|
|
|
|
out = BytesIO()
|
|
|
|
im.save(out, "JPEG", **options)
|
2015-04-24 11:24:52 +03:00
|
|
|
test_bytes = out.tell()
|
2014-06-10 13:10:47 +04:00
|
|
|
out.seek(0)
|
|
|
|
im = Image.open(out)
|
2015-04-24 11:24:52 +03:00
|
|
|
im.bytes = test_bytes # for testing only
|
2014-06-10 13:10:47 +04:00
|
|
|
return im
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
def gen_random_image(self, size, mode="RGB"):
|
2020-08-31 00:37:17 +03:00
|
|
|
"""Generates a very hard to compress file
|
2016-12-03 17:45:05 +03:00
|
|
|
:param size: tuple
|
|
|
|
:param mode: optional image mode
|
2017-03-03 13:32:31 +03:00
|
|
|
|
2016-12-03 17:45:05 +03:00
|
|
|
"""
|
2019-06-13 18:54:11 +03:00
|
|
|
return Image.frombytes(mode, size, os.urandom(size[0] * size[1] * len(mode)))
|
2017-03-03 13:32:31 +03:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_sanity(self):
|
|
|
|
# internal version number
|
2019-10-12 16:29:10 +03:00
|
|
|
assert re.search(r"\d+\.\d+$", features.version_codec("jpg"))
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(TEST_FILE) as im:
|
|
|
|
im.load()
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.mode == "RGB"
|
|
|
|
assert im.size == (128, 128)
|
|
|
|
assert im.format == "JPEG"
|
|
|
|
assert im.get_format_mimetype() == "image/jpeg"
|
2014-05-21 15:32:24 +04:00
|
|
|
|
2022-03-28 13:18:53 +03:00
|
|
|
@pytest.mark.parametrize("size", ((1, 0), (0, 1), (0, 0)))
|
|
|
|
def test_zero(self, size, tmp_path):
|
|
|
|
f = str(tmp_path / "temp.jpg")
|
|
|
|
im = Image.new("RGB", size)
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
im.save(f)
|
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_app(self):
|
|
|
|
# Test APP/COM reader (@PIL135)
|
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(TEST_FILE) as im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.applist[0] == ("APP0", b"JFIF\x00\x01\x01\x01\x00`\x00`\x00\x00")
|
|
|
|
assert im.applist[1] == (
|
|
|
|
"COM",
|
|
|
|
b"File written by Adobe Photoshop\xa8 4.0\x00",
|
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
|
|
|
)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert len(im.applist) == 2
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2020-03-06 22:57:29 +03:00
|
|
|
assert im.info["comment"] == b"File written by Adobe Photoshop\xa8 4.0\x00"
|
2022-12-03 01:59:22 +03:00
|
|
|
assert im.app["COM"] == im.info["comment"]
|
2020-03-06 22:57:29 +03:00
|
|
|
|
2022-12-03 01:59:22 +03:00
|
|
|
def test_comment_write(self):
|
2022-12-02 20:57:19 +03:00
|
|
|
with Image.open(TEST_FILE) as im:
|
2022-12-03 01:59:22 +03:00
|
|
|
assert im.info["comment"] == b"File written by Adobe Photoshop\xa8 4.0\x00"
|
|
|
|
|
|
|
|
# Test that existing comment is saved by default
|
|
|
|
out = BytesIO()
|
|
|
|
im.save(out, format="JPEG")
|
|
|
|
with Image.open(out) as reloaded:
|
|
|
|
assert im.info["comment"] == reloaded.info["comment"]
|
|
|
|
|
2022-12-03 18:07:37 +03:00
|
|
|
# Ensure that a blank comment causes any existing comment to be removed
|
|
|
|
for comment in ("", b"", None):
|
|
|
|
out = BytesIO()
|
|
|
|
im.save(out, format="JPEG", comment=comment)
|
|
|
|
with Image.open(out) as reloaded:
|
|
|
|
assert "comment" not in reloaded.info
|
|
|
|
|
2022-12-03 01:59:22 +03:00
|
|
|
# Test that a comment argument overrides the default comment
|
|
|
|
for comment in ("Test comment text", b"Text comment text"):
|
|
|
|
out = BytesIO()
|
|
|
|
im.save(out, format="JPEG", comment=comment)
|
|
|
|
with Image.open(out) as reloaded:
|
|
|
|
if not isinstance(comment, bytes):
|
|
|
|
comment = comment.encode()
|
|
|
|
assert reloaded.info["comment"] == comment
|
2022-12-02 20:57:19 +03:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_cmyk(self):
|
|
|
|
# Test CMYK handling. Thanks to Tim and Charlie for test data,
|
|
|
|
# Michael for getting me to look one more time.
|
|
|
|
f = "Tests/images/pil_sample_cmyk.jpg"
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(f) as im:
|
|
|
|
# the source image has red pixels in the upper left corner.
|
2021-10-15 13:10:22 +03:00
|
|
|
c, m, y, k = (x / 255.0 for x in im.getpixel((0, 0)))
|
2020-02-22 16:06:21 +03:00
|
|
|
assert c == 0.0
|
|
|
|
assert m > 0.8
|
|
|
|
assert y > 0.8
|
|
|
|
assert k == 0.0
|
2019-11-25 23:03:23 +03:00
|
|
|
# the opposite corner is black
|
2021-10-15 13:10:22 +03:00
|
|
|
c, m, y, k = (
|
2019-11-25 23:03:23 +03:00
|
|
|
x / 255.0 for x in im.getpixel((im.size[0] - 1, im.size[1] - 1))
|
2021-10-15 13:10:22 +03:00
|
|
|
)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert k > 0.9
|
2019-11-25 23:03:23 +03:00
|
|
|
# roundtrip, and check again
|
|
|
|
im = self.roundtrip(im)
|
2021-10-15 13:10:22 +03:00
|
|
|
c, m, y, k = (x / 255.0 for x in im.getpixel((0, 0)))
|
2020-02-22 16:06:21 +03:00
|
|
|
assert c == 0.0
|
|
|
|
assert m > 0.8
|
|
|
|
assert y > 0.8
|
|
|
|
assert k == 0.0
|
2021-10-15 13:10:22 +03:00
|
|
|
c, m, y, k = (
|
2019-11-25 23:03:23 +03:00
|
|
|
x / 255.0 for x in im.getpixel((im.size[0] - 1, im.size[1] - 1))
|
2021-10-15 13:10:22 +03:00
|
|
|
)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert k > 0.9
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2020-05-26 00:15:20 +03:00
|
|
|
@pytest.mark.parametrize(
|
2020-08-31 00:37:17 +03:00
|
|
|
"test_image_path",
|
|
|
|
[TEST_FILE, "Tests/images/pil_sample_cmyk.jpg"],
|
2020-05-26 00:15:20 +03:00
|
|
|
)
|
|
|
|
def test_dpi(self, test_image_path):
|
2014-06-10 13:10:47 +04:00
|
|
|
def test(xdpi, ydpi=None):
|
2020-05-26 00:15:20 +03:00
|
|
|
with Image.open(test_image_path) as im:
|
2019-11-25 23:03:23 +03:00
|
|
|
im = self.roundtrip(im, dpi=(xdpi, ydpi or xdpi))
|
2014-06-10 13:10:47 +04:00
|
|
|
return im.info.get("dpi")
|
2019-06-13 18:54:11 +03:00
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
assert test(72) == (72, 72)
|
|
|
|
assert test(300) == (300, 300)
|
|
|
|
assert test(100, 200) == (100, 200)
|
|
|
|
assert test(0) is None # square pixels
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2021-04-10 17:58:01 +03:00
|
|
|
@mark_if_feature_version(
|
|
|
|
pytest.mark.valgrind_known_error, "libjpeg_turbo", "2.0", reason="Known Failing"
|
|
|
|
)
|
2020-03-02 17:02:19 +03:00
|
|
|
def test_icc(self, tmp_path):
|
2014-06-10 13:10:47 +04:00
|
|
|
# Test ICC support
|
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/rgb.jpg") as im1:
|
|
|
|
icc_profile = im1.info["icc_profile"]
|
2020-02-22 16:06:21 +03:00
|
|
|
assert len(icc_profile) == 3144
|
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
|
|
|
# Roundtrip via physical file.
|
2020-03-02 17:02:19 +03:00
|
|
|
f = str(tmp_path / "temp.jpg")
|
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
|
|
|
im1.save(f, icc_profile=icc_profile)
|
|
|
|
with Image.open(f) as im2:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im2.info.get("icc_profile") == icc_profile
|
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
|
|
|
# Roundtrip via memory buffer.
|
|
|
|
im1 = self.roundtrip(hopper())
|
|
|
|
im2 = self.roundtrip(hopper(), icc_profile=icc_profile)
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_equal(im1, im2)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert not im1.info.get("icc_profile")
|
|
|
|
assert im2.info.get("icc_profile")
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2022-10-03 08:57:42 +03:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"n",
|
|
|
|
(
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
3,
|
|
|
|
4,
|
|
|
|
5,
|
|
|
|
65533 - 14, # full JPEG marker block
|
|
|
|
65533 - 14 + 1, # full block plus one byte
|
|
|
|
ImageFile.MAXBLOCK, # full buffer block
|
|
|
|
ImageFile.MAXBLOCK + 1, # full buffer block plus one byte
|
|
|
|
ImageFile.MAXBLOCK * 4 + 3, # large block
|
|
|
|
),
|
|
|
|
)
|
|
|
|
def test_icc_big(self, n):
|
2014-06-10 13:10:47 +04:00
|
|
|
# Make sure that the "extra" support handles large blocks
|
2022-10-03 08:57:42 +03:00
|
|
|
# The ICC APP marker can store 65519 bytes per marker, so
|
|
|
|
# using a 4-byte test code should allow us to detect out of
|
|
|
|
# order issues.
|
|
|
|
icc_profile = (b"Test" * int(n / 4 + 1))[:n]
|
|
|
|
assert len(icc_profile) == n # sanity
|
|
|
|
im1 = self.roundtrip(hopper(), icc_profile=icc_profile)
|
|
|
|
assert im1.info.get("icc_profile") == (icc_profile or None)
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2021-04-10 17:58:01 +03:00
|
|
|
@mark_if_feature_version(
|
|
|
|
pytest.mark.valgrind_known_error, "libjpeg_turbo", "2.0", reason="Known Failing"
|
|
|
|
)
|
2020-03-02 17:02:19 +03:00
|
|
|
def test_large_icc_meta(self, tmp_path):
|
2017-06-27 16:31:52 +03:00
|
|
|
# https://github.com/python-pillow/Pillow/issues/148
|
2017-06-29 12:21:19 +03:00
|
|
|
# Sometimes the meta data on the icc_profile block is bigger than
|
2017-06-27 16:31:52 +03:00
|
|
|
# Image.MAXBLOCK or the image size.
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open("Tests/images/icc_profile_big.jpg") as im:
|
2020-03-02 17:02:19 +03:00
|
|
|
f = str(tmp_path / "temp.jpg")
|
2019-11-25 23:03:23 +03:00
|
|
|
icc_profile = im.info["icc_profile"]
|
2020-04-07 09:58:21 +03:00
|
|
|
# Should not raise OSError for image with icc larger than image size.
|
2019-11-25 23:03:23 +03:00
|
|
|
im.save(
|
|
|
|
f,
|
|
|
|
format="JPEG",
|
|
|
|
progressive=True,
|
|
|
|
quality=95,
|
|
|
|
icc_profile=icc_profile,
|
|
|
|
optimize=True,
|
|
|
|
)
|
2017-06-27 16:31:52 +03:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_optimize(self):
|
2014-09-23 20:52:03 +04:00
|
|
|
im1 = self.roundtrip(hopper())
|
2016-09-25 07:10:27 +03:00
|
|
|
im2 = self.roundtrip(hopper(), optimize=0)
|
|
|
|
im3 = self.roundtrip(hopper(), optimize=1)
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_equal(im1, im2)
|
|
|
|
assert_image_equal(im1, im3)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im1.bytes >= im2.bytes
|
|
|
|
assert im1.bytes >= im3.bytes
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2020-03-02 17:02:19 +03:00
|
|
|
def test_optimize_large_buffer(self, tmp_path):
|
2014-06-10 13:10:47 +04:00
|
|
|
# https://github.com/python-pillow/Pillow/issues/148
|
2020-03-02 17:02:19 +03:00
|
|
|
f = str(tmp_path / "temp.jpg")
|
2014-06-10 13:10:47 +04:00
|
|
|
# this requires ~ 1.5x Image.MAXBLOCK
|
2019-06-13 18:54:11 +03:00
|
|
|
im = Image.new("RGB", (4096, 4096), 0xFF3333)
|
2014-06-10 13:10:47 +04:00
|
|
|
im.save(f, format="JPEG", optimize=True)
|
|
|
|
|
|
|
|
def test_progressive(self):
|
2014-09-23 20:52:03 +04:00
|
|
|
im1 = self.roundtrip(hopper())
|
2016-09-25 07:10:27 +03:00
|
|
|
im2 = self.roundtrip(hopper(), progressive=False)
|
|
|
|
im3 = self.roundtrip(hopper(), progressive=True)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert not im1.info.get("progressive")
|
|
|
|
assert not im2.info.get("progressive")
|
|
|
|
assert im3.info.get("progressive")
|
2016-09-25 07:10:27 +03:00
|
|
|
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_equal(im1, im3)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im1.bytes >= im3.bytes
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2020-03-02 17:02:19 +03:00
|
|
|
def test_progressive_large_buffer(self, tmp_path):
|
|
|
|
f = str(tmp_path / "temp.jpg")
|
2014-06-10 13:10:47 +04:00
|
|
|
# this requires ~ 1.5x Image.MAXBLOCK
|
2019-06-13 18:54:11 +03:00
|
|
|
im = Image.new("RGB", (4096, 4096), 0xFF3333)
|
2014-06-10 13:10:47 +04:00
|
|
|
im.save(f, format="JPEG", progressive=True)
|
|
|
|
|
2020-03-02 17:02:19 +03:00
|
|
|
def test_progressive_large_buffer_highest_quality(self, tmp_path):
|
|
|
|
f = str(tmp_path / "temp.jpg")
|
2017-03-14 12:26:11 +03:00
|
|
|
im = self.gen_random_image((255, 255))
|
2014-06-10 13:10:47 +04:00
|
|
|
# this requires more bytes than pixels in the image
|
|
|
|
im.save(f, format="JPEG", progressive=True, quality=100)
|
|
|
|
|
2016-12-03 17:45:45 +03:00
|
|
|
def test_progressive_cmyk_buffer(self):
|
|
|
|
# Issue 2272, quality 90 cmyk image is tripping the large buffer bug.
|
|
|
|
f = BytesIO()
|
2019-06-13 18:54:11 +03:00
|
|
|
im = self.gen_random_image((256, 256), "CMYK")
|
|
|
|
im.save(f, format="JPEG", progressive=True, quality=94)
|
2017-03-03 13:32:31 +03:00
|
|
|
|
2020-03-02 17:02:19 +03:00
|
|
|
def test_large_exif(self, tmp_path):
|
2014-06-10 13:10:47 +04:00
|
|
|
# https://github.com/python-pillow/Pillow/issues/148
|
2020-03-02 17:02:19 +03:00
|
|
|
f = str(tmp_path / "temp.jpg")
|
2014-09-23 20:52:03 +04:00
|
|
|
im = hopper()
|
2023-02-11 08:20:27 +03:00
|
|
|
im.save(f, "JPEG", quality=90, exif=b"1" * 65533)
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
im.save(f, "JPEG", quality=90, exif=b"1" * 65534)
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2015-07-29 16:38:26 +03:00
|
|
|
def test_exif_typeerror(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/exif_typeerror.jpg") as im:
|
|
|
|
# Should not raise a TypeError
|
|
|
|
im._getexif()
|
2015-09-10 15:21:21 +03:00
|
|
|
|
2020-08-13 14:36:39 +03:00
|
|
|
def test_exif_gps(self, tmp_path):
|
|
|
|
expected_exif_gps = {
|
|
|
|
0: b"\x00\x00\x00\x01",
|
|
|
|
2: 4294967295,
|
|
|
|
5: b"\x01",
|
|
|
|
30: 65535,
|
|
|
|
29: "1999:99:99 99:99:99",
|
|
|
|
}
|
|
|
|
gps_index = 34853
|
|
|
|
|
|
|
|
# Reading
|
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/exif_gps.jpg") as im:
|
|
|
|
exif = im._getexif()
|
2020-08-13 14:36:39 +03:00
|
|
|
assert exif[gps_index] == expected_exif_gps
|
2015-10-07 12:24:15 +03:00
|
|
|
|
2020-08-13 14:36:39 +03:00
|
|
|
# Writing
|
|
|
|
f = str(tmp_path / "temp.jpg")
|
|
|
|
exif = Image.Exif()
|
|
|
|
exif[gps_index] = expected_exif_gps
|
|
|
|
hopper().save(f, exif=exif)
|
2015-10-07 12:24:15 +03:00
|
|
|
|
2020-08-13 14:36:39 +03:00
|
|
|
with Image.open(f) as reloaded:
|
|
|
|
exif = reloaded._getexif()
|
|
|
|
assert exif[gps_index] == expected_exif_gps
|
2015-10-07 12:24:15 +03:00
|
|
|
|
2020-09-02 14:14:36 +03:00
|
|
|
def test_empty_exif_gps(self):
|
|
|
|
with Image.open("Tests/images/empty_gps_ifd.jpg") as im:
|
|
|
|
exif = im.getexif()
|
|
|
|
del exif[0x8769]
|
|
|
|
|
|
|
|
# Assert that it needs to be transposed
|
2022-01-15 01:02:31 +03:00
|
|
|
assert exif[0x0112] == Image.Transpose.TRANSVERSE
|
2020-09-02 14:14:36 +03:00
|
|
|
|
|
|
|
# Assert that the GPS IFD is present and empty
|
2021-03-15 04:32:42 +03:00
|
|
|
assert exif.get_ifd(0x8825) == {}
|
2020-09-02 14:14:36 +03:00
|
|
|
|
|
|
|
transposed = ImageOps.exif_transpose(im)
|
|
|
|
exif = transposed.getexif()
|
2021-03-15 04:32:42 +03:00
|
|
|
assert exif.get_ifd(0x8825) == {}
|
2020-09-02 14:14:36 +03:00
|
|
|
|
|
|
|
# Assert that it was transposed
|
|
|
|
assert 0x0112 not in exif
|
2020-07-24 18:05:28 +03:00
|
|
|
|
2020-07-24 17:30:47 +03:00
|
|
|
def test_exif_equality(self):
|
2020-07-25 08:09:00 +03:00
|
|
|
# In 7.2.0, Exif rationals were changed to be read as
|
|
|
|
# TiffImagePlugin.IFDRational. This class had a bug in __eq__,
|
|
|
|
# breaking the self-equality of Exif data
|
2020-07-24 17:30:47 +03:00
|
|
|
exifs = []
|
|
|
|
for i in range(2):
|
|
|
|
with Image.open("Tests/images/exif-200dpcm.jpg") as im:
|
|
|
|
exifs.append(im._getexif())
|
|
|
|
assert exifs[0] == exifs[1]
|
2015-10-07 12:24:15 +03:00
|
|
|
|
2016-01-01 15:08:44 +03:00
|
|
|
def test_exif_rollback(self):
|
|
|
|
# rolling back exif support in 3.1 to pre-3.0 formatting.
|
|
|
|
# expected from 2.9, with b/u qualifiers switched for 3.2 compatibility
|
2016-02-05 01:57:13 +03:00
|
|
|
# this test passes on 2.9 and 3.1, but not 3.0
|
2019-06-13 18:54:11 +03:00
|
|
|
expected_exif = {
|
|
|
|
34867: 4294967295,
|
|
|
|
258: (24, 24, 24),
|
|
|
|
36867: "2099:09:29 10:10:10",
|
|
|
|
34853: {
|
|
|
|
0: b"\x00\x00\x00\x01",
|
2020-05-22 14:12:09 +03:00
|
|
|
2: 4294967295,
|
2019-06-13 18:54:11 +03:00
|
|
|
5: b"\x01",
|
|
|
|
30: 65535,
|
|
|
|
29: "1999:99:99 99:99:99",
|
|
|
|
},
|
|
|
|
296: 65535,
|
|
|
|
34665: 185,
|
|
|
|
41994: 65535,
|
|
|
|
514: 4294967295,
|
|
|
|
271: "Make",
|
|
|
|
272: "XXX-XXX",
|
|
|
|
305: "PIL",
|
2020-05-22 14:12:09 +03:00
|
|
|
42034: (1, 1, 1, 1),
|
2019-06-13 18:54:11 +03:00
|
|
|
42035: "LensMake",
|
|
|
|
34856: b"\xaa\xaa\xaa\xaa\xaa\xaa",
|
2020-05-22 14:12:09 +03:00
|
|
|
282: 4294967295,
|
|
|
|
33434: 4294967295,
|
2019-06-13 18:54:11 +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/exif_gps.jpg") as im:
|
|
|
|
exif = im._getexif()
|
2016-01-01 15:08:44 +03:00
|
|
|
|
|
|
|
for tag, value in expected_exif.items():
|
2020-02-22 16:06:21 +03:00
|
|
|
assert value == exif[tag]
|
2016-01-01 15:08:44 +03:00
|
|
|
|
2015-09-10 15:21:21 +03:00
|
|
|
def test_exif_gps_typeerror(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/exif_gps_typeerror.jpg") as im:
|
|
|
|
# Should not raise a TypeError
|
|
|
|
im._getexif()
|
2015-07-29 16:38:26 +03:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_progressive_compat(self):
|
2014-09-23 20:52:03 +04:00
|
|
|
im1 = self.roundtrip(hopper())
|
2020-02-22 16:06:21 +03:00
|
|
|
assert not im1.info.get("progressive")
|
|
|
|
assert not im1.info.get("progression")
|
2016-09-23 13:32:21 +03:00
|
|
|
|
|
|
|
im2 = self.roundtrip(hopper(), progressive=0)
|
|
|
|
im3 = self.roundtrip(hopper(), progression=0) # compatibility
|
2020-02-22 16:06:21 +03:00
|
|
|
assert not im2.info.get("progressive")
|
|
|
|
assert not im2.info.get("progression")
|
|
|
|
assert not im3.info.get("progressive")
|
|
|
|
assert not im3.info.get("progression")
|
2016-09-23 13:32:21 +03:00
|
|
|
|
2014-09-23 20:52:03 +04:00
|
|
|
im2 = self.roundtrip(hopper(), progressive=1)
|
|
|
|
im3 = self.roundtrip(hopper(), progression=1) # compatibility
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_equal(im1, im2)
|
|
|
|
assert_image_equal(im1, im3)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im2.info.get("progressive")
|
|
|
|
assert im2.info.get("progression")
|
|
|
|
assert im3.info.get("progressive")
|
|
|
|
assert im3.info.get("progression")
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
def test_quality(self):
|
2014-09-23 20:52:03 +04:00
|
|
|
im1 = self.roundtrip(hopper())
|
|
|
|
im2 = self.roundtrip(hopper(), quality=50)
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image(im1, im2.mode, im2.size)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im1.bytes >= im2.bytes
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2020-02-21 14:05:44 +03:00
|
|
|
im3 = self.roundtrip(hopper(), quality=0)
|
|
|
|
assert_image(im1, im3.mode, im3.size)
|
2020-03-02 17:02:19 +03:00
|
|
|
assert im2.bytes > im3.bytes
|
2020-02-21 14:05:44 +03:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_smooth(self):
|
2014-09-23 20:52:03 +04:00
|
|
|
im1 = self.roundtrip(hopper())
|
|
|
|
im2 = self.roundtrip(hopper(), smooth=100)
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image(im1, im2.mode, im2.size)
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
def test_subsampling(self):
|
|
|
|
def getsampling(im):
|
|
|
|
layer = im.layer
|
|
|
|
return layer[0][1:3] + layer[1][1:3] + layer[2][1:3]
|
2019-06-13 18:54:11 +03:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# experimental API
|
2014-09-23 20:52:03 +04:00
|
|
|
im = self.roundtrip(hopper(), subsampling=-1) # default
|
2020-02-22 16:06:21 +03:00
|
|
|
assert getsampling(im) == (2, 2, 1, 1, 1, 1)
|
2014-09-23 20:52:03 +04:00
|
|
|
im = self.roundtrip(hopper(), subsampling=0) # 4:4:4
|
2020-02-22 16:06:21 +03:00
|
|
|
assert getsampling(im) == (1, 1, 1, 1, 1, 1)
|
2014-09-23 20:52:03 +04:00
|
|
|
im = self.roundtrip(hopper(), subsampling=1) # 4:2:2
|
2020-02-22 16:06:21 +03:00
|
|
|
assert getsampling(im) == (2, 1, 1, 1, 1, 1)
|
2017-08-27 20:03:36 +03:00
|
|
|
im = self.roundtrip(hopper(), subsampling=2) # 4:2:0
|
2020-02-22 16:06:21 +03:00
|
|
|
assert getsampling(im) == (2, 2, 1, 1, 1, 1)
|
2014-09-23 20:52:03 +04:00
|
|
|
im = self.roundtrip(hopper(), subsampling=3) # default (undefined)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert getsampling(im) == (2, 2, 1, 1, 1, 1)
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2014-09-23 20:52:03 +04:00
|
|
|
im = self.roundtrip(hopper(), subsampling="4:4:4")
|
2020-02-22 16:06:21 +03:00
|
|
|
assert getsampling(im) == (1, 1, 1, 1, 1, 1)
|
2014-09-23 20:52:03 +04:00
|
|
|
im = self.roundtrip(hopper(), subsampling="4:2:2")
|
2020-02-22 16:06:21 +03:00
|
|
|
assert getsampling(im) == (2, 1, 1, 1, 1, 1)
|
2017-08-27 20:03:36 +03:00
|
|
|
im = self.roundtrip(hopper(), subsampling="4:2:0")
|
2020-02-22 16:06:21 +03:00
|
|
|
assert getsampling(im) == (2, 2, 1, 1, 1, 1)
|
2014-09-23 20:52:03 +04:00
|
|
|
im = self.roundtrip(hopper(), subsampling="4:1:1")
|
2020-02-22 16:06:21 +03:00
|
|
|
assert getsampling(im) == (2, 2, 1, 1, 1, 1)
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(TypeError):
|
|
|
|
self.roundtrip(hopper(), subsampling="1:1:1")
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
def test_exif(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/pil_sample_rgb.jpg") as im:
|
|
|
|
info = im._getexif()
|
2020-02-22 16:06:21 +03:00
|
|
|
assert info[305] == "Adobe Photoshop CS Macintosh"
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2022-12-06 11:30:53 +03:00
|
|
|
def test_get_child_images(self):
|
|
|
|
with Image.open("Tests/images/flower.jpg") as im:
|
|
|
|
ims = im.get_child_images()
|
|
|
|
|
|
|
|
assert len(ims) == 1
|
2023-01-02 11:54:12 +03:00
|
|
|
assert_image_similar_tofile(ims[0], "Tests/images/flower_thumbnail.png", 2.1)
|
2022-12-06 11:30:53 +03:00
|
|
|
|
2014-07-16 19:36:56 +04:00
|
|
|
def test_mp(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/pil_sample_rgb.jpg") as im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im._getmp() is None
|
2014-07-16 19:36:56 +04:00
|
|
|
|
2020-03-02 17:02:19 +03:00
|
|
|
def test_quality_keep(self, tmp_path):
|
2014-09-02 22:49:24 +04:00
|
|
|
# RGB
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open("Tests/images/hopper.jpg") as im:
|
2020-03-02 17:02:19 +03:00
|
|
|
f = str(tmp_path / "temp.jpg")
|
2019-11-25 23:03:23 +03:00
|
|
|
im.save(f, quality="keep")
|
2014-09-02 22:49:24 +04:00
|
|
|
# Grayscale
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open("Tests/images/hopper_gray.jpg") as im:
|
2020-03-02 17:02:19 +03:00
|
|
|
f = str(tmp_path / "temp.jpg")
|
2019-11-25 23:03:23 +03:00
|
|
|
im.save(f, quality="keep")
|
2014-09-02 22:49:24 +04:00
|
|
|
# CMYK
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open("Tests/images/pil_sample_cmyk.jpg") as im:
|
2020-03-02 17:02:19 +03:00
|
|
|
f = str(tmp_path / "temp.jpg")
|
2019-11-25 23:03:23 +03:00
|
|
|
im.save(f, quality="keep")
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
def test_junk_jpeg_header(self):
|
|
|
|
# https://github.com/python-pillow/Pillow/issues/630
|
|
|
|
filename = "Tests/images/junk_jpeg_header.jpg"
|
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(filename):
|
|
|
|
pass
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2016-06-22 23:36:23 +03:00
|
|
|
def test_ff00_jpeg_header(self):
|
|
|
|
filename = "Tests/images/jpeg_ff00_header.jpg"
|
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(filename):
|
|
|
|
pass
|
2016-06-22 23:36:23 +03:00
|
|
|
|
2021-04-10 17:58:01 +03:00
|
|
|
@mark_if_feature_version(
|
|
|
|
pytest.mark.valgrind_known_error, "libjpeg_turbo", "2.0", reason="Known Failing"
|
|
|
|
)
|
2018-03-08 08:31:51 +03:00
|
|
|
def test_truncated_jpeg_should_read_all_the_data(self):
|
|
|
|
filename = "Tests/images/truncated_jpeg.jpg"
|
|
|
|
ImageFile.LOAD_TRUNCATED_IMAGES = True
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(filename) as im:
|
|
|
|
im.load()
|
|
|
|
ImageFile.LOAD_TRUNCATED_IMAGES = False
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.getbbox() is not None
|
2018-03-08 08:31:51 +03:00
|
|
|
|
2020-04-07 09:58:21 +03:00
|
|
|
def test_truncated_jpeg_throws_oserror(self):
|
2018-03-08 08:31:51 +03:00
|
|
|
filename = "Tests/images/truncated_jpeg.jpg"
|
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(filename) as im:
|
2020-04-07 09:58:21 +03:00
|
|
|
with pytest.raises(OSError):
|
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
|
|
|
im.load()
|
2018-03-08 08:31:51 +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
|
|
|
# Test that the error is raised if loaded a second time
|
2020-04-07 09:58:21 +03:00
|
|
|
with pytest.raises(OSError):
|
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
|
|
|
im.load()
|
2019-07-13 01:37:17 +03:00
|
|
|
|
2021-04-10 17:58:01 +03:00
|
|
|
@mark_if_feature_version(
|
|
|
|
pytest.mark.valgrind_known_error, "libjpeg_turbo", "2.0", reason="Known Failing"
|
|
|
|
)
|
2020-03-02 17:02:19 +03:00
|
|
|
def test_qtables(self, tmp_path):
|
|
|
|
def _n_qtables_helper(n, test_file):
|
|
|
|
with Image.open(test_file) as im:
|
|
|
|
f = str(tmp_path / "temp.jpg")
|
|
|
|
im.save(f, qtables=[[n] * 64] * n)
|
|
|
|
with Image.open(f) as im:
|
|
|
|
assert len(im.quantization) == n
|
|
|
|
reloaded = self.roundtrip(im, qtables="keep")
|
|
|
|
assert im.quantization == reloaded.quantization
|
2020-10-17 21:20:59 +03:00
|
|
|
assert max(reloaded.quantization[0]) <= 255
|
2016-05-23 03:53:26 +03:00
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open("Tests/images/hopper.jpg") as im:
|
|
|
|
qtables = im.quantization
|
|
|
|
reloaded = self.roundtrip(im, qtables=qtables, subsampling=0)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.quantization == reloaded.quantization
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_similar(im, self.roundtrip(im, qtables="web_low"), 30)
|
|
|
|
assert_image_similar(im, self.roundtrip(im, qtables="web_high"), 30)
|
|
|
|
assert_image_similar(im, self.roundtrip(im, qtables="keep"), 30)
|
2019-11-25 23:03:23 +03:00
|
|
|
|
|
|
|
# valid bounds for baseline qtable
|
|
|
|
bounds_qtable = [int(s) for s in ("255 1 " * 32).split(None)]
|
2020-10-17 21:20:59 +03:00
|
|
|
im2 = self.roundtrip(im, qtables=[bounds_qtable])
|
2021-06-29 12:41:00 +03:00
|
|
|
assert im2.quantization == {0: bounds_qtable}
|
2019-11-25 23:03:23 +03:00
|
|
|
|
|
|
|
# values from wizard.txt in jpeg9-a src package.
|
|
|
|
standard_l_qtable = [
|
|
|
|
int(s)
|
|
|
|
for s in """
|
|
|
|
16 11 10 16 24 40 51 61
|
|
|
|
12 12 14 19 26 58 60 55
|
|
|
|
14 13 16 24 40 57 69 56
|
|
|
|
14 17 22 29 51 87 80 62
|
|
|
|
18 22 37 56 68 109 103 77
|
|
|
|
24 35 55 64 81 104 113 92
|
|
|
|
49 64 78 87 103 121 120 101
|
|
|
|
72 92 95 98 112 100 103 99
|
|
|
|
""".split(
|
|
|
|
None
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
standard_chrominance_qtable = [
|
|
|
|
int(s)
|
|
|
|
for s in """
|
|
|
|
17 18 24 47 99 99 99 99
|
|
|
|
18 21 26 66 99 99 99 99
|
|
|
|
24 26 56 99 99 99 99 99
|
|
|
|
47 66 99 99 99 99 99 99
|
|
|
|
99 99 99 99 99 99 99 99
|
|
|
|
99 99 99 99 99 99 99 99
|
|
|
|
99 99 99 99 99 99 99 99
|
|
|
|
99 99 99 99 99 99 99 99
|
|
|
|
""".split(
|
|
|
|
None
|
|
|
|
)
|
|
|
|
]
|
|
|
|
# list of qtable lists
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_similar(
|
2019-11-25 23:03:23 +03:00
|
|
|
im,
|
|
|
|
self.roundtrip(
|
|
|
|
im, qtables=[standard_l_qtable, standard_chrominance_qtable]
|
|
|
|
),
|
|
|
|
30,
|
2019-06-13 18:54:11 +03:00
|
|
|
)
|
2019-11-25 23:03:23 +03:00
|
|
|
|
|
|
|
# tuple of qtable lists
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_similar(
|
2019-11-25 23:03:23 +03:00
|
|
|
im,
|
|
|
|
self.roundtrip(
|
|
|
|
im, qtables=(standard_l_qtable, standard_chrominance_qtable)
|
|
|
|
),
|
|
|
|
30,
|
|
|
|
)
|
|
|
|
|
|
|
|
# dict of qtable lists
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_similar(
|
2019-11-25 23:03:23 +03:00
|
|
|
im,
|
|
|
|
self.roundtrip(
|
|
|
|
im, qtables={0: standard_l_qtable, 1: standard_chrominance_qtable}
|
|
|
|
),
|
|
|
|
30,
|
2019-06-13 18:54:11 +03:00
|
|
|
)
|
2019-11-25 23:03:23 +03:00
|
|
|
|
2020-03-02 17:02:19 +03:00
|
|
|
_n_qtables_helper(1, "Tests/images/hopper_gray.jpg")
|
|
|
|
_n_qtables_helper(1, "Tests/images/pil_sample_rgb.jpg")
|
|
|
|
_n_qtables_helper(2, "Tests/images/pil_sample_rgb.jpg")
|
|
|
|
_n_qtables_helper(3, "Tests/images/pil_sample_rgb.jpg")
|
|
|
|
_n_qtables_helper(1, "Tests/images/pil_sample_cmyk.jpg")
|
|
|
|
_n_qtables_helper(2, "Tests/images/pil_sample_cmyk.jpg")
|
|
|
|
_n_qtables_helper(3, "Tests/images/pil_sample_cmyk.jpg")
|
|
|
|
_n_qtables_helper(4, "Tests/images/pil_sample_cmyk.jpg")
|
2019-11-25 23:03:23 +03:00
|
|
|
|
|
|
|
# not a sequence
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
self.roundtrip(im, qtables="a")
|
2019-11-25 23:03:23 +03:00
|
|
|
# sequence wrong length
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
self.roundtrip(im, qtables=[])
|
2019-11-25 23:03:23 +03:00
|
|
|
# sequence wrong length
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
self.roundtrip(im, qtables=[1, 2, 3, 4, 5])
|
2019-11-25 23:03:23 +03:00
|
|
|
|
|
|
|
# qtable entry not a sequence
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
self.roundtrip(im, qtables=[1])
|
2019-11-25 23:03:23 +03:00
|
|
|
# qtable entry has wrong number of items
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
self.roundtrip(im, qtables=[[1, 2, 3, 4]])
|
2014-09-25 01:15:17 +04:00
|
|
|
|
2020-10-10 19:53:49 +03:00
|
|
|
def test_load_16bit_qtables(self):
|
|
|
|
with Image.open("Tests/images/hopper_16bit_qtables.jpg") as im:
|
|
|
|
assert len(im.quantization) == 2
|
|
|
|
assert len(im.quantization[0]) == 64
|
|
|
|
assert max(im.quantization[0]) > 255
|
|
|
|
|
|
|
|
def test_save_multiple_16bit_qtables(self):
|
|
|
|
with Image.open("Tests/images/hopper_16bit_qtables.jpg") as im:
|
2020-10-10 21:01:28 +03:00
|
|
|
im2 = self.roundtrip(im, qtables="keep")
|
2020-10-10 19:53:49 +03:00
|
|
|
assert im.quantization == im2.quantization
|
|
|
|
|
|
|
|
def test_save_single_16bit_qtable(self):
|
|
|
|
with Image.open("Tests/images/hopper_16bit_qtables.jpg") as im:
|
2020-10-10 21:01:28 +03:00
|
|
|
im2 = self.roundtrip(im, qtables={0: im.quantization[0]})
|
2020-10-10 19:53:49 +03:00
|
|
|
assert len(im2.quantization) == 1
|
|
|
|
assert im2.quantization[0] == im.quantization[0]
|
|
|
|
|
|
|
|
def test_save_low_quality_baseline_qtables(self):
|
|
|
|
with Image.open(TEST_FILE) as im:
|
|
|
|
im2 = self.roundtrip(im, quality=10)
|
|
|
|
assert len(im2.quantization) == 2
|
|
|
|
assert max(im2.quantization[0]) <= 255
|
|
|
|
assert max(im2.quantization[1]) <= 255
|
|
|
|
|
2020-10-17 21:20:59 +03:00
|
|
|
def test_convert_dict_qtables_deprecation(self):
|
|
|
|
with pytest.warns(DeprecationWarning):
|
|
|
|
qtable = {0: [1, 2, 3, 4]}
|
|
|
|
qtable2 = JpegImagePlugin.convert_dict_qtables(qtable)
|
|
|
|
assert qtable == qtable2
|
|
|
|
|
2020-03-02 17:02:19 +03:00
|
|
|
@pytest.mark.skipif(not djpeg_available(), reason="djpeg not available")
|
2014-06-27 07:37:49 +04:00
|
|
|
def test_load_djpeg(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(TEST_FILE) as img:
|
|
|
|
img.load_djpeg()
|
2021-02-21 14:22:29 +03:00
|
|
|
assert_image_similar_tofile(img, TEST_FILE, 5)
|
2014-06-27 07:37:49 +04:00
|
|
|
|
2020-03-02 17:02:19 +03:00
|
|
|
@pytest.mark.skipif(not cjpeg_available(), reason="cjpeg not available")
|
|
|
|
def test_save_cjpeg(self, tmp_path):
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(TEST_FILE) as img:
|
2020-03-02 17:02:19 +03:00
|
|
|
tempfile = str(tmp_path / "temp.jpg")
|
2019-11-25 23:03:23 +03:00
|
|
|
JpegImagePlugin._save_cjpeg(img, 0, tempfile)
|
|
|
|
# Default save quality is 75%, so a tiny bit of difference is alright
|
2021-02-21 14:22:29 +03:00
|
|
|
assert_image_similar_tofile(img, tempfile, 17)
|
2014-06-27 07:37:49 +04:00
|
|
|
|
2014-08-27 11:46:34 +04:00
|
|
|
def test_no_duplicate_0x1001_tag(self):
|
|
|
|
# Arrange
|
2017-05-28 19:34:41 +03:00
|
|
|
tag_ids = {v: k for k, v in ExifTags.TAGS.items()}
|
2014-08-27 11:46:34 +04:00
|
|
|
|
|
|
|
# Assert
|
2020-02-22 16:06:21 +03:00
|
|
|
assert tag_ids["RelatedImageWidth"] == 0x1001
|
|
|
|
assert tag_ids["RelatedImageLength"] == 0x1002
|
2014-08-27 11:46:34 +04:00
|
|
|
|
2020-03-02 17:02:19 +03:00
|
|
|
def test_MAXBLOCK_scaling(self, tmp_path):
|
2016-12-03 17:45:05 +03:00
|
|
|
im = self.gen_random_image((512, 512))
|
2020-03-02 17:02:19 +03:00
|
|
|
f = str(tmp_path / "temp.jpeg")
|
2015-01-18 21:56:29 +03:00
|
|
|
im.save(f, quality=100, optimize=True)
|
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(f) as reloaded:
|
|
|
|
# none of these should crash
|
|
|
|
reloaded.save(f, quality="keep")
|
|
|
|
reloaded.save(f, quality="keep", progressive=True)
|
|
|
|
reloaded.save(f, quality="keep", optimize=True)
|
2015-01-18 21:56:29 +03:00
|
|
|
|
2015-07-19 15:56:04 +03:00
|
|
|
def test_bad_mpo_header(self):
|
2021-08-12 14:50:09 +03:00
|
|
|
"""Treat unknown MPO as JPEG"""
|
2015-07-19 15:56:04 +03:00
|
|
|
# Arrange
|
|
|
|
|
|
|
|
# Act
|
|
|
|
# Shouldn't raise error
|
2015-09-15 02:52:02 +03:00
|
|
|
fn = "Tests/images/sugarshack_bad_mpo_header.jpg"
|
2020-02-03 12:11:32 +03:00
|
|
|
with pytest.warns(UserWarning, Image.open, fn) as im:
|
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
|
|
|
# Assert
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.format == "JPEG"
|
2015-07-19 15:56:04 +03:00
|
|
|
|
2022-10-03 08:57:42 +03:00
|
|
|
@pytest.mark.parametrize("mode", ("1", "L", "RGB", "RGBX", "CMYK", "YCbCr"))
|
|
|
|
def test_save_correct_modes(self, mode):
|
2016-07-03 05:40:34 +03:00
|
|
|
out = BytesIO()
|
2022-10-03 08:57:42 +03:00
|
|
|
img = Image.new(mode, (20, 20))
|
|
|
|
img.save(out, "JPEG")
|
2016-07-03 05:40:34 +03:00
|
|
|
|
2022-10-03 08:57:42 +03:00
|
|
|
@pytest.mark.parametrize("mode", ("LA", "La", "RGBA", "RGBa", "P"))
|
|
|
|
def test_save_wrong_modes(self, mode):
|
2016-08-09 03:11:35 +03:00
|
|
|
# ref https://github.com/python-pillow/Pillow/issues/2005
|
|
|
|
out = BytesIO()
|
2022-10-03 08:57:42 +03:00
|
|
|
img = Image.new(mode, (20, 20))
|
|
|
|
with pytest.raises(OSError):
|
|
|
|
img.save(out, "JPEG")
|
2016-08-09 03:11:35 +03:00
|
|
|
|
2020-03-02 17:02:19 +03:00
|
|
|
def test_save_tiff_with_dpi(self, tmp_path):
|
2016-09-22 12:41:32 +03:00
|
|
|
# Arrange
|
2020-03-02 17:02:19 +03:00
|
|
|
outfile = str(tmp_path / "temp.tif")
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open("Tests/images/hopper.tif") as im:
|
|
|
|
# Act
|
|
|
|
im.save(outfile, "JPEG", dpi=im.info["dpi"])
|
2016-09-22 12:41:32 +03:00
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
# Assert
|
|
|
|
with Image.open(outfile) as reloaded:
|
|
|
|
reloaded.load()
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info["dpi"] == reloaded.info["dpi"]
|
2016-09-22 12:41:32 +03:00
|
|
|
|
2020-03-02 17:02:19 +03:00
|
|
|
def test_save_dpi_rounding(self, tmp_path):
|
|
|
|
outfile = str(tmp_path / "temp.jpg")
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open("Tests/images/hopper.jpg") as im:
|
|
|
|
im.save(outfile, dpi=(72.2, 72.2))
|
2019-03-30 07:03:57 +03:00
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(outfile) as reloaded:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert reloaded.info["dpi"] == (72, 72)
|
2019-03-30 07:03:57 +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
|
|
|
im.save(outfile, dpi=(72.8, 72.8))
|
2019-11-25 23:03:23 +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(outfile) as reloaded:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert reloaded.info["dpi"] == (73, 73)
|
2019-03-30 07:03:57 +03:00
|
|
|
|
2017-04-11 13:53:01 +03:00
|
|
|
def test_dpi_tuple_from_exif(self):
|
2017-03-14 12:26:11 +03:00
|
|
|
# Arrange
|
|
|
|
# This Photoshop CC 2017 image has DPI in EXIF not metadata
|
2017-04-11 13:53:01 +03:00
|
|
|
# EXIF XResolution is (2000000, 10000)
|
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/photoshop-200dpi.jpg") as im:
|
|
|
|
# Act / Assert
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info.get("dpi") == (200, 200)
|
2017-04-04 01:28:33 +03:00
|
|
|
|
2017-04-11 13:53:01 +03:00
|
|
|
def test_dpi_int_from_exif(self):
|
|
|
|
# Arrange
|
|
|
|
# This image has DPI in EXIF not metadata
|
|
|
|
# EXIF XResolution is 72
|
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/exif-72dpi-int.jpg") as im:
|
|
|
|
# Act / Assert
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info.get("dpi") == (72, 72)
|
2017-04-11 13:53:01 +03:00
|
|
|
|
2017-04-04 01:28:33 +03:00
|
|
|
def test_dpi_from_dpcm_exif(self):
|
|
|
|
# Arrange
|
|
|
|
# This is photoshop-200dpi.jpg with EXIF resolution unit set to cm:
|
|
|
|
# exiftool -exif:ResolutionUnit=cm photoshop-200dpi.jpg
|
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/exif-200dpcm.jpg") as im:
|
|
|
|
# Act / Assert
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info.get("dpi") == (508, 508)
|
2017-04-04 01:28:33 +03:00
|
|
|
|
2017-08-09 16:16:14 +03:00
|
|
|
def test_dpi_exif_zero_division(self):
|
|
|
|
# Arrange
|
|
|
|
# This is photoshop-200dpi.jpg with EXIF resolution set to 0/0:
|
|
|
|
# exiftool -XResolution=0/0 -YResolution=0/0 photoshop-200dpi.jpg
|
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/exif-dpi-zerodivision.jpg") as im:
|
|
|
|
# Act / Assert
|
|
|
|
# This should return the default, and not raise a ZeroDivisionError
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info.get("dpi") == (72, 72)
|
2017-08-09 16:16:14 +03:00
|
|
|
|
2021-07-26 16:13:01 +03:00
|
|
|
def test_dpi_exif_string(self):
|
|
|
|
# Arrange
|
|
|
|
# 0x011A tag in this exif contains string '300300\x02'
|
|
|
|
with Image.open("Tests/images/broken_exif_dpi.jpg") as im:
|
|
|
|
# Act / Assert
|
|
|
|
# This should return the default
|
|
|
|
assert im.info.get("dpi") == (72, 72)
|
|
|
|
|
2017-04-04 01:28:33 +03:00
|
|
|
def test_no_dpi_in_exif(self):
|
|
|
|
# Arrange
|
|
|
|
# This is photoshop-200dpi.jpg with resolution removed from EXIF:
|
|
|
|
# exiftool "-*resolution*"= photoshop-200dpi.jpg
|
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/no-dpi-in-exif.jpg") as im:
|
|
|
|
# Act / Assert
|
|
|
|
# "When the image resolution is unknown, 72 [dpi] is designated."
|
2022-04-28 01:26:57 +03:00
|
|
|
# https://exiv2.org/tags.html
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info.get("dpi") == (72, 72)
|
2017-03-14 12:26:11 +03:00
|
|
|
|
2017-07-18 11:06:54 +03:00
|
|
|
def test_invalid_exif(self):
|
|
|
|
# This is no-dpi-in-exif with the tiff header of the exif block
|
|
|
|
# hexedited from MM * to FF FF FF FF
|
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/invalid-exif.jpg") as im:
|
|
|
|
# This should return the default, and not a SyntaxError or
|
|
|
|
# OSError for unidentified image.
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info.get("dpi") == (72, 72)
|
2017-07-18 11:06:54 +03:00
|
|
|
|
2021-04-10 17:58:01 +03:00
|
|
|
@mark_if_feature_version(
|
|
|
|
pytest.mark.valgrind_known_error, "libjpeg_turbo", "2.0", reason="Known Failing"
|
|
|
|
)
|
2020-05-22 14:12:09 +03:00
|
|
|
def test_exif_x_resolution(self, tmp_path):
|
|
|
|
with Image.open("Tests/images/flower.jpg") as im:
|
|
|
|
exif = im.getexif()
|
|
|
|
assert exif[282] == 180
|
|
|
|
|
|
|
|
out = str(tmp_path / "out.jpg")
|
2022-02-21 05:49:01 +03:00
|
|
|
with warnings.catch_warnings():
|
2020-05-22 14:12:09 +03:00
|
|
|
im.save(out, exif=exif)
|
|
|
|
|
|
|
|
with Image.open(out) as reloaded:
|
|
|
|
assert reloaded.getexif()[282] == 180
|
|
|
|
|
2019-10-17 20:02:15 +03:00
|
|
|
def test_invalid_exif_x_resolution(self):
|
2019-10-28 19:48:37 +03:00
|
|
|
# When no x or y resolution is defined in EXIF
|
2020-01-27 02:59:20 +03:00
|
|
|
with Image.open("Tests/images/invalid-exif-without-x-resolution.jpg") as im:
|
|
|
|
# This should return the default, and not a ValueError or
|
|
|
|
# OSError for an unidentified image.
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info.get("dpi") == (72, 72)
|
2019-10-17 20:02:15 +03:00
|
|
|
|
2019-01-12 03:40:32 +03:00
|
|
|
def test_ifd_offset_exif(self):
|
|
|
|
# Arrange
|
|
|
|
# This image has been manually hexedited to have an IFD offset of 10,
|
|
|
|
# in contrast to normal 8
|
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/exif-ifd-offset.jpg") as im:
|
|
|
|
# Act / Assert
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im._getexif()[306] == "2017:03:13 23:03:09"
|
2019-01-12 03:40:32 +03:00
|
|
|
|
2021-04-10 17:58:01 +03:00
|
|
|
@mark_if_feature_version(
|
|
|
|
pytest.mark.valgrind_known_error, "libjpeg_turbo", "2.0", reason="Known Failing"
|
|
|
|
)
|
2019-03-06 02:28:45 +03:00
|
|
|
def test_photoshop(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/photoshop-200dpi.jpg") as im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info["photoshop"][0x03ED] == {
|
|
|
|
"XResolution": 200.0,
|
|
|
|
"DisplayedUnitsX": 1,
|
|
|
|
"YResolution": 200.0,
|
|
|
|
"DisplayedUnitsY": 1,
|
|
|
|
}
|
2019-03-06 02:28:45 +03:00
|
|
|
|
2019-11-30 02:08:32 +03:00
|
|
|
# Test that the image can still load, even with broken Photoshop data
|
|
|
|
# This image had the APP13 length hexedited to be smaller
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile(im, "Tests/images/photoshop-200dpi-broken.jpg")
|
2019-11-30 02:08:32 +03:00
|
|
|
|
2019-04-05 12:02:45 +03:00
|
|
|
# This image does not contain a Photoshop header string
|
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/app13.jpg") as im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert "photoshop" not in im.info
|
2019-04-05 12:02:45 +03:00
|
|
|
|
2020-01-20 17:25:40 +03:00
|
|
|
def test_photoshop_malformed_and_multiple(self):
|
|
|
|
with Image.open("Tests/images/app13-multiple.jpg") as im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert "photoshop" in im.info
|
|
|
|
assert 24 == len(im.info["photoshop"])
|
2020-01-20 17:25:40 +03:00
|
|
|
apps_13_lengths = [len(v) for k, v in im.applist if k == "APP13"]
|
2020-02-22 16:06:21 +03:00
|
|
|
assert [65504, 24] == apps_13_lengths
|
2020-01-20 17:25:40 +03:00
|
|
|
|
2021-04-15 14:49:51 +03:00
|
|
|
def test_adobe_transform(self):
|
|
|
|
with Image.open("Tests/images/pil_sample_rgb.jpg") as im:
|
|
|
|
assert im.info["adobe_transform"] == 1
|
|
|
|
|
|
|
|
with Image.open("Tests/images/pil_sample_cmyk.jpg") as im:
|
|
|
|
assert im.info["adobe_transform"] == 2
|
|
|
|
|
2021-04-20 13:53:50 +03:00
|
|
|
# This image has been manually hexedited
|
|
|
|
# so that the APP14 reports its length to be 11,
|
|
|
|
# leaving no room for "adobe_transform"
|
|
|
|
with Image.open("Tests/images/truncated_app14.jpg") as im:
|
|
|
|
assert "adobe" in im.info
|
|
|
|
assert "adobe_transform" not in im.info
|
|
|
|
|
2020-05-08 19:24:38 +03:00
|
|
|
def test_icc_after_SOF(self):
|
|
|
|
with Image.open("Tests/images/icc-after-SOF.jpg") as im:
|
|
|
|
assert im.info["icc_profile"] == b"profile"
|
|
|
|
|
2020-06-20 02:57:51 +03:00
|
|
|
def test_jpeg_magic_number(self):
|
2020-06-20 02:48:55 +03:00
|
|
|
size = 4097
|
2020-06-18 16:18:18 +03:00
|
|
|
buffer = BytesIO(b"\xFF" * size) # Many xFF bytes
|
|
|
|
buffer.max_pos = 0
|
|
|
|
orig_read = buffer.read
|
|
|
|
|
|
|
|
def read(n=-1):
|
|
|
|
res = orig_read(n)
|
|
|
|
buffer.max_pos = max(buffer.max_pos, buffer.tell())
|
|
|
|
return res
|
|
|
|
|
|
|
|
buffer.read = read
|
2020-06-20 02:51:48 +03:00
|
|
|
with pytest.raises(UnidentifiedImageError):
|
2021-02-11 13:43:54 +03:00
|
|
|
with Image.open(buffer):
|
|
|
|
pass
|
2020-06-18 16:18:18 +03:00
|
|
|
|
2020-06-20 02:48:55 +03:00
|
|
|
# Assert the entire file has not been read
|
|
|
|
assert 0 < buffer.max_pos < size
|
2020-06-18 16:18:18 +03:00
|
|
|
|
2021-04-01 14:28:37 +03:00
|
|
|
def test_getxmp(self):
|
|
|
|
with Image.open("Tests/images/xmp_test.jpg") as im:
|
2021-06-30 04:28:00 +03:00
|
|
|
if ElementTree is None:
|
2021-06-30 04:23:57 +03:00
|
|
|
with pytest.warns(UserWarning):
|
|
|
|
assert im.getxmp() == {}
|
2021-06-30 04:28:00 +03:00
|
|
|
else:
|
|
|
|
xmp = im.getxmp()
|
|
|
|
|
|
|
|
description = xmp["xmpmeta"]["RDF"]["Description"]
|
|
|
|
assert description["DerivedFrom"] == {
|
|
|
|
"documentID": "8367D410E636EA95B7DE7EBA1C43A412",
|
|
|
|
"originalDocumentID": "8367D410E636EA95B7DE7EBA1C43A412",
|
|
|
|
}
|
|
|
|
assert description["Look"]["Description"]["Group"]["Alt"]["li"] == {
|
|
|
|
"lang": "x-default",
|
|
|
|
"text": "Profiles",
|
|
|
|
}
|
|
|
|
assert description["ToneCurve"]["Seq"]["li"] == ["0, 0", "255, 255"]
|
|
|
|
|
|
|
|
# Attribute
|
|
|
|
assert description["Version"] == "10.4"
|
|
|
|
|
|
|
|
if ElementTree is not None:
|
|
|
|
with Image.open("Tests/images/hopper.jpg") as im:
|
|
|
|
assert im.getxmp() == {}
|
2021-06-12 06:57:14 +03:00
|
|
|
|
2022-01-01 13:04:32 +03:00
|
|
|
@pytest.mark.timeout(timeout=1)
|
|
|
|
def test_eof(self):
|
|
|
|
# Even though this decoder never says that it is finished
|
|
|
|
# the image should still end when there is no new data
|
|
|
|
class InfiniteMockPyDecoder(ImageFile.PyDecoder):
|
|
|
|
def decode(self, buffer):
|
|
|
|
return 0, 0
|
|
|
|
|
|
|
|
decoder = InfiniteMockPyDecoder(None)
|
|
|
|
|
|
|
|
def closure(mode, *args):
|
|
|
|
decoder.__init__(mode, *args)
|
|
|
|
return decoder
|
|
|
|
|
|
|
|
Image.register_decoder("INFINITE", closure)
|
|
|
|
|
|
|
|
with Image.open(TEST_FILE) as im:
|
|
|
|
im.tile = [
|
|
|
|
("INFINITE", (0, 0, 128, 128), 0, ("RGB", 0, 1)),
|
|
|
|
]
|
|
|
|
ImageFile.LOAD_TRUNCATED_IMAGES = True
|
|
|
|
im.load()
|
|
|
|
ImageFile.LOAD_TRUNCATED_IMAGES = False
|
|
|
|
|
2014-06-27 07:37:49 +04:00
|
|
|
|
2020-03-02 17:02:19 +03:00
|
|
|
@pytest.mark.skipif(not is_win32(), reason="Windows only")
|
2020-02-18 01:03:32 +03:00
|
|
|
@skip_unless_feature("jpg")
|
2020-03-02 17:02:19 +03:00
|
|
|
class TestFileCloseW32:
|
|
|
|
def test_fd_leak(self, tmp_path):
|
|
|
|
tmpfile = str(tmp_path / "temp.jpg")
|
2017-04-01 19:18:38 +03:00
|
|
|
|
|
|
|
with Image.open("Tests/images/hopper.jpg") as im:
|
|
|
|
im.save(tmpfile)
|
|
|
|
|
|
|
|
im = Image.open(tmpfile)
|
|
|
|
fp = im.fp
|
2020-02-22 16:06:21 +03:00
|
|
|
assert not fp.closed
|
2020-04-07 09:58:21 +03:00
|
|
|
with pytest.raises(OSError):
|
2020-02-22 16:06:21 +03:00
|
|
|
os.remove(tmpfile)
|
2017-04-01 19:18:38 +03:00
|
|
|
im.load()
|
2020-02-22 16:06:21 +03:00
|
|
|
assert fp.closed
|
2017-04-11 13:53:01 +03:00
|
|
|
# this should not fail, as load should have closed the file.
|
2017-04-01 19:18:38 +03:00
|
|
|
os.remove(tmpfile)
|