2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
2024-01-20 14:23:03 +03:00
|
|
|
|
2019-12-26 05:10:39 +03:00
|
|
|
import io
|
2023-11-17 00:37:46 +03:00
|
|
|
import logging
|
2019-07-06 23:40:53 +03:00
|
|
|
import os
|
|
|
|
import shutil
|
2021-12-29 16:11:20 +03:00
|
|
|
import sys
|
2019-10-07 16:28:36 +03:00
|
|
|
import tempfile
|
2022-02-21 05:49:01 +03:00
|
|
|
import warnings
|
2024-01-27 15:06:06 +03:00
|
|
|
from pathlib import Path
|
2024-06-26 12:31:40 +03:00
|
|
|
from types import ModuleType
|
2024-06-23 23:59:00 +03:00
|
|
|
from typing import IO, Any
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-02-03 12:11:32 +03:00
|
|
|
import pytest
|
2020-08-07 13:28:33 +03:00
|
|
|
|
2022-12-05 01:09:00 +03:00
|
|
|
from PIL import (
|
|
|
|
ExifTags,
|
|
|
|
Image,
|
|
|
|
ImageDraw,
|
2024-02-15 12:20:42 +03:00
|
|
|
ImageFile,
|
2022-12-05 01:09:00 +03:00
|
|
|
ImagePalette,
|
|
|
|
UnidentifiedImageError,
|
|
|
|
features,
|
|
|
|
)
|
2019-07-06 23:40:53 +03:00
|
|
|
|
2020-01-30 17:56:07 +03:00
|
|
|
from .helper import (
|
|
|
|
assert_image_equal,
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile,
|
2024-06-01 14:31:53 +03:00
|
|
|
assert_image_similar,
|
2021-02-21 14:22:29 +03:00
|
|
|
assert_image_similar_tofile,
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_not_all_same,
|
|
|
|
hopper,
|
2024-04-20 17:19:20 +03:00
|
|
|
is_big_endian,
|
2020-01-30 17:56:07 +03:00
|
|
|
is_win32,
|
2021-04-10 00:33:21 +03:00
|
|
|
mark_if_feature_version,
|
2021-04-10 17:58:01 +03:00
|
|
|
skip_unless_feature,
|
2020-01-30 17:56:07 +03:00
|
|
|
)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2024-06-26 12:31:40 +03:00
|
|
|
ElementTree: ModuleType | None
|
|
|
|
try:
|
|
|
|
from defusedxml import ElementTree
|
|
|
|
except ImportError:
|
|
|
|
ElementTree = None
|
|
|
|
|
2024-08-19 15:55:07 +03:00
|
|
|
PrettyPrinter: type | None
|
|
|
|
try:
|
|
|
|
from IPython.lib.pretty import PrettyPrinter
|
|
|
|
except ImportError:
|
|
|
|
PrettyPrinter = None
|
|
|
|
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2024-04-25 16:00:14 +03:00
|
|
|
# Deprecation helper
|
|
|
|
def helper_image_new(mode: str, size: tuple[int, int]) -> Image.Image:
|
|
|
|
if mode.startswith("BGR;"):
|
|
|
|
with pytest.warns(DeprecationWarning):
|
|
|
|
return Image.new(mode, size)
|
|
|
|
else:
|
|
|
|
return Image.new(mode, size)
|
|
|
|
|
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
class TestImage:
|
2024-05-12 14:20:46 +03:00
|
|
|
@pytest.mark.parametrize("mode", Image.MODES + ["BGR;15", "BGR;16", "BGR;24"])
|
2024-02-12 13:06:17 +03:00
|
|
|
def test_image_modes_success(self, mode: str) -> None:
|
2024-04-25 16:00:14 +03:00
|
|
|
helper_image_new(mode, (1, 1))
|
2017-08-05 21:58:31 +03:00
|
|
|
|
2023-03-19 16:30:10 +03:00
|
|
|
@pytest.mark.parametrize("mode", ("", "bad", "very very long"))
|
2024-02-12 13:06:17 +03:00
|
|
|
def test_image_modes_fail(self, mode: str) -> None:
|
2022-08-23 14:41:32 +03:00
|
|
|
with pytest.raises(ValueError) as e:
|
|
|
|
Image.new(mode, (1, 1))
|
|
|
|
assert str(e.value) == "unrecognized image mode"
|
2017-08-05 21:58:31 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_exception_inheritance(self) -> None:
|
2020-04-07 09:58:21 +03:00
|
|
|
assert issubclass(UnidentifiedImageError, OSError)
|
2019-11-19 13:20:02 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_sanity(self) -> None:
|
2014-06-10 13:10:47 +04:00
|
|
|
im = Image.new("L", (100, 100))
|
2020-02-22 16:06:21 +03:00
|
|
|
assert repr(im)[:45] == "<PIL.Image.Image image mode=L size=100x100 at"
|
|
|
|
assert im.mode == "L"
|
|
|
|
assert im.size == (100, 100)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
im = Image.new("RGB", (100, 100))
|
2020-02-22 16:06:21 +03:00
|
|
|
assert repr(im)[:45] == "<PIL.Image.Image image mode=RGB size=100x100 "
|
|
|
|
assert im.mode == "RGB"
|
|
|
|
assert im.size == (100, 100)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
Image.new("L", (100, 100), None)
|
|
|
|
im2 = Image.new("L", (100, 100), 0)
|
|
|
|
im3 = Image.new("L", (100, 100), "black")
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im2.getcolors() == [(10000, 0)]
|
|
|
|
assert im3.getcolors() == [(10000, 0)]
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
Image.new("X", (100, 100))
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
Image.new("", (100, 100))
|
|
|
|
# with pytest.raises(MemoryError):
|
|
|
|
# Image.new("L", (1000000, 1000000))
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2024-08-19 15:55:07 +03:00
|
|
|
@pytest.mark.skipif(PrettyPrinter is None, reason="IPython is not installed")
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_repr_pretty(self) -> None:
|
2022-01-18 03:46:29 +03:00
|
|
|
im = Image.new("L", (100, 100))
|
|
|
|
|
2024-08-19 15:55:07 +03:00
|
|
|
output = io.StringIO()
|
|
|
|
assert PrettyPrinter is not None
|
|
|
|
p = PrettyPrinter(output)
|
2024-07-30 13:20:09 +03:00
|
|
|
im._repr_pretty_(p, False)
|
2024-08-19 15:55:07 +03:00
|
|
|
assert output.getvalue() == "<PIL.Image.Image image mode=L size=100x100>"
|
2022-01-18 03:46:29 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_open_formats(self) -> None:
|
2020-08-03 01:24:02 +03:00
|
|
|
PNGFILE = "Tests/images/hopper.png"
|
|
|
|
JPGFILE = "Tests/images/hopper.jpg"
|
|
|
|
|
|
|
|
with pytest.raises(TypeError):
|
2024-05-30 10:17:22 +03:00
|
|
|
with Image.open(PNGFILE, formats=123): # type: ignore[arg-type]
|
2021-02-11 13:43:54 +03:00
|
|
|
pass
|
2020-08-03 01:24:02 +03:00
|
|
|
|
2024-05-30 10:17:22 +03:00
|
|
|
format_list: list[list[str] | tuple[str, ...]] = [
|
|
|
|
["JPEG"],
|
|
|
|
("JPEG",),
|
|
|
|
["jpeg"],
|
|
|
|
["Jpeg"],
|
|
|
|
["jPeG"],
|
|
|
|
["JpEg"],
|
|
|
|
]
|
|
|
|
for formats in format_list:
|
2020-08-03 01:24:02 +03:00
|
|
|
with pytest.raises(UnidentifiedImageError):
|
2021-02-11 13:43:54 +03:00
|
|
|
with Image.open(PNGFILE, formats=formats):
|
|
|
|
pass
|
2020-08-03 01:24:02 +03:00
|
|
|
|
|
|
|
with Image.open(JPGFILE, formats=formats) as im:
|
|
|
|
assert im.mode == "RGB"
|
|
|
|
assert im.size == (128, 128)
|
|
|
|
|
|
|
|
for file in [PNGFILE, JPGFILE]:
|
|
|
|
with Image.open(file, formats=None) as im:
|
|
|
|
assert im.mode == "RGB"
|
|
|
|
assert im.size == (128, 128)
|
|
|
|
|
2024-05-17 01:44:04 +03:00
|
|
|
def test_open_verbose_failure(self, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
|
|
monkeypatch.setattr(Image, "WARN_POSSIBLE_FORMATS", True)
|
|
|
|
|
2024-05-16 10:47:35 +03:00
|
|
|
im = io.BytesIO(b"")
|
2024-05-17 01:44:04 +03:00
|
|
|
with pytest.warns(UserWarning):
|
|
|
|
with pytest.raises(UnidentifiedImageError):
|
|
|
|
with Image.open(im):
|
|
|
|
pass
|
2024-05-16 10:47:35 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_width_height(self) -> None:
|
2015-06-24 03:35:37 +03:00
|
|
|
im = Image.new("RGB", (1, 2))
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.width == 1
|
|
|
|
assert im.height == 2
|
2015-06-24 03:35:37 +03:00
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(AttributeError):
|
2024-03-02 05:12:17 +03:00
|
|
|
im.size = (3, 4) # type: ignore[misc]
|
2015-06-24 03:35:37 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_set_mode(self) -> None:
|
2023-07-29 02:28:18 +03:00
|
|
|
im = Image.new("RGB", (1, 1))
|
|
|
|
|
|
|
|
with pytest.raises(AttributeError):
|
2024-03-02 05:12:17 +03:00
|
|
|
im.mode = "P" # type: ignore[misc]
|
2023-07-29 02:28:18 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_invalid_image(self) -> None:
|
2019-09-26 15:12:28 +03:00
|
|
|
im = io.BytesIO(b"")
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(UnidentifiedImageError):
|
2021-02-11 13:43:54 +03:00
|
|
|
with Image.open(im):
|
|
|
|
pass
|
2015-06-09 07:36:34 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_bad_mode(self) -> None:
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(ValueError):
|
2024-05-30 10:17:22 +03:00
|
|
|
with Image.open("filename", "bad mode"): # type: ignore[arg-type]
|
2021-02-11 13:43:54 +03:00
|
|
|
pass
|
2015-06-09 07:36:34 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_stringio(self) -> None:
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(ValueError):
|
2024-06-18 16:03:03 +03:00
|
|
|
with Image.open(io.StringIO()): # type: ignore[arg-type]
|
2021-02-11 13:43:54 +03:00
|
|
|
pass
|
2019-12-26 05:10:39 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_pathlib(self, tmp_path: Path) -> None:
|
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(Path("Tests/images/multipage-mmap.tiff")) as im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.mode == "P"
|
|
|
|
assert im.size == (10, 10)
|
2019-05-04 08:00:49 +03:00
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(Path("Tests/images/hopper.jpg")) as im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.mode == "RGB"
|
|
|
|
assert im.size == (128, 128)
|
2015-08-05 14:29:24 +03:00
|
|
|
|
2021-07-24 07:21:33 +03:00
|
|
|
for ext in (".jpg", ".jp2"):
|
2022-04-29 12:17:03 +03:00
|
|
|
if ext == ".jp2" and not features.check_codec("jpg_2000"):
|
|
|
|
pytest.skip("jpg_2000 not available")
|
2021-07-24 07:21:33 +03:00
|
|
|
temp_file = str(tmp_path / ("temp." + ext))
|
|
|
|
if os.path.exists(temp_file):
|
|
|
|
os.remove(temp_file)
|
|
|
|
im.save(Path(temp_file))
|
2016-02-27 05:10:50 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_fp_name(self, tmp_path: Path) -> None:
|
2020-02-23 00:03:01 +03:00
|
|
|
temp_file = str(tmp_path / "temp.jpg")
|
2016-11-15 12:13:25 +03:00
|
|
|
|
2024-06-23 23:59:00 +03:00
|
|
|
class FP(io.BytesIO):
|
2024-02-12 13:06:17 +03:00
|
|
|
name: str
|
|
|
|
|
2024-06-23 23:59:00 +03:00
|
|
|
if sys.version_info >= (3, 12):
|
|
|
|
from collections.abc import Buffer
|
|
|
|
|
|
|
|
def write(self, data: Buffer) -> int:
|
|
|
|
return len(data)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
def write(self, data: Any) -> int:
|
|
|
|
return len(data)
|
2019-06-13 18:54:24 +03:00
|
|
|
|
2016-11-15 12:13:25 +03:00
|
|
|
fp = FP()
|
|
|
|
fp.name = temp_file
|
|
|
|
|
|
|
|
im = hopper()
|
|
|
|
im.save(fp)
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_tempfile(self) -> None:
|
2015-10-03 10:12:44 +03:00
|
|
|
# see #1460, pathlib support breaks tempfile.TemporaryFile on py27
|
|
|
|
# Will error out on save on 3.0.0
|
|
|
|
im = hopper()
|
2016-12-28 01:54:10 +03:00
|
|
|
with tempfile.TemporaryFile() as fp:
|
2019-06-13 18:54:24 +03:00
|
|
|
im.save(fp, "JPEG")
|
2016-12-28 01:54:10 +03:00
|
|
|
fp.seek(0)
|
2024-06-01 14:31:53 +03:00
|
|
|
with Image.open(fp) as reloaded:
|
|
|
|
assert_image_similar(im, reloaded, 20)
|
2015-10-03 10:12:44 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_unknown_extension(self, tmp_path: Path) -> None:
|
2017-02-17 16:39:16 +03:00
|
|
|
im = hopper()
|
2020-02-23 00:03:01 +03:00
|
|
|
temp_file = str(tmp_path / "temp.unknown")
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
im.save(temp_file)
|
2017-02-17 16:39:16 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_internals(self) -> None:
|
2014-06-10 13:10:47 +04:00
|
|
|
im = Image.new("L", (100, 100))
|
|
|
|
im.readonly = 1
|
|
|
|
im._copy()
|
2020-02-22 16:06:21 +03:00
|
|
|
assert not im.readonly
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
im.readonly = 1
|
|
|
|
im.paste(0, (0, 0, 100, 100))
|
2020-02-22 16:06:21 +03:00
|
|
|
assert not im.readonly
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
@pytest.mark.skipif(is_win32(), reason="Test requires opening tempfile twice")
|
2021-11-05 02:48:53 +03:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
sys.platform == "cygwin",
|
|
|
|
reason="Test requires opening an mmaped file for writing",
|
|
|
|
)
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_readonly_save(self, tmp_path: Path) -> None:
|
2020-02-23 00:03:01 +03:00
|
|
|
temp_file = str(tmp_path / "temp.bmp")
|
2019-03-17 15:37:40 +03:00
|
|
|
shutil.copy("Tests/images/rgb32bf-rgba.bmp", temp_file)
|
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(temp_file) as im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.readonly
|
2019-11-25 23:03:23 +03:00
|
|
|
im.save(temp_file)
|
2019-03-17 15:37:40 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_dump(self, tmp_path: Path) -> None:
|
2018-04-01 07:28:37 +03:00
|
|
|
im = Image.new("L", (10, 10))
|
2020-02-23 00:03:01 +03:00
|
|
|
im._dump(str(tmp_path / "temp_L.ppm"))
|
2018-04-01 07:28:37 +03:00
|
|
|
|
|
|
|
im = Image.new("RGB", (10, 10))
|
2020-02-23 00:03:01 +03:00
|
|
|
im._dump(str(tmp_path / "temp_RGB.ppm"))
|
2018-04-01 07:28:37 +03:00
|
|
|
|
|
|
|
im = Image.new("HSV", (10, 10))
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(ValueError):
|
2020-02-23 00:03:01 +03:00
|
|
|
im._dump(str(tmp_path / "temp_HSV.ppm"))
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_comparison_with_other_type(self) -> None:
|
2014-07-05 17:29:40 +04:00
|
|
|
# Arrange
|
2019-06-13 18:54:24 +03:00
|
|
|
item = Image.new("RGB", (25, 25), "#000")
|
2014-07-05 17:29:40 +04:00
|
|
|
num = 12
|
|
|
|
|
|
|
|
# Act/Assert
|
|
|
|
# Shouldn't cause AttributeError (#774)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert item is not None
|
|
|
|
assert item != num
|
2014-07-05 17:29:40 +04:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_expand_x(self) -> None:
|
2014-07-10 03:00:26 +04:00
|
|
|
# Arrange
|
2014-09-04 09:03:55 +04:00
|
|
|
im = hopper()
|
2014-07-10 03:00:26 +04:00
|
|
|
orig_size = im.size
|
|
|
|
xmargin = 5
|
|
|
|
|
|
|
|
# Act
|
|
|
|
im = im._expand(xmargin)
|
|
|
|
|
|
|
|
# Assert
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.size[0] == orig_size[0] + 2 * xmargin
|
|
|
|
assert im.size[1] == orig_size[1] + 2 * xmargin
|
2014-07-10 03:00:26 +04:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_expand_xy(self) -> None:
|
2014-07-10 03:00:26 +04:00
|
|
|
# Arrange
|
2014-09-04 09:03:55 +04:00
|
|
|
im = hopper()
|
2014-07-10 03:00:26 +04:00
|
|
|
orig_size = im.size
|
|
|
|
xmargin = 5
|
|
|
|
ymargin = 3
|
|
|
|
|
|
|
|
# Act
|
|
|
|
im = im._expand(xmargin, ymargin)
|
|
|
|
|
|
|
|
# Assert
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.size[0] == orig_size[0] + 2 * xmargin
|
|
|
|
assert im.size[1] == orig_size[1] + 2 * ymargin
|
2014-07-10 03:00:26 +04:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_getbands(self) -> None:
|
2017-08-12 14:10:39 +03:00
|
|
|
# Assert
|
2020-02-22 16:06:21 +03:00
|
|
|
assert hopper("RGB").getbands() == ("R", "G", "B")
|
|
|
|
assert hopper("YCbCr").getbands() == ("Y", "Cb", "Cr")
|
2014-07-10 03:00:26 +04:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_getchannel_wrong_params(self) -> None:
|
2017-08-12 14:10:39 +03:00
|
|
|
im = hopper()
|
2014-07-10 03:00:26 +04:00
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
im.getchannel(-1)
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
im.getchannel(3)
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
im.getchannel("Z")
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
im.getchannel("1")
|
2017-08-12 14:10:39 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_getchannel(self) -> None:
|
2019-06-13 18:54:24 +03:00
|
|
|
im = hopper("YCbCr")
|
2017-08-12 14:10:39 +03:00
|
|
|
Y, Cb, Cr = im.split()
|
|
|
|
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_equal(Y, im.getchannel(0))
|
|
|
|
assert_image_equal(Y, im.getchannel("Y"))
|
|
|
|
assert_image_equal(Cb, im.getchannel(1))
|
|
|
|
assert_image_equal(Cb, im.getchannel("Cb"))
|
|
|
|
assert_image_equal(Cr, im.getchannel(2))
|
|
|
|
assert_image_equal(Cr, im.getchannel("Cr"))
|
2014-07-10 03:00:26 +04:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_getbbox(self) -> None:
|
2014-07-10 03:00:26 +04:00
|
|
|
# Arrange
|
2014-09-04 09:03:55 +04:00
|
|
|
im = hopper()
|
2014-07-10 03:00:26 +04:00
|
|
|
|
|
|
|
# Act
|
|
|
|
bbox = im.getbbox()
|
|
|
|
|
|
|
|
# Assert
|
2020-02-22 16:06:21 +03:00
|
|
|
assert bbox == (0, 0, 128, 128)
|
2014-07-10 03:00:26 +04:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_ne(self) -> None:
|
2014-07-15 01:42:31 +04:00
|
|
|
# Arrange
|
2019-06-13 18:54:24 +03:00
|
|
|
im1 = Image.new("RGB", (25, 25), "black")
|
|
|
|
im2 = Image.new("RGB", (25, 25), "white")
|
2014-07-15 01:42:31 +04:00
|
|
|
|
|
|
|
# Act / Assert
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im1 != im2
|
2014-07-15 01:42:31 +04:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_alpha_composite(self) -> None:
|
2017-02-14 12:27:02 +03:00
|
|
|
# https://stackoverflow.com/questions/3374878
|
2014-07-15 01:42:31 +04:00
|
|
|
# Arrange
|
2019-06-13 18:54:24 +03:00
|
|
|
expected_colors = sorted(
|
|
|
|
[
|
|
|
|
(1122, (128, 127, 0, 255)),
|
|
|
|
(1089, (0, 255, 0, 255)),
|
|
|
|
(3300, (255, 0, 0, 255)),
|
|
|
|
(1156, (170, 85, 0, 192)),
|
|
|
|
(1122, (0, 255, 0, 128)),
|
|
|
|
(1122, (255, 0, 0, 128)),
|
|
|
|
(1089, (0, 255, 0, 0)),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
dst = Image.new("RGBA", size=(100, 100), color=(0, 255, 0, 255))
|
2014-07-15 01:42:31 +04:00
|
|
|
draw = ImageDraw.Draw(dst)
|
|
|
|
draw.rectangle((0, 33, 100, 66), fill=(0, 255, 0, 128))
|
|
|
|
draw.rectangle((0, 67, 100, 100), fill=(0, 255, 0, 0))
|
2019-06-13 18:54:24 +03:00
|
|
|
src = Image.new("RGBA", size=(100, 100), color=(255, 0, 0, 255))
|
2014-07-15 01:42:31 +04:00
|
|
|
draw = ImageDraw.Draw(src)
|
|
|
|
draw.rectangle((33, 0, 66, 100), fill=(255, 0, 0, 128))
|
|
|
|
draw.rectangle((67, 0, 100, 100), fill=(255, 0, 0, 0))
|
|
|
|
|
|
|
|
# Act
|
|
|
|
img = Image.alpha_composite(dst, src)
|
|
|
|
|
|
|
|
# Assert
|
2024-07-05 20:56:24 +03:00
|
|
|
img_colors = img.getcolors()
|
|
|
|
assert img_colors is not None
|
|
|
|
assert sorted(img_colors) == expected_colors
|
2014-07-15 01:42:31 +04:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_alpha_inplace(self) -> None:
|
2019-06-13 18:54:24 +03:00
|
|
|
src = Image.new("RGBA", (128, 128), "blue")
|
2017-06-20 19:54:59 +03:00
|
|
|
|
2019-06-13 18:54:24 +03:00
|
|
|
over = Image.new("RGBA", (128, 128), "red")
|
|
|
|
mask = hopper("L")
|
2017-06-20 19:54:59 +03:00
|
|
|
over.putalpha(mask)
|
|
|
|
|
|
|
|
target = Image.alpha_composite(src, over)
|
|
|
|
|
|
|
|
# basic
|
|
|
|
full = src.copy()
|
|
|
|
full.alpha_composite(over)
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_equal(full, target)
|
2017-06-20 19:54:59 +03:00
|
|
|
|
|
|
|
# with offset down to right
|
|
|
|
offset = src.copy()
|
|
|
|
offset.alpha_composite(over, (64, 64))
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_equal(offset.crop((64, 64, 127, 127)), target.crop((0, 0, 63, 63)))
|
2020-02-22 16:06:21 +03:00
|
|
|
assert offset.size == (128, 128)
|
2017-06-20 19:54:59 +03:00
|
|
|
|
2021-03-06 12:54:21 +03:00
|
|
|
# with negative offset
|
|
|
|
offset = src.copy()
|
|
|
|
offset.alpha_composite(over, (-64, -64))
|
|
|
|
assert_image_equal(offset.crop((0, 0, 63, 63)), target.crop((64, 64, 127, 127)))
|
|
|
|
assert offset.size == (128, 128)
|
|
|
|
|
2017-06-20 19:54:59 +03:00
|
|
|
# offset and crop
|
|
|
|
box = src.copy()
|
2017-06-28 00:03:38 +03:00
|
|
|
box.alpha_composite(over, (64, 64), (0, 0, 32, 32))
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_equal(box.crop((64, 64, 96, 96)), target.crop((0, 0, 32, 32)))
|
|
|
|
assert_image_equal(box.crop((96, 96, 128, 128)), src.crop((0, 0, 32, 32)))
|
2020-02-22 16:06:21 +03:00
|
|
|
assert box.size == (128, 128)
|
2017-06-26 22:04:44 +03:00
|
|
|
|
|
|
|
# source point
|
|
|
|
source = src.copy()
|
2017-06-28 00:03:38 +03:00
|
|
|
source.alpha_composite(over, (32, 32), (32, 32, 96, 96))
|
2017-06-26 22:04:44 +03:00
|
|
|
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_equal(source.crop((32, 32, 96, 96)), target.crop((32, 32, 96, 96)))
|
2020-02-22 16:06:21 +03:00
|
|
|
assert source.size == (128, 128)
|
2017-06-20 19:54:59 +03:00
|
|
|
|
2017-09-01 13:36:51 +03:00
|
|
|
# errors
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(ValueError):
|
2024-06-22 03:09:11 +03:00
|
|
|
source.alpha_composite(over, "invalid destination") # type: ignore[arg-type]
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(ValueError):
|
2024-06-22 03:09:11 +03:00
|
|
|
source.alpha_composite(over, (0, 0), "invalid source") # type: ignore[arg-type]
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(ValueError):
|
2024-06-22 03:09:11 +03:00
|
|
|
source.alpha_composite(over, 0) # type: ignore[arg-type]
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(ValueError):
|
2024-06-22 03:09:11 +03:00
|
|
|
source.alpha_composite(over, (0, 0), 0) # type: ignore[arg-type]
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
source.alpha_composite(over, (0, 0), (0, -1))
|
2017-09-01 13:36:51 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_register_open_duplicates(self) -> None:
|
2023-01-28 14:43:04 +03:00
|
|
|
# Arrange
|
|
|
|
factory, accept = Image.OPEN["JPEG"]
|
|
|
|
id_length = len(Image.ID)
|
|
|
|
|
|
|
|
# Act
|
|
|
|
Image.register_open("JPEG", factory, accept)
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert len(Image.ID) == id_length
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_registered_extensions_uninitialized(self) -> None:
|
2017-01-07 05:20:16 +03:00
|
|
|
# Arrange
|
|
|
|
Image._initialized = 0
|
|
|
|
|
2016-06-20 18:36:26 +03:00
|
|
|
# Act
|
2017-01-07 05:20:16 +03:00
|
|
|
Image.registered_extensions()
|
2016-06-20 18:36:26 +03:00
|
|
|
|
|
|
|
# Assert
|
2020-02-22 16:06:21 +03:00
|
|
|
assert Image._initialized == 2
|
2017-01-07 05:20:16 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_registered_extensions(self) -> None:
|
2016-06-20 18:36:26 +03:00
|
|
|
# Arrange
|
|
|
|
# Open an image to trigger plugin registration
|
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"):
|
|
|
|
pass
|
2016-06-20 18:36:26 +03:00
|
|
|
|
|
|
|
# Act
|
2017-01-07 05:20:16 +03:00
|
|
|
extensions = Image.registered_extensions()
|
2016-06-20 18:36:26 +03:00
|
|
|
|
|
|
|
# Assert
|
2020-02-22 16:06:21 +03:00
|
|
|
assert extensions
|
2019-06-13 18:54:24 +03:00
|
|
|
for ext in [".cur", ".icns", ".tif", ".tiff"]:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert ext in extensions
|
2016-06-20 18:36:26 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_effect_mandelbrot(self) -> None:
|
2014-09-02 16:14:00 +04:00
|
|
|
# Arrange
|
|
|
|
size = (512, 512)
|
|
|
|
extent = (-3, -2.5, 2, 2.5)
|
|
|
|
quality = 100
|
|
|
|
|
|
|
|
# Act
|
|
|
|
im = Image.effect_mandelbrot(size, extent, quality)
|
|
|
|
|
|
|
|
# Assert
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.size == (512, 512)
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile(im, "Tests/images/effect_mandelbrot.png")
|
2014-09-02 16:14:00 +04:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_effect_mandelbrot_bad_arguments(self) -> None:
|
2014-09-02 17:39:35 +04:00
|
|
|
# Arrange
|
|
|
|
size = (512, 512)
|
|
|
|
# Get coordinates the wrong way round:
|
|
|
|
extent = (+3, +2.5, -2, -2.5)
|
|
|
|
# Quality < 2:
|
|
|
|
quality = 1
|
|
|
|
|
|
|
|
# Act/Assert
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
Image.effect_mandelbrot(size, extent, quality)
|
2014-09-02 17:39:35 +04:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_effect_noise(self) -> None:
|
2014-09-02 15:11:08 +04:00
|
|
|
# Arrange
|
|
|
|
size = (100, 100)
|
|
|
|
sigma = 128
|
|
|
|
|
|
|
|
# Act
|
2017-12-19 18:12:58 +03:00
|
|
|
im = Image.effect_noise(size, sigma)
|
|
|
|
|
|
|
|
# Assert
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.size == (100, 100)
|
|
|
|
assert im.mode == "L"
|
2017-12-19 18:12:58 +03:00
|
|
|
p0 = im.getpixel((0, 0))
|
|
|
|
p1 = im.getpixel((0, 1))
|
|
|
|
p2 = im.getpixel((0, 2))
|
|
|
|
p3 = im.getpixel((0, 3))
|
|
|
|
p4 = im.getpixel((0, 4))
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_not_all_same([p0, p1, p2, p3, p4])
|
2014-09-02 15:11:08 +04:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_effect_spread(self) -> None:
|
2014-09-02 16:53:58 +04:00
|
|
|
# Arrange
|
2014-09-04 09:03:55 +04:00
|
|
|
im = hopper()
|
2014-09-02 16:53:58 +04:00
|
|
|
distance = 10
|
|
|
|
|
|
|
|
# Act
|
|
|
|
im2 = im.effect_spread(distance)
|
|
|
|
|
|
|
|
# Assert
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.size == (128, 128)
|
2021-02-21 14:22:29 +03:00
|
|
|
assert_image_similar_tofile(im2, "Tests/images/effect_spread.png", 110)
|
2014-09-02 16:53:58 +04:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_effect_spread_zero(self) -> None:
|
2020-09-08 11:51:58 +03:00
|
|
|
# Arrange
|
|
|
|
im = hopper()
|
|
|
|
distance = 0
|
|
|
|
|
|
|
|
# Act
|
|
|
|
im2 = im.effect_spread(distance)
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert_image_equal(im, im2)
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_check_size(self) -> None:
|
2020-02-23 00:03:01 +03:00
|
|
|
# Checking that the _check_size function throws value errors when we want it to
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(ValueError):
|
2024-05-30 10:17:22 +03:00
|
|
|
# not a tuple
|
|
|
|
Image.new("RGB", 0) # type: ignore[arg-type]
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(ValueError):
|
2024-05-30 10:17:22 +03:00
|
|
|
# tuple too short
|
|
|
|
Image.new("RGB", (0,)) # type: ignore[arg-type]
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(ValueError):
|
2019-06-13 18:54:24 +03:00
|
|
|
Image.new("RGB", (-1, -1)) # w,h < 0
|
2016-11-29 22:25:49 +03:00
|
|
|
|
|
|
|
# this should pass with 0 sized images, #2259
|
2019-06-13 18:54:24 +03:00
|
|
|
im = Image.new("L", (0, 0))
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.size == (0, 0)
|
2016-10-03 13:38:15 +03:00
|
|
|
|
2019-06-13 18:54:24 +03:00
|
|
|
im = Image.new("L", (0, 100))
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.size == (0, 100)
|
2017-09-17 02:58:01 +03:00
|
|
|
|
2019-06-13 18:54:24 +03:00
|
|
|
im = Image.new("L", (100, 0))
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.size == (100, 0)
|
2017-09-17 02:58:01 +03:00
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
assert Image.new("RGB", (1, 1))
|
2016-10-04 03:06:35 +03:00
|
|
|
# Should pass lists too
|
2019-06-13 18:54:24 +03:00
|
|
|
i = Image.new("RGB", [1, 1])
|
2020-02-22 16:06:21 +03:00
|
|
|
assert isinstance(i.size, tuple)
|
2016-10-03 13:50:25 +03:00
|
|
|
|
2022-12-31 05:47:07 +03:00
|
|
|
@pytest.mark.timeout(0.75)
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
"PILLOW_VALGRIND_TEST" in os.environ, reason="Valgrind is slower"
|
|
|
|
)
|
2022-12-30 08:48:33 +03:00
|
|
|
@pytest.mark.parametrize("size", ((0, 100000000), (100000000, 0)))
|
2024-02-12 13:06:17 +03:00
|
|
|
def test_empty_image(self, size: tuple[int, int]) -> None:
|
2022-12-30 08:48:33 +03:00
|
|
|
Image.new("RGB", size)
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_storage_neg(self) -> None:
|
2016-10-03 13:50:25 +03:00
|
|
|
# Storage.c accepted negative values for xsize, ysize. Was
|
|
|
|
# test_neg_ppm, but the core function for that has been
|
|
|
|
# removed Calling directly into core to test the error in
|
|
|
|
# Storage.c, rather than the size check above
|
2016-10-04 03:06:35 +03:00
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(ValueError):
|
2019-06-13 18:54:24 +03:00
|
|
|
Image.core.fill("RGB", (2, -2), (0, 0, 0))
|
2016-10-03 13:50:25 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_one_item_tuple(self) -> None:
|
2020-09-20 07:23:05 +03:00
|
|
|
for mode in ("I", "F", "L"):
|
|
|
|
im = Image.new(mode, (100, 100), (5,))
|
|
|
|
px = im.load()
|
2024-07-02 13:10:47 +03:00
|
|
|
assert px is not None
|
2020-09-20 07:23:05 +03:00
|
|
|
assert px[0, 0] == 5
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_linear_gradient_wrong_mode(self) -> None:
|
2017-01-29 19:17:31 +03:00
|
|
|
# Arrange
|
|
|
|
wrong_mode = "RGB"
|
|
|
|
|
|
|
|
# Act / Assert
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
Image.linear_gradient(wrong_mode)
|
2017-01-29 19:17:31 +03:00
|
|
|
|
2022-08-23 14:41:32 +03:00
|
|
|
@pytest.mark.parametrize("mode", ("L", "P", "I", "F"))
|
2024-02-12 13:06:17 +03:00
|
|
|
def test_linear_gradient(self, mode: str) -> None:
|
2017-01-29 19:17:31 +03:00
|
|
|
# Arrange
|
2017-02-06 23:03:17 +03:00
|
|
|
target_file = "Tests/images/linear_gradient.png"
|
2017-01-29 19:17:31 +03:00
|
|
|
|
2022-08-23 14:41:32 +03:00
|
|
|
# Act
|
|
|
|
im = Image.linear_gradient(mode)
|
2017-01-29 19:17:31 +03:00
|
|
|
|
2022-08-23 14:41:32 +03:00
|
|
|
# Assert
|
|
|
|
assert im.size == (256, 256)
|
|
|
|
assert im.mode == mode
|
|
|
|
assert im.getpixel((0, 0)) == 0
|
|
|
|
assert im.getpixel((255, 255)) == 255
|
|
|
|
with Image.open(target_file) as target:
|
|
|
|
target = target.convert(mode)
|
|
|
|
assert_image_equal(im, target)
|
2017-01-29 19:17:31 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_radial_gradient_wrong_mode(self) -> None:
|
2017-01-29 19:44:24 +03:00
|
|
|
# Arrange
|
|
|
|
wrong_mode = "RGB"
|
|
|
|
|
|
|
|
# Act / Assert
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
Image.radial_gradient(wrong_mode)
|
2017-01-29 19:44:24 +03:00
|
|
|
|
2022-08-23 14:41:32 +03:00
|
|
|
@pytest.mark.parametrize("mode", ("L", "P", "I", "F"))
|
2024-02-12 13:06:17 +03:00
|
|
|
def test_radial_gradient(self, mode: str) -> None:
|
2017-01-29 19:44:24 +03:00
|
|
|
# Arrange
|
2017-02-06 23:03:17 +03:00
|
|
|
target_file = "Tests/images/radial_gradient.png"
|
2022-08-23 14:41:32 +03:00
|
|
|
|
|
|
|
# Act
|
|
|
|
im = Image.radial_gradient(mode)
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert im.size == (256, 256)
|
|
|
|
assert im.mode == mode
|
|
|
|
assert im.getpixel((0, 0)) == 255
|
|
|
|
assert im.getpixel((128, 128)) == 0
|
|
|
|
with Image.open(target_file) as target:
|
|
|
|
target = target.convert(mode)
|
|
|
|
assert_image_equal(im, target)
|
2017-01-29 19:44:24 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_register_extensions(self) -> None:
|
2017-09-04 13:32:15 +03:00
|
|
|
test_format = "a"
|
|
|
|
exts = ["b", "c"]
|
|
|
|
for ext in exts:
|
|
|
|
Image.register_extension(test_format, ext)
|
|
|
|
ext_individual = Image.EXTENSION.copy()
|
|
|
|
for ext in exts:
|
|
|
|
del Image.EXTENSION[ext]
|
|
|
|
|
|
|
|
Image.register_extensions(test_format, exts)
|
|
|
|
ext_multiple = Image.EXTENSION.copy()
|
|
|
|
for ext in exts:
|
|
|
|
del Image.EXTENSION[ext]
|
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
assert ext_individual == ext_multiple
|
2017-09-04 13:32:15 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_remap_palette(self) -> None:
|
2021-06-28 13:11:14 +03:00
|
|
|
# Test identity transform
|
|
|
|
with Image.open("Tests/images/hopper.gif") as im:
|
|
|
|
assert_image_equal(im, im.remap_palette(list(range(256))))
|
|
|
|
|
2022-06-05 17:12:48 +03:00
|
|
|
# Test identity transform with an RGBA palette
|
|
|
|
im = Image.new("P", (256, 1))
|
|
|
|
for x in range(256):
|
|
|
|
im.putpixel((x, 0), x)
|
|
|
|
im.putpalette(list(range(256)) * 4, "RGBA")
|
|
|
|
im_remapped = im.remap_palette(list(range(256)))
|
|
|
|
assert_image_equal(im, im_remapped)
|
|
|
|
assert im.palette.palette == im_remapped.palette.palette
|
|
|
|
|
2017-09-01 13:36:51 +03:00
|
|
|
# Test illegal image mode
|
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 hopper() as im:
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
im.remap_palette(None)
|
2017-09-01 13:36:51 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_remap_palette_transparency(self) -> None:
|
2023-07-17 16:04:43 +03:00
|
|
|
im = Image.new("P", (1, 2), (0, 0, 0))
|
|
|
|
im.putpixel((0, 1), (255, 0, 0))
|
2022-05-21 10:38:44 +03:00
|
|
|
im.info["transparency"] = 0
|
|
|
|
|
|
|
|
im_remapped = im.remap_palette([1, 0])
|
|
|
|
assert im_remapped.info["transparency"] == 1
|
2024-07-05 20:56:24 +03:00
|
|
|
palette = im_remapped.getpalette()
|
|
|
|
assert palette is not None
|
|
|
|
assert len(palette) == 6
|
2022-05-21 10:38:44 +03:00
|
|
|
|
|
|
|
# Test unused transparency
|
|
|
|
im.info["transparency"] = 2
|
|
|
|
|
|
|
|
im_remapped = im.remap_palette([1, 0])
|
|
|
|
assert "transparency" not in im_remapped.info
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test__new(self) -> None:
|
2019-06-13 18:54:24 +03:00
|
|
|
im = hopper("RGB")
|
|
|
|
im_p = hopper("P")
|
2017-08-31 16:18:59 +03:00
|
|
|
|
2019-06-13 18:54:24 +03:00
|
|
|
blank_p = Image.new("P", (10, 10))
|
|
|
|
blank_pa = Image.new("PA", (10, 10))
|
2017-08-31 16:18:59 +03:00
|
|
|
blank_p.palette = None
|
|
|
|
blank_pa.palette = None
|
2017-09-04 13:32:15 +03:00
|
|
|
|
2024-02-12 13:06:17 +03:00
|
|
|
def _make_new(
|
|
|
|
base_image: Image.Image,
|
|
|
|
image: Image.Image,
|
|
|
|
palette_result: ImagePalette.ImagePalette | None = None,
|
|
|
|
) -> None:
|
2023-08-07 17:49:20 +03:00
|
|
|
new_image = base_image._new(image.im)
|
|
|
|
assert new_image.mode == image.mode
|
|
|
|
assert new_image.size == image.size
|
|
|
|
assert new_image.info == base_image.info
|
2017-08-31 16:18:59 +03:00
|
|
|
if palette_result is not None:
|
2024-08-02 16:30:27 +03:00
|
|
|
assert new_image.palette is not None
|
2023-08-07 17:49:20 +03:00
|
|
|
assert new_image.palette.tobytes() == palette_result.tobytes()
|
2017-08-31 16:18:59 +03:00
|
|
|
else:
|
2023-08-07 17:49:20 +03:00
|
|
|
assert new_image.palette is None
|
2017-09-04 13:32:15 +03:00
|
|
|
|
2024-07-06 12:17:23 +03:00
|
|
|
_make_new(im, im_p, ImagePalette.ImagePalette("RGB"))
|
2017-08-31 16:18:59 +03:00
|
|
|
_make_new(im_p, im, None)
|
|
|
|
_make_new(im, blank_p, ImagePalette.ImagePalette())
|
|
|
|
_make_new(im, blank_pa, ImagePalette.ImagePalette())
|
2017-09-04 13:32:15 +03:00
|
|
|
|
2024-02-26 09:34:54 +03:00
|
|
|
@pytest.mark.parametrize(
|
2024-02-26 16:47:13 +03:00
|
|
|
"mode, color",
|
2024-02-26 09:34:54 +03:00
|
|
|
(
|
2019-06-13 18:54:24 +03:00
|
|
|
("RGB", "#DDEEFF"),
|
2019-03-15 09:30:28 +03:00
|
|
|
("RGB", (221, 238, 255)),
|
2019-06-13 18:54:24 +03:00
|
|
|
("RGBA", (221, 238, 255, 255)),
|
2024-02-26 09:34:54 +03:00
|
|
|
),
|
|
|
|
)
|
2024-02-26 17:47:30 +03:00
|
|
|
def test_p_from_rgb_rgba(self, mode: str, color: str | tuple[int, ...]) -> None:
|
2024-02-26 09:34:54 +03:00
|
|
|
im = Image.new("P", (100, 100), color)
|
|
|
|
expected = Image.new(mode, (100, 100), color)
|
|
|
|
assert_image_equal(im.convert(mode), expected)
|
2019-03-15 09:30:28 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_no_resource_warning_on_save(self, tmp_path: Path) -> None:
|
2014-12-30 17:20:42 +03:00
|
|
|
# https://github.com/python-pillow/Pillow/issues/835
|
|
|
|
# Arrange
|
2019-06-13 18:54:24 +03:00
|
|
|
test_file = "Tests/images/hopper.png"
|
2020-02-23 00:03:01 +03:00
|
|
|
temp_file = str(tmp_path / "temp.jpg")
|
2014-12-30 17:20:42 +03:00
|
|
|
|
|
|
|
# Act/Assert
|
|
|
|
with Image.open(test_file) as im:
|
2022-02-21 05:49:01 +03:00
|
|
|
with warnings.catch_warnings():
|
2024-10-26 08:15:46 +03:00
|
|
|
warnings.simplefilter("error")
|
|
|
|
|
2021-02-10 15:37:55 +03:00
|
|
|
im.save(temp_file)
|
2014-12-30 17:20:42 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_no_new_file_on_error(self, tmp_path: Path) -> None:
|
2022-03-14 15:33:45 +03:00
|
|
|
temp_file = str(tmp_path / "temp.jpg")
|
|
|
|
|
|
|
|
im = Image.new("RGB", (0, 0))
|
2022-03-28 13:18:53 +03:00
|
|
|
with pytest.raises(ValueError):
|
2022-03-14 15:33:45 +03:00
|
|
|
im.save(temp_file)
|
|
|
|
|
|
|
|
assert not os.path.exists(temp_file)
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_load_on_nonexclusive_multiframe(self) -> None:
|
2019-03-12 00:54:43 +03:00
|
|
|
with open("Tests/images/frozenpond.mpo", "rb") as fp:
|
2019-06-13 18:54:24 +03:00
|
|
|
|
2024-02-12 13:06:17 +03:00
|
|
|
def act(fp: IO[bytes]) -> None:
|
2019-03-12 00:54:43 +03:00
|
|
|
im = Image.open(fp)
|
|
|
|
im.load()
|
2019-06-13 18:54:24 +03:00
|
|
|
|
2019-03-12 00:54:43 +03:00
|
|
|
act(fp)
|
|
|
|
|
|
|
|
with Image.open(fp) as im:
|
|
|
|
im.load()
|
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
assert not fp.closed
|
2019-03-12 00:54:43 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_empty_exif(self) -> None:
|
2022-03-12 00:23:40 +03:00
|
|
|
with Image.open("Tests/images/exif.png") as im:
|
|
|
|
exif = im.getexif()
|
2022-10-13 05:20:11 +03:00
|
|
|
assert dict(exif)
|
2022-03-12 00:23:40 +03:00
|
|
|
|
|
|
|
# Test that exif data is cleared after another load
|
|
|
|
exif.load(None)
|
2022-10-13 05:20:11 +03:00
|
|
|
assert not dict(exif)
|
2022-03-12 00:23:40 +03:00
|
|
|
|
|
|
|
# Test loading just the EXIF header
|
|
|
|
exif.load(b"Exif\x00\x00")
|
2022-10-13 05:20:11 +03:00
|
|
|
assert not dict(exif)
|
2022-03-12 00:23:40 +03:00
|
|
|
|
2024-09-06 04:56:06 +03:00
|
|
|
def test_duplicate_exif_header(self) -> None:
|
|
|
|
with Image.open("Tests/images/exif.png") as im:
|
|
|
|
im.load()
|
|
|
|
im.info["exif"] = b"Exif\x00\x00" + im.info["exif"]
|
|
|
|
|
|
|
|
exif = im.getexif()
|
|
|
|
assert exif[274] == 1
|
|
|
|
|
2024-07-13 05:46:15 +03:00
|
|
|
def test_empty_get_ifd(self) -> None:
|
|
|
|
exif = Image.Exif()
|
|
|
|
ifd = exif.get_ifd(0x8769)
|
|
|
|
assert ifd == {}
|
|
|
|
|
|
|
|
ifd[36864] = b"0220"
|
|
|
|
assert exif.get_ifd(0x8769) == {36864: b"0220"}
|
|
|
|
|
2021-04-10 17:58:01 +03:00
|
|
|
@mark_if_feature_version(
|
|
|
|
pytest.mark.valgrind_known_error, "libjpeg_turbo", "2.0", reason="Known Failing"
|
|
|
|
)
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_exif_jpeg(self, tmp_path: Path) -> None:
|
2020-07-02 13:28:00 +03:00
|
|
|
with Image.open("Tests/images/exif-72dpi-int.jpg") as im: # Little endian
|
|
|
|
exif = im.getexif()
|
|
|
|
assert 258 not in exif
|
2020-10-03 13:58:12 +03:00
|
|
|
assert 274 in exif
|
2020-10-05 12:35:45 +03:00
|
|
|
assert 282 in exif
|
|
|
|
assert exif[296] == 2
|
2020-07-02 13:28:00 +03:00
|
|
|
assert exif[11] == "gThumb 3.0.1"
|
|
|
|
|
|
|
|
out = str(tmp_path / "temp.jpg")
|
|
|
|
exif[258] = 8
|
2020-10-03 13:58:12 +03:00
|
|
|
del exif[274]
|
2020-10-05 12:35:45 +03:00
|
|
|
del exif[282]
|
|
|
|
exif[296] = 455
|
2020-07-02 13:28:00 +03:00
|
|
|
exif[11] = "Pillow test"
|
|
|
|
im.save(out, exif=exif)
|
|
|
|
with Image.open(out) as reloaded:
|
|
|
|
reloaded_exif = reloaded.getexif()
|
|
|
|
assert reloaded_exif[258] == 8
|
2020-10-03 13:58:12 +03:00
|
|
|
assert 274 not in reloaded_exif
|
2020-10-05 12:35:45 +03:00
|
|
|
assert 282 not in reloaded_exif
|
|
|
|
assert reloaded_exif[296] == 455
|
2020-07-02 13:28:00 +03:00
|
|
|
assert reloaded_exif[11] == "Pillow test"
|
|
|
|
|
|
|
|
with Image.open("Tests/images/no-dpi-in-exif.jpg") as im: # Big endian
|
|
|
|
exif = im.getexif()
|
|
|
|
assert 258 not in exif
|
2020-10-05 12:35:45 +03:00
|
|
|
assert 306 in exif
|
|
|
|
assert exif[274] == 1
|
2020-07-02 13:28:00 +03:00
|
|
|
assert exif[305] == "Adobe Photoshop CC 2017 (Macintosh)"
|
|
|
|
|
|
|
|
out = str(tmp_path / "temp.jpg")
|
|
|
|
exif[258] = 8
|
2020-10-05 12:35:45 +03:00
|
|
|
del exif[306]
|
|
|
|
exif[274] = 455
|
2020-07-02 13:28:00 +03:00
|
|
|
exif[305] = "Pillow test"
|
|
|
|
im.save(out, exif=exif)
|
|
|
|
with Image.open(out) as reloaded:
|
|
|
|
reloaded_exif = reloaded.getexif()
|
|
|
|
assert reloaded_exif[258] == 8
|
2020-10-05 12:35:45 +03:00
|
|
|
assert 306 not in reloaded_exif
|
|
|
|
assert reloaded_exif[274] == 455
|
2020-07-02 13:28:00 +03:00
|
|
|
assert reloaded_exif[305] == "Pillow test"
|
|
|
|
|
|
|
|
@skip_unless_feature("webp")
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_exif_webp(self, tmp_path: Path) -> None:
|
2020-07-02 13:28:00 +03:00
|
|
|
with Image.open("Tests/images/hopper.webp") as im:
|
|
|
|
exif = im.getexif()
|
|
|
|
assert exif == {}
|
|
|
|
|
|
|
|
out = str(tmp_path / "temp.webp")
|
|
|
|
exif[258] = 8
|
|
|
|
exif[40963] = 455
|
|
|
|
exif[305] = "Pillow test"
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def check_exif() -> None:
|
2020-07-02 13:28:00 +03:00
|
|
|
with Image.open(out) as reloaded:
|
|
|
|
reloaded_exif = reloaded.getexif()
|
|
|
|
assert reloaded_exif[258] == 8
|
|
|
|
assert reloaded_exif[40963] == 455
|
|
|
|
assert reloaded_exif[305] == "Pillow test"
|
|
|
|
|
|
|
|
im.save(out, exif=exif)
|
|
|
|
check_exif()
|
|
|
|
im.save(out, exif=exif, save_all=True)
|
|
|
|
check_exif()
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_exif_png(self, tmp_path: Path) -> None:
|
2020-07-02 13:28:00 +03:00
|
|
|
with Image.open("Tests/images/exif.png") as im:
|
|
|
|
exif = im.getexif()
|
|
|
|
assert exif == {274: 1}
|
|
|
|
|
|
|
|
out = str(tmp_path / "temp.png")
|
|
|
|
exif[258] = 8
|
|
|
|
del exif[274]
|
|
|
|
exif[40963] = 455
|
|
|
|
exif[305] = "Pillow test"
|
|
|
|
im.save(out, exif=exif)
|
|
|
|
|
|
|
|
with Image.open(out) as reloaded:
|
|
|
|
reloaded_exif = reloaded.getexif()
|
|
|
|
assert reloaded_exif == {258: 8, 40963: 455, 305: "Pillow test"}
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_exif_interop(self) -> None:
|
2020-07-02 13:28:00 +03:00
|
|
|
with Image.open("Tests/images/flower.jpg") as im:
|
|
|
|
exif = im.getexif()
|
|
|
|
assert exif.get_ifd(0xA005) == {
|
|
|
|
1: "R98",
|
|
|
|
2: b"0100",
|
|
|
|
4097: 2272,
|
|
|
|
4098: 1704,
|
|
|
|
}
|
|
|
|
|
2021-02-21 23:47:59 +03:00
|
|
|
reloaded_exif = Image.Exif()
|
|
|
|
reloaded_exif.load(exif.tobytes())
|
|
|
|
assert reloaded_exif.get_ifd(0xA005) == exif.get_ifd(0xA005)
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_exif_ifd1(self) -> None:
|
2022-12-05 01:09:00 +03:00
|
|
|
with Image.open("Tests/images/flower.jpg") as im:
|
|
|
|
exif = im.getexif()
|
|
|
|
assert exif.get_ifd(ExifTags.IFD.IFD1) == {
|
|
|
|
513: 2036,
|
|
|
|
514: 5448,
|
|
|
|
259: 6,
|
|
|
|
296: 2,
|
|
|
|
282: 180.0,
|
|
|
|
283: 180.0,
|
|
|
|
}
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_exif_ifd(self) -> None:
|
2021-02-21 23:47:59 +03:00
|
|
|
with Image.open("Tests/images/flower.jpg") as im:
|
|
|
|
exif = im.getexif()
|
|
|
|
del exif.get_ifd(0x8769)[0xA005]
|
2020-10-05 12:16:48 +03:00
|
|
|
|
|
|
|
reloaded_exif = Image.Exif()
|
|
|
|
reloaded_exif.load(exif.tobytes())
|
|
|
|
assert reloaded_exif.get_ifd(0x8769) == exif.get_ifd(0x8769)
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_exif_load_from_fp(self) -> None:
|
2021-04-19 12:46:49 +03:00
|
|
|
with Image.open("Tests/images/flower.jpg") as im:
|
|
|
|
data = im.info["exif"]
|
|
|
|
if data.startswith(b"Exif\x00\x00"):
|
|
|
|
data = data[6:]
|
|
|
|
fp = io.BytesIO(data)
|
|
|
|
|
|
|
|
exif = Image.Exif()
|
|
|
|
exif.load_from_fp(fp)
|
|
|
|
assert exif == {
|
|
|
|
271: "Canon",
|
|
|
|
272: "Canon PowerShot S40",
|
|
|
|
274: 1,
|
|
|
|
282: 180.0,
|
|
|
|
283: 180.0,
|
|
|
|
296: 2,
|
|
|
|
306: "2003:12:14 12:01:44",
|
|
|
|
531: 1,
|
|
|
|
34665: 196,
|
|
|
|
}
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_exif_hide_offsets(self) -> None:
|
2022-11-26 07:44:03 +03:00
|
|
|
with Image.open("Tests/images/flower.jpg") as im:
|
|
|
|
exif = im.getexif()
|
|
|
|
|
|
|
|
# Check offsets are present initially
|
|
|
|
assert 0x8769 in exif
|
|
|
|
for tag in (0xA005, 0x927C):
|
|
|
|
assert tag in exif.get_ifd(0x8769)
|
|
|
|
assert exif.get_ifd(0xA005)
|
|
|
|
loaded_exif = exif
|
|
|
|
|
|
|
|
with Image.open("Tests/images/flower.jpg") as im:
|
|
|
|
new_exif = im.getexif()
|
|
|
|
|
|
|
|
for exif in (loaded_exif, new_exif):
|
|
|
|
exif.hide_offsets()
|
|
|
|
|
|
|
|
# Assert they are hidden afterwards,
|
|
|
|
# but that the IFDs are still available
|
|
|
|
assert 0x8769 not in exif
|
|
|
|
assert exif.get_ifd(0x8769)
|
|
|
|
for tag in (0xA005, 0x927C):
|
|
|
|
assert tag not in exif.get_ifd(0x8769)
|
|
|
|
assert exif.get_ifd(0xA005)
|
|
|
|
|
2024-05-20 16:11:50 +03:00
|
|
|
def test_empty_xmp(self) -> None:
|
|
|
|
with Image.open("Tests/images/hopper.gif") as im:
|
2024-08-23 11:48:28 +03:00
|
|
|
if ElementTree is None:
|
|
|
|
with pytest.warns(
|
|
|
|
UserWarning,
|
|
|
|
match="XMP data cannot be read without defusedxml dependency",
|
|
|
|
):
|
|
|
|
xmp = im.getxmp()
|
|
|
|
else:
|
|
|
|
xmp = im.getxmp()
|
|
|
|
assert xmp == {}
|
2024-05-20 16:11:50 +03:00
|
|
|
|
2024-06-26 12:31:40 +03:00
|
|
|
def test_getxmp_padded(self) -> None:
|
|
|
|
im = Image.new("RGB", (1, 1))
|
|
|
|
im.info["xmp"] = (
|
|
|
|
b'<?xpacket begin="\xef\xbb\xbf" id="W5M0MpCehiHzreSzNTczkc9d"?>\n'
|
|
|
|
b'<x:xmpmeta xmlns:x="adobe:ns:meta/" />\n<?xpacket end="w"?>\x00\x00'
|
|
|
|
)
|
|
|
|
if ElementTree is None:
|
|
|
|
with pytest.warns(
|
|
|
|
UserWarning,
|
|
|
|
match="XMP data cannot be read without defusedxml dependency",
|
|
|
|
):
|
|
|
|
assert im.getxmp() == {}
|
|
|
|
else:
|
|
|
|
assert im.getxmp() == {"xmpmeta": None}
|
|
|
|
|
2022-01-07 08:29:38 +03:00
|
|
|
@pytest.mark.parametrize("size", ((1, 0), (0, 1), (0, 0)))
|
2024-02-12 13:06:17 +03:00
|
|
|
def test_zero_tobytes(self, size: tuple[int, int]) -> None:
|
2022-01-07 08:29:38 +03:00
|
|
|
im = Image.new("RGB", size)
|
|
|
|
assert im.tobytes() == b""
|
|
|
|
|
2023-10-25 00:52:06 +03:00
|
|
|
@pytest.mark.parametrize("size", ((1, 0), (0, 1), (0, 0)))
|
2024-02-12 13:06:17 +03:00
|
|
|
def test_zero_frombytes(self, size: tuple[int, int]) -> None:
|
2023-10-25 00:52:06 +03:00
|
|
|
Image.frombytes("RGB", size, b"")
|
|
|
|
|
2023-10-25 00:52:26 +03:00
|
|
|
im = Image.new("RGB", size)
|
|
|
|
im.frombytes(b"")
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_has_transparency_data(self) -> None:
|
2023-09-25 13:10:44 +03:00
|
|
|
for mode in ("1", "L", "P", "RGB"):
|
|
|
|
im = Image.new(mode, (1, 1))
|
2023-09-26 13:14:22 +03:00
|
|
|
assert not im.has_transparency_data
|
2023-09-25 13:10:44 +03:00
|
|
|
|
|
|
|
for mode in ("LA", "La", "PA", "RGBA", "RGBa"):
|
|
|
|
im = Image.new(mode, (1, 1))
|
2023-09-26 13:14:22 +03:00
|
|
|
assert im.has_transparency_data
|
2023-09-25 13:10:44 +03:00
|
|
|
|
|
|
|
# P mode with "transparency" info
|
|
|
|
with Image.open("Tests/images/first_frame_transparency.gif") as im:
|
|
|
|
assert "transparency" in im.info
|
2023-09-26 13:14:22 +03:00
|
|
|
assert im.has_transparency_data
|
2023-09-25 13:10:44 +03:00
|
|
|
|
|
|
|
# RGB mode with "transparency" info
|
|
|
|
with Image.open("Tests/images/rgb_trns.png") as im:
|
|
|
|
assert "transparency" in im.info
|
2023-09-26 13:14:22 +03:00
|
|
|
assert im.has_transparency_data
|
2023-09-25 13:10:44 +03:00
|
|
|
|
|
|
|
# P mode with RGBA palette
|
|
|
|
im = Image.new("RGBA", (1, 1)).convert("P")
|
|
|
|
assert im.mode == "P"
|
2024-08-02 16:30:27 +03:00
|
|
|
assert im.palette is not None
|
2023-09-25 13:10:44 +03:00
|
|
|
assert im.palette.mode == "RGBA"
|
2023-09-26 13:14:22 +03:00
|
|
|
assert im.has_transparency_data
|
2023-09-25 13:10:44 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_apply_transparency(self) -> None:
|
2022-06-06 15:47:58 +03:00
|
|
|
im = Image.new("P", (1, 1))
|
|
|
|
im.putpalette((0, 0, 0, 1, 1, 1))
|
2024-08-02 16:30:27 +03:00
|
|
|
assert im.palette is not None
|
2022-06-06 15:47:58 +03:00
|
|
|
assert im.palette.colors == {(0, 0, 0): 0, (1, 1, 1): 1}
|
|
|
|
|
|
|
|
# Test that no transformation is applied without transparency
|
|
|
|
im.apply_transparency()
|
|
|
|
assert im.palette.colors == {(0, 0, 0): 0, (1, 1, 1): 1}
|
|
|
|
|
|
|
|
# Test that a transparency index is applied
|
|
|
|
im.info["transparency"] = 0
|
|
|
|
im.apply_transparency()
|
|
|
|
assert "transparency" not in im.info
|
|
|
|
assert im.palette.colors == {(0, 0, 0, 0): 0, (1, 1, 1, 255): 1}
|
|
|
|
|
|
|
|
# Test that existing transparency is kept
|
|
|
|
im = Image.new("P", (1, 1))
|
|
|
|
im.putpalette((0, 0, 0, 255, 1, 1, 1, 128), "RGBA")
|
|
|
|
im.info["transparency"] = 0
|
|
|
|
im.apply_transparency()
|
2024-08-02 16:30:27 +03:00
|
|
|
assert im.palette is not None
|
2022-06-06 15:47:58 +03:00
|
|
|
assert im.palette.colors == {(0, 0, 0, 0): 0, (1, 1, 1, 128): 1}
|
|
|
|
|
|
|
|
# Test that transparency bytes are applied
|
|
|
|
with Image.open("Tests/images/pil123p.png") as im:
|
|
|
|
assert isinstance(im.info["transparency"], bytes)
|
2024-08-02 16:30:27 +03:00
|
|
|
assert im.palette is not None
|
2022-06-06 15:47:58 +03:00
|
|
|
assert im.palette.colors[(27, 35, 6)] == 24
|
|
|
|
im.apply_transparency()
|
2024-08-02 16:30:27 +03:00
|
|
|
assert im.palette is not None
|
2022-06-06 15:47:58 +03:00
|
|
|
assert im.palette.colors[(27, 35, 6, 214)] == 24
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_constants(self) -> None:
|
2022-01-15 02:07:07 +03:00
|
|
|
for enum in (
|
|
|
|
Image.Transpose,
|
|
|
|
Image.Transform,
|
|
|
|
Image.Resampling,
|
|
|
|
Image.Dither,
|
|
|
|
Image.Palette,
|
|
|
|
Image.Quantize,
|
|
|
|
):
|
|
|
|
for name in enum.__members__:
|
2022-12-28 01:44:53 +03:00
|
|
|
assert getattr(Image, name) == enum[name]
|
2022-01-15 02:07:07 +03:00
|
|
|
|
2021-01-02 12:41:17 +03:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"path",
|
|
|
|
[
|
2019-12-21 10:38:22 +03:00
|
|
|
"fli_overrun.bin",
|
|
|
|
"sgi_overrun.bin",
|
2020-01-01 06:16:45 +03:00
|
|
|
"sgi_overrun_expandrow.bin",
|
|
|
|
"sgi_overrun_expandrow2.bin",
|
2019-12-21 10:38:22 +03:00
|
|
|
"pcx_overrun.bin",
|
|
|
|
"pcx_overrun2.bin",
|
2020-12-17 02:17:53 +03:00
|
|
|
"ossfuzz-4836216264589312.pcx",
|
2020-03-09 23:21:40 +03:00
|
|
|
"01r_00.pcx",
|
2021-01-02 12:41:17 +03:00
|
|
|
],
|
|
|
|
)
|
2024-02-12 13:06:17 +03:00
|
|
|
def test_overrun(self, path: str) -> None:
|
2020-12-17 02:17:53 +03:00
|
|
|
"""For overrun completeness, test as:
|
|
|
|
valgrind pytest -qq Tests/test_image.py::TestImage::test_overrun | grep decode.c
|
|
|
|
"""
|
|
|
|
with Image.open(os.path.join("Tests/images", path)) as im:
|
2024-09-14 13:59:02 +03:00
|
|
|
with pytest.raises(OSError) as e:
|
2020-12-17 02:17:53 +03:00
|
|
|
im.load()
|
2024-09-14 13:59:02 +03:00
|
|
|
buffer_overrun = str(e.value) == "buffer overrun when reading image file"
|
|
|
|
truncated = "image file is truncated" in str(e.value)
|
2021-01-02 12:41:17 +03:00
|
|
|
|
2024-09-14 13:59:02 +03:00
|
|
|
assert buffer_overrun or truncated
|
2019-09-30 11:45:43 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_fli_overrun2(self) -> None:
|
2020-01-02 07:23:36 +03:00
|
|
|
with Image.open("Tests/images/fli_overrun2.bin") as im:
|
2024-09-14 13:59:02 +03:00
|
|
|
with pytest.raises(OSError, match="buffer overrun when reading image file"):
|
2020-01-02 07:23:36 +03:00
|
|
|
im.seek(1)
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_exit_fp(self) -> None:
|
2023-11-24 07:19:19 +03:00
|
|
|
with Image.new("L", (1, 1)) as im:
|
|
|
|
pass
|
|
|
|
assert not hasattr(im, "fp")
|
|
|
|
|
2024-02-12 13:06:17 +03:00
|
|
|
def test_close_graceful(self, caplog: pytest.LogCaptureFixture) -> None:
|
2023-11-16 22:46:11 +03:00
|
|
|
with Image.open("Tests/images/hopper.jpg") as im:
|
|
|
|
copy = im.copy()
|
2023-11-17 00:37:46 +03:00
|
|
|
with caplog.at_level(logging.DEBUG):
|
|
|
|
im.close()
|
|
|
|
copy.close()
|
|
|
|
assert len(caplog.records) == 0
|
2023-11-16 22:46:11 +03:00
|
|
|
assert im.fp is None
|
|
|
|
|
2024-09-10 09:20:52 +03:00
|
|
|
def test_deprecation(self) -> None:
|
|
|
|
with pytest.warns(DeprecationWarning):
|
|
|
|
assert not Image.isImageType(None)
|
|
|
|
|
2017-08-31 16:18:59 +03:00
|
|
|
|
2022-12-26 03:43:39 +03:00
|
|
|
class TestImageBytes:
|
2024-05-12 14:20:46 +03:00
|
|
|
@pytest.mark.parametrize("mode", Image.MODES + ["BGR;15", "BGR;16", "BGR;24"])
|
2024-03-21 11:12:48 +03:00
|
|
|
def test_roundtrip_bytes_constructor(self, mode: str) -> None:
|
2024-03-21 11:11:19 +03:00
|
|
|
im = hopper(mode)
|
|
|
|
source_bytes = im.tobytes()
|
|
|
|
|
2024-04-15 12:28:52 +03:00
|
|
|
if mode.startswith("BGR;"):
|
|
|
|
with pytest.warns(DeprecationWarning):
|
|
|
|
reloaded = Image.frombytes(mode, im.size, source_bytes)
|
|
|
|
else:
|
|
|
|
reloaded = Image.frombytes(mode, im.size, source_bytes)
|
2024-03-21 11:11:19 +03:00
|
|
|
assert reloaded.tobytes() == source_bytes
|
2022-12-26 03:43:39 +03:00
|
|
|
|
2024-05-12 14:20:46 +03:00
|
|
|
@pytest.mark.parametrize("mode", Image.MODES + ["BGR;15", "BGR;16", "BGR;24"])
|
2024-03-21 11:12:48 +03:00
|
|
|
def test_roundtrip_bytes_method(self, mode: str) -> None:
|
2024-03-21 11:11:19 +03:00
|
|
|
im = hopper(mode)
|
|
|
|
source_bytes = im.tobytes()
|
|
|
|
|
2024-04-25 16:00:14 +03:00
|
|
|
reloaded = helper_image_new(mode, im.size)
|
2024-03-21 11:11:19 +03:00
|
|
|
reloaded.frombytes(source_bytes)
|
|
|
|
assert reloaded.tobytes() == source_bytes
|
2022-12-26 03:43:39 +03:00
|
|
|
|
2024-05-12 14:20:46 +03:00
|
|
|
@pytest.mark.parametrize("mode", Image.MODES + ["BGR;15", "BGR;16", "BGR;24"])
|
2024-04-06 18:48:38 +03:00
|
|
|
def test_getdata_putdata(self, mode: str) -> None:
|
2024-04-27 07:08:36 +03:00
|
|
|
if is_big_endian() and mode == "BGR;15":
|
2024-04-25 04:06:04 +03:00
|
|
|
pytest.xfail("Known failure of BGR;15 on big-endian")
|
2024-04-07 15:54:47 +03:00
|
|
|
im = hopper(mode)
|
2024-04-25 16:00:14 +03:00
|
|
|
reloaded = helper_image_new(mode, im.size)
|
2024-03-21 11:11:19 +03:00
|
|
|
reloaded.putdata(im.getdata())
|
|
|
|
assert_image_equal(im, reloaded)
|
2022-12-26 03:43:39 +03:00
|
|
|
|
|
|
|
|
2024-02-15 12:20:42 +03:00
|
|
|
class MockEncoder(ImageFile.PyEncoder):
|
|
|
|
pass
|
2017-03-11 20:03:09 +03:00
|
|
|
|
2017-04-20 14:14:23 +03:00
|
|
|
|
2020-02-23 00:03:01 +03:00
|
|
|
class TestRegistry:
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_encode_registry(self) -> None:
|
2024-02-15 12:20:42 +03:00
|
|
|
Image.register_encoder("MOCK", MockEncoder)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert "MOCK" in Image.ENCODERS
|
2017-03-11 20:03:09 +03:00
|
|
|
|
2019-06-13 18:54:24 +03:00
|
|
|
enc = Image._getencoder("RGB", "MOCK", ("args",), extra=("extra",))
|
2017-03-11 20:03:09 +03:00
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
assert isinstance(enc, MockEncoder)
|
2024-02-15 12:20:42 +03:00
|
|
|
assert enc.mode == "RGB"
|
|
|
|
assert enc.args == ("args", "extra")
|
2017-03-11 20:03:09 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_encode_registry_fail(self) -> None:
|
2020-04-07 09:58:21 +03:00
|
|
|
with pytest.raises(OSError):
|
2020-02-22 16:06:21 +03:00
|
|
|
Image._getencoder("RGB", "DoesNotExist", ("args",), extra=("extra",))
|