2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
2024-01-20 14:23:03 +03:00
|
|
|
|
2022-02-21 05:49:01 +03:00
|
|
|
import warnings
|
2014-07-16 19:36:56 +04:00
|
|
|
from io import BytesIO
|
2024-07-08 13:09:45 +03:00
|
|
|
from typing import Any
|
2019-07-06 23:40:53 +03:00
|
|
|
|
2020-02-03 12:11:32 +03:00
|
|
|
import pytest
|
2020-08-07 13:28:33 +03:00
|
|
|
|
2024-07-08 13:09:45 +03:00
|
|
|
from PIL import Image, ImageFile, MpoImagePlugin
|
2014-07-16 19:36:56 +04:00
|
|
|
|
2022-07-16 13:02:58 +03:00
|
|
|
from .helper import (
|
|
|
|
assert_image_equal,
|
|
|
|
assert_image_similar,
|
|
|
|
is_pypy,
|
|
|
|
skip_unless_feature,
|
|
|
|
)
|
2014-07-16 19:36:56 +04:00
|
|
|
|
|
|
|
test_files = ["Tests/images/sugarshack.mpo", "Tests/images/frozenpond.mpo"]
|
|
|
|
|
2020-02-18 01:03:32 +03:00
|
|
|
pytestmark = skip_unless_feature("jpg")
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
|
2024-07-08 13:09:45 +03:00
|
|
|
def roundtrip(im: Image.Image, **options: Any) -> ImageFile.ImageFile:
|
2020-02-12 19:29:19 +03:00
|
|
|
out = BytesIO()
|
|
|
|
im.save(out, "MPO", **options)
|
|
|
|
out.seek(0)
|
2024-07-08 13:09:45 +03:00
|
|
|
return Image.open(out)
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
|
2022-08-23 14:41:32 +03:00
|
|
|
@pytest.mark.parametrize("test_file", test_files)
|
2024-01-31 13:55:32 +03:00
|
|
|
def test_sanity(test_file: str) -> None:
|
2022-08-23 14:41:32 +03:00
|
|
|
with Image.open(test_file) as im:
|
|
|
|
im.load()
|
|
|
|
assert im.mode == "RGB"
|
|
|
|
assert im.size == (640, 480)
|
|
|
|
assert im.format == "MPO"
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(is_pypy(), reason="Requires CPython")
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_unclosed_file() -> None:
|
|
|
|
def open() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
im = Image.open(test_files[0])
|
|
|
|
im.load()
|
2023-03-02 23:50:52 +03:00
|
|
|
|
|
|
|
with pytest.warns(ResourceWarning):
|
|
|
|
open()
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_closed_file() -> None:
|
2022-02-21 05:49:01 +03:00
|
|
|
with warnings.catch_warnings():
|
2020-02-12 19:29:19 +03:00
|
|
|
im = Image.open(test_files[0])
|
|
|
|
im.load()
|
|
|
|
im.close()
|
|
|
|
|
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
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_seek_after_close() -> None:
|
2022-04-17 05:14:53 +03:00
|
|
|
im = Image.open(test_files[0])
|
|
|
|
im.close()
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
im.seek(1)
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_context_manager() -> None:
|
2022-02-21 05:49:01 +03:00
|
|
|
with warnings.catch_warnings():
|
2020-02-12 19:29:19 +03:00
|
|
|
with Image.open(test_files[0]) 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
|
|
|
im.load()
|
2019-01-18 08:08:46 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
|
2022-08-23 14:41:32 +03:00
|
|
|
@pytest.mark.parametrize("test_file", test_files)
|
2024-01-31 13:55:32 +03:00
|
|
|
def test_app(test_file: str) -> None:
|
2022-08-23 14:41:32 +03:00
|
|
|
# Test APP/COM reader (@PIL135)
|
|
|
|
with Image.open(test_file) as im:
|
|
|
|
assert im.applist[0][0] == "APP1"
|
|
|
|
assert im.applist[1][0] == "APP2"
|
|
|
|
assert (
|
|
|
|
im.applist[1][1][:16] == b"MPF\x00MM\x00*\x00\x00\x00\x08\x00\x03\xb0\x00"
|
|
|
|
)
|
|
|
|
assert len(im.applist) == 2
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
|
2022-08-23 14:41:32 +03:00
|
|
|
@pytest.mark.parametrize("test_file", test_files)
|
2024-01-31 13:55:32 +03:00
|
|
|
def test_exif(test_file: str) -> None:
|
2022-12-22 07:31:36 +03:00
|
|
|
with Image.open(test_file) as im_original:
|
|
|
|
im_reloaded = roundtrip(im_original, save_all=True, exif=im_original.getexif())
|
|
|
|
|
|
|
|
for im in (im_original, im_reloaded):
|
2024-07-20 12:07:42 +03:00
|
|
|
assert isinstance(im, MpoImagePlugin.MpoImageFile)
|
2022-08-23 14:41:32 +03:00
|
|
|
info = im._getexif()
|
2024-07-20 12:07:42 +03:00
|
|
|
assert info is not None
|
2022-08-23 14:41:32 +03:00
|
|
|
assert info[272] == "Nintendo 3DS"
|
|
|
|
assert info[296] == 2
|
|
|
|
assert info[34665] == 188
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_frame_size() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# This image has been hexedited to contain a different size
|
2024-03-16 10:40:16 +03:00
|
|
|
# in the SOF marker of the second frame
|
2020-02-12 19:29:19 +03:00
|
|
|
with Image.open("Tests/images/sugarshack_frame_size.mpo") as im:
|
|
|
|
assert im.size == (640, 480)
|
|
|
|
|
|
|
|
im.seek(1)
|
|
|
|
assert im.size == (680, 480)
|
|
|
|
|
2022-03-01 11:10:10 +03:00
|
|
|
im.seek(0)
|
|
|
|
assert im.size == (640, 480)
|
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_ignore_frame_size() -> None:
|
2021-01-01 04:45:02 +03:00
|
|
|
# Ignore the different size of the second frame
|
|
|
|
# since this is not a "Large Thumbnail" image
|
|
|
|
with Image.open("Tests/images/ignore_frame_size.mpo") as im:
|
|
|
|
assert im.size == (64, 64)
|
|
|
|
|
|
|
|
im.seek(1)
|
2021-01-01 05:00:01 +03:00
|
|
|
assert (
|
|
|
|
im.mpinfo[0xB002][1]["Attribute"]["MPType"]
|
|
|
|
== "Multi-Frame Image: (Disparity)"
|
|
|
|
)
|
2021-01-01 04:45:02 +03:00
|
|
|
assert im.size == (64, 64)
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_parallax() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Nintendo
|
|
|
|
with Image.open("Tests/images/sugarshack.mpo") as im:
|
|
|
|
exif = im.getexif()
|
|
|
|
assert exif.get_ifd(0x927C)[0x1101]["Parallax"] == -44.798187255859375
|
2019-03-28 13:11:09 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Fujifilm
|
|
|
|
with Image.open("Tests/images/fujifilm.mpo") as im:
|
|
|
|
im.seek(1)
|
|
|
|
exif = im.getexif()
|
|
|
|
assert exif.get_ifd(0x927C)[0xB211] == -3.125
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_reload_exif_after_seek() -> None:
|
2022-05-27 00:54:54 +03:00
|
|
|
with Image.open("Tests/images/sugarshack.mpo") as im:
|
|
|
|
exif = im.getexif()
|
|
|
|
del exif[296]
|
|
|
|
|
|
|
|
im.seek(1)
|
|
|
|
assert 296 in exif
|
|
|
|
|
|
|
|
|
2022-08-23 14:41:32 +03:00
|
|
|
@pytest.mark.parametrize("test_file", test_files)
|
2024-01-31 13:55:32 +03:00
|
|
|
def test_mp(test_file: str) -> None:
|
2022-08-23 14:41:32 +03:00
|
|
|
with Image.open(test_file) as im:
|
|
|
|
mpinfo = im._getmp()
|
|
|
|
assert mpinfo[45056] == b"0100"
|
|
|
|
assert mpinfo[45057] == 2
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_mp_offset() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# This image has been manually hexedited to have an IFD offset of 10
|
|
|
|
# in APP2 data, in contrast to normal 8
|
|
|
|
with Image.open("Tests/images/sugarshack_ifd_offset.mpo") as im:
|
|
|
|
mpinfo = im._getmp()
|
|
|
|
assert mpinfo[45056] == b"0100"
|
|
|
|
assert mpinfo[45057] == 2
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_mp_no_data() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# This image has been manually hexedited to have the second frame
|
|
|
|
# beyond the end of the file
|
|
|
|
with Image.open("Tests/images/sugarshack_no_data.mpo") as im:
|
|
|
|
with pytest.raises(ValueError):
|
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.seek(1)
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
|
2022-08-23 14:41:32 +03:00
|
|
|
@pytest.mark.parametrize("test_file", test_files)
|
2024-01-31 13:55:32 +03:00
|
|
|
def test_mp_attribute(test_file: str) -> None:
|
2022-08-23 14:41:32 +03:00
|
|
|
with Image.open(test_file) as im:
|
|
|
|
mpinfo = im._getmp()
|
2023-01-07 21:45:55 +03:00
|
|
|
for frame_number, mpentry in enumerate(mpinfo[0xB002]):
|
2022-08-23 14:41:32 +03:00
|
|
|
mpattr = mpentry["Attribute"]
|
|
|
|
if frame_number:
|
|
|
|
assert not mpattr["RepresentativeImageFlag"]
|
|
|
|
else:
|
|
|
|
assert mpattr["RepresentativeImageFlag"]
|
|
|
|
assert not mpattr["DependentParentImageFlag"]
|
|
|
|
assert not mpattr["DependentChildImageFlag"]
|
|
|
|
assert mpattr["ImageDataFormat"] == "JPEG"
|
|
|
|
assert mpattr["MPType"] == "Multi-Frame Image: (Disparity)"
|
|
|
|
assert mpattr["Reserved"] == 0
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("test_file", test_files)
|
2024-01-31 13:55:32 +03:00
|
|
|
def test_seek(test_file: str) -> None:
|
2022-08-23 14:41:32 +03:00
|
|
|
with Image.open(test_file) as im:
|
|
|
|
assert im.tell() == 0
|
|
|
|
# prior to first image raises an error, both blatant and borderline
|
|
|
|
with pytest.raises(EOFError):
|
|
|
|
im.seek(-1)
|
|
|
|
with pytest.raises(EOFError):
|
|
|
|
im.seek(-523)
|
|
|
|
# after the final image raises an error,
|
|
|
|
# both blatant and borderline
|
|
|
|
with pytest.raises(EOFError):
|
|
|
|
im.seek(2)
|
|
|
|
with pytest.raises(EOFError):
|
|
|
|
im.seek(523)
|
|
|
|
# bad calls shouldn't change the frame
|
|
|
|
assert im.tell() == 0
|
|
|
|
# this one will work
|
|
|
|
im.seek(1)
|
|
|
|
assert im.tell() == 1
|
|
|
|
# and this one, too
|
|
|
|
im.seek(0)
|
|
|
|
assert im.tell() == 0
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_n_frames() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
with Image.open("Tests/images/sugarshack.mpo") as im:
|
|
|
|
assert im.n_frames == 2
|
|
|
|
assert im.is_animated
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_eoferror() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
with Image.open("Tests/images/sugarshack.mpo") as im:
|
|
|
|
n_frames = im.n_frames
|
|
|
|
|
|
|
|
# Test seeking past the last frame
|
|
|
|
with pytest.raises(EOFError):
|
|
|
|
im.seek(n_frames)
|
|
|
|
assert im.tell() < n_frames
|
|
|
|
|
|
|
|
# Test that seeking to the last frame does not raise an error
|
|
|
|
im.seek(n_frames - 1)
|
|
|
|
|
|
|
|
|
2024-07-08 13:09:45 +03:00
|
|
|
def test_adopt_jpeg() -> None:
|
|
|
|
with Image.open("Tests/images/hopper.jpg") as im:
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
MpoImagePlugin.MpoImageFile.adopt(im)
|
|
|
|
|
|
|
|
|
2024-05-13 14:51:16 +03:00
|
|
|
def test_ultra_hdr() -> None:
|
|
|
|
with Image.open("Tests/images/ultrahdr.jpg") as im:
|
|
|
|
assert im.format == "JPEG"
|
|
|
|
|
|
|
|
|
2022-08-23 14:41:32 +03:00
|
|
|
@pytest.mark.parametrize("test_file", test_files)
|
2024-01-31 13:55:32 +03:00
|
|
|
def test_image_grab(test_file: str) -> None:
|
2022-08-23 14:41:32 +03:00
|
|
|
with Image.open(test_file) as im:
|
|
|
|
assert im.tell() == 0
|
|
|
|
im0 = im.tobytes()
|
|
|
|
im.seek(1)
|
|
|
|
assert im.tell() == 1
|
|
|
|
im1 = im.tobytes()
|
|
|
|
im.seek(0)
|
|
|
|
assert im.tell() == 0
|
|
|
|
im02 = im.tobytes()
|
|
|
|
assert im0 == im02
|
|
|
|
assert im0 != im1
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("test_file", test_files)
|
2024-01-31 13:55:32 +03:00
|
|
|
def test_save(test_file: str) -> None:
|
2022-08-23 14:41:32 +03:00
|
|
|
with Image.open(test_file) as im:
|
|
|
|
assert im.tell() == 0
|
|
|
|
jpg0 = roundtrip(im)
|
|
|
|
assert_image_similar(im, jpg0, 30)
|
|
|
|
im.seek(1)
|
|
|
|
assert im.tell() == 1
|
|
|
|
jpg1 = roundtrip(im)
|
|
|
|
assert_image_similar(im, jpg1, 30)
|
2022-07-16 13:02:58 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_save_all() -> None:
|
2022-07-16 13:02:58 +03:00
|
|
|
for test_file in test_files:
|
|
|
|
with Image.open(test_file) as im:
|
|
|
|
im_reloaded = roundtrip(im, save_all=True)
|
|
|
|
|
|
|
|
im.seek(0)
|
|
|
|
assert_image_similar(im, im_reloaded, 30)
|
|
|
|
|
|
|
|
im.seek(1)
|
|
|
|
im_reloaded.seek(1)
|
|
|
|
assert_image_similar(im, im_reloaded, 30)
|
|
|
|
|
|
|
|
im = Image.new("RGB", (1, 1))
|
|
|
|
im2 = Image.new("RGB", (1, 1), "#f00")
|
|
|
|
im_reloaded = roundtrip(im, save_all=True, append_images=[im2])
|
|
|
|
|
|
|
|
assert_image_equal(im, im_reloaded)
|
2024-07-08 13:09:45 +03:00
|
|
|
assert isinstance(im_reloaded, MpoImagePlugin.MpoImageFile)
|
|
|
|
assert im_reloaded.mpinfo is not None
|
2022-11-13 00:00:20 +03:00
|
|
|
assert im_reloaded.mpinfo[45056] == b"0100"
|
2022-07-16 13:02:58 +03:00
|
|
|
|
|
|
|
im_reloaded.seek(1)
|
|
|
|
assert_image_similar(im2, im_reloaded, 1)
|
|
|
|
|
|
|
|
# Test that a single frame image will not be saved as an MPO
|
|
|
|
jpg = roundtrip(im, save_all=True)
|
|
|
|
assert "mp" not in jpg.info
|