2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
2024-01-20 14:23:03 +03:00
|
|
|
|
2018-06-15 23:01:06 +03:00
|
|
|
import os
|
|
|
|
from glob import glob
|
|
|
|
from itertools import product
|
2024-01-31 12:12:58 +03:00
|
|
|
from pathlib import Path
|
2018-06-15 23:01:06 +03:00
|
|
|
|
2020-02-03 12:11:32 +03:00
|
|
|
import pytest
|
2020-09-01 20:16:46 +03:00
|
|
|
|
2024-02-13 23:10:44 +03:00
|
|
|
from PIL import Image, UnidentifiedImageError
|
2014-08-20 10:32:06 +04:00
|
|
|
|
2021-04-09 14:38:28 +03:00
|
|
|
from .helper import assert_image_equal, assert_image_equal_tofile, hopper
|
2014-08-20 10:32:06 +04:00
|
|
|
|
2018-06-15 23:01:06 +03:00
|
|
|
_TGA_DIR = os.path.join("Tests", "images", "tga")
|
|
|
|
_TGA_DIR_COMMON = os.path.join(_TGA_DIR, "common")
|
|
|
|
|
|
|
|
|
2020-03-22 22:54:54 +03:00
|
|
|
_MODES = ("L", "LA", "P", "RGB", "RGBA")
|
|
|
|
_ORIGINS = ("tl", "bl")
|
2014-08-20 10:32:06 +04:00
|
|
|
|
2020-03-22 22:54:54 +03:00
|
|
|
_ORIGIN_TO_ORIENTATION = {"tl": 1, "bl": -1}
|
2018-06-15 23:01:06 +03:00
|
|
|
|
|
|
|
|
2022-08-23 14:41:32 +03:00
|
|
|
@pytest.mark.parametrize("mode", _MODES)
|
2024-02-20 07:41:20 +03:00
|
|
|
def test_sanity(mode: str, tmp_path: Path) -> None:
|
|
|
|
def roundtrip(original_im: Image.Image) -> None:
|
2022-08-23 14:41:32 +03:00
|
|
|
out = str(tmp_path / "temp.tga")
|
|
|
|
|
|
|
|
original_im.save(out, rle=rle)
|
|
|
|
with Image.open(out) as saved_im:
|
|
|
|
if rle:
|
|
|
|
assert saved_im.info["compression"] == original_im.info["compression"]
|
|
|
|
assert saved_im.info["orientation"] == original_im.info["orientation"]
|
|
|
|
if mode == "P":
|
|
|
|
assert saved_im.getpalette() == original_im.getpalette()
|
|
|
|
|
|
|
|
assert_image_equal(saved_im, original_im)
|
|
|
|
|
|
|
|
png_paths = glob(os.path.join(_TGA_DIR_COMMON, f"*x*_{mode.lower()}.png"))
|
|
|
|
|
|
|
|
for png_path in png_paths:
|
|
|
|
with Image.open(png_path) as reference_im:
|
|
|
|
assert reference_im.mode == mode
|
|
|
|
|
|
|
|
path_no_ext = os.path.splitext(png_path)[0]
|
|
|
|
for origin, rle in product(_ORIGINS, (True, False)):
|
|
|
|
tga_path = "{}_{}_{}.tga".format(
|
|
|
|
path_no_ext, origin, "rle" if rle else "raw"
|
|
|
|
)
|
|
|
|
|
|
|
|
with Image.open(tga_path) as original_im:
|
|
|
|
assert original_im.format == "TGA"
|
|
|
|
assert original_im.get_format_mimetype() == "image/x-tga"
|
|
|
|
if rle:
|
|
|
|
assert original_im.info["compression"] == "tga_rle"
|
2020-03-22 22:54:54 +03:00
|
|
|
assert (
|
2022-08-23 14:41:32 +03:00
|
|
|
original_im.info["orientation"]
|
|
|
|
== _ORIGIN_TO_ORIENTATION[origin]
|
2020-03-22 22:54:54 +03:00
|
|
|
)
|
2022-08-23 14:41:32 +03:00
|
|
|
if mode == "P":
|
|
|
|
assert original_im.getpalette() == reference_im.getpalette()
|
2015-07-03 09:22:56 +03:00
|
|
|
|
2022-08-23 14:41:32 +03:00
|
|
|
assert_image_equal(original_im, reference_im)
|
2018-09-01 16:31:39 +03:00
|
|
|
|
2022-08-23 14:41:32 +03:00
|
|
|
roundtrip(original_im)
|
2018-09-08 01:26:32 +03:00
|
|
|
|
2018-09-01 16:31:39 +03:00
|
|
|
|
2024-02-13 23:10:44 +03:00
|
|
|
def test_palette_depth_8(tmp_path: Path) -> None:
|
|
|
|
with pytest.raises(UnidentifiedImageError):
|
|
|
|
Image.open("Tests/images/p_8.tga")
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_palette_depth_16(tmp_path: Path) -> None:
|
2021-04-10 15:11:20 +03:00
|
|
|
with Image.open("Tests/images/p_16.tga") as im:
|
2024-06-25 13:50:17 +03:00
|
|
|
assert im.palette.mode == "RGBA"
|
|
|
|
assert_image_equal_tofile(im.convert("RGBA"), "Tests/images/p_16.png")
|
2021-04-10 15:11:20 +03:00
|
|
|
|
2021-06-19 02:16:05 +03:00
|
|
|
out = str(tmp_path / "temp.png")
|
|
|
|
im.save(out)
|
|
|
|
with Image.open(out) as reloaded:
|
2024-06-25 13:50:17 +03:00
|
|
|
assert_image_equal_tofile(reloaded.convert("RGBA"), "Tests/images/p_16.png")
|
2021-06-19 02:16:05 +03:00
|
|
|
|
2021-04-10 15:11:20 +03:00
|
|
|
|
2024-06-26 11:06:47 +03:00
|
|
|
def test_rgba_16() -> None:
|
|
|
|
with Image.open("Tests/images/rgba16.tga") as im:
|
|
|
|
assert im.mode == "RGBA"
|
|
|
|
|
|
|
|
assert im.getpixel((0, 0)) == (172, 0, 255, 255)
|
|
|
|
assert im.getpixel((1, 0)) == (0, 255, 82, 0)
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_id_field() -> None:
|
2020-03-22 22:54:54 +03:00
|
|
|
# tga file with id field
|
|
|
|
test_file = "Tests/images/tga_id_field.tga"
|
2018-09-01 16:31:39 +03:00
|
|
|
|
2020-03-22 22:54:54 +03:00
|
|
|
# Act
|
|
|
|
with Image.open(test_file) as im:
|
|
|
|
# Assert
|
|
|
|
assert im.size == (100, 100)
|
2018-09-01 16:31:39 +03:00
|
|
|
|
2016-04-21 17:40:20 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_id_field_rle() -> None:
|
2020-03-22 22:54:54 +03:00
|
|
|
# tga file with id field
|
|
|
|
test_file = "Tests/images/rgb32rle.tga"
|
2016-04-21 17:40:20 +03:00
|
|
|
|
2020-03-22 22:54:54 +03:00
|
|
|
# Act
|
|
|
|
with Image.open(test_file) as im:
|
|
|
|
# Assert
|
|
|
|
assert im.size == (199, 199)
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_cross_scan_line() -> None:
|
2022-02-23 07:08:33 +03:00
|
|
|
with Image.open("Tests/images/cross_scan_line.tga") as im:
|
|
|
|
assert_image_equal_tofile(im, "Tests/images/cross_scan_line.png")
|
|
|
|
|
2022-05-15 06:51:31 +03:00
|
|
|
with Image.open("Tests/images/cross_scan_line_truncated.tga") as im:
|
|
|
|
with pytest.raises(OSError):
|
|
|
|
im.load()
|
|
|
|
|
2022-02-23 07:08:33 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_save(tmp_path: Path) -> None:
|
2020-03-22 22:54:54 +03:00
|
|
|
test_file = "Tests/images/tga_id_field.tga"
|
|
|
|
with Image.open(test_file) as im:
|
|
|
|
out = str(tmp_path / "temp.tga")
|
|
|
|
|
|
|
|
# Save
|
|
|
|
im.save(out)
|
Improve handling of file resources
Follow Python's file object semantics. User code is responsible for
closing resources (usually through a context manager) in a deterministic
way.
To achieve this, remove __del__ functions. These functions used to
closed open file handlers in an attempt to silence Python
ResourceWarnings. However, using __del__ has the following drawbacks:
- __del__ isn't called until the object's reference count reaches 0.
Therefore, resource handlers remain open or in use longer than
necessary.
- The __del__ method isn't guaranteed to execute on system exit. See the
Python documentation:
https://docs.python.org/3/reference/datamodel.html#object.__del__
> It is not guaranteed that __del__() methods are called for objects
> that still exist when the interpreter exits.
- Exceptions that occur inside __del__ are ignored instead of raised.
This has the potential of hiding bugs. This is also in the Python
documentation:
> Warning: Due to the precarious circumstances under which __del__()
> methods are invoked, exceptions that occur during their execution
> are ignored, and a warning is printed to sys.stderr instead.
Instead, always close resource handlers when they are no longer in use.
This will close the file handler at a specified point in the user's code
and not wait until the interpreter chooses to. It is always guaranteed
to run. And, if an exception occurs while closing the file handler, the
bug will not be ignored.
Now, when code receives a ResourceWarning, it will highlight an area
that is mishandling resources. It should not simply be silenced, but
fixed by closing resources with a context manager.
All warnings that were emitted during tests have been cleaned up. To
enable warnings, I passed the `-Wa` CLI option to Python. This exposed
some mishandling of resources in ImageFile.__init__() and
SpiderImagePlugin.loadImageSeries(), they too were fixed.
2019-05-25 19:30:58 +03:00
|
|
|
with Image.open(out) as test_im:
|
2020-03-22 22:54:54 +03:00
|
|
|
assert test_im.size == (100, 100)
|
|
|
|
assert test_im.info["id_section"] == im.info["id_section"]
|
2016-04-21 17:40:20 +03:00
|
|
|
|
|
|
|
# RGBA save
|
2018-09-01 16:31:39 +03:00
|
|
|
im.convert("RGBA").save(out)
|
2020-03-22 22:54:54 +03:00
|
|
|
with Image.open(out) as test_im:
|
|
|
|
assert test_im.size == (100, 100)
|
2016-04-21 17:40:20 +03:00
|
|
|
|
2018-09-01 16:31:39 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_small_palette(tmp_path: Path) -> None:
|
2022-08-13 12:46:07 +03:00
|
|
|
im = Image.new("P", (1, 1))
|
|
|
|
colors = [0, 0, 0]
|
|
|
|
im.putpalette(colors)
|
|
|
|
|
|
|
|
out = str(tmp_path / "temp.tga")
|
|
|
|
im.save(out)
|
|
|
|
|
|
|
|
with Image.open(out) as reloaded:
|
|
|
|
assert reloaded.getpalette() == colors
|
|
|
|
|
|
|
|
|
2024-02-13 22:55:13 +03:00
|
|
|
def test_missing_palette() -> None:
|
|
|
|
with Image.open("Tests/images/dilation4.lut") as im:
|
|
|
|
assert im.mode == "L"
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_save_wrong_mode(tmp_path: Path) -> None:
|
2020-03-22 22:54:54 +03:00
|
|
|
im = hopper("PA")
|
|
|
|
out = str(tmp_path / "temp.tga")
|
2018-09-01 16:31:39 +03:00
|
|
|
|
2020-03-22 22:54:54 +03:00
|
|
|
with pytest.raises(OSError):
|
|
|
|
im.save(out)
|
2018-06-14 12:18:08 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_save_mapdepth() -> None:
|
2021-04-09 14:38:28 +03:00
|
|
|
# This image has been manually hexedited from 200x32_p_bl_raw.tga
|
|
|
|
# to include an origin
|
|
|
|
test_file = "Tests/images/200x32_p_bl_raw_origin.tga"
|
|
|
|
with Image.open(test_file) as im:
|
|
|
|
assert_image_equal_tofile(im, "Tests/images/tga/common/200x32_p.png")
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_save_id_section(tmp_path: Path) -> None:
|
2020-03-22 22:54:54 +03:00
|
|
|
test_file = "Tests/images/rgb32rle.tga"
|
|
|
|
with Image.open(test_file) as im:
|
|
|
|
out = str(tmp_path / "temp.tga")
|
2018-06-14 12:18:08 +03:00
|
|
|
|
2020-03-22 22:54:54 +03:00
|
|
|
# Check there is no id section
|
|
|
|
im.save(out)
|
|
|
|
with Image.open(out) as test_im:
|
|
|
|
assert "id_section" not in test_im.info
|
|
|
|
|
|
|
|
# Save with custom id section
|
|
|
|
im.save(out, id_section=b"Test content")
|
|
|
|
with Image.open(out) as test_im:
|
|
|
|
assert test_im.info["id_section"] == b"Test content"
|
|
|
|
|
|
|
|
# Save with custom id section greater than 255 characters
|
|
|
|
id_section = b"Test content" * 25
|
2023-02-23 16:30:38 +03:00
|
|
|
with pytest.warns(UserWarning):
|
|
|
|
im.save(out, id_section=id_section)
|
|
|
|
|
2020-03-22 22:54:54 +03:00
|
|
|
with Image.open(out) as test_im:
|
|
|
|
assert test_im.info["id_section"] == id_section[:255]
|
|
|
|
|
|
|
|
test_file = "Tests/images/tga_id_field.tga"
|
|
|
|
with Image.open(test_file) as im:
|
|
|
|
# Save with no id section
|
|
|
|
im.save(out, id_section="")
|
|
|
|
with Image.open(out) as test_im:
|
|
|
|
assert "id_section" not in test_im.info
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_save_orientation(tmp_path: Path) -> None:
|
2020-03-22 22:54:54 +03:00
|
|
|
test_file = "Tests/images/rgb32rle.tga"
|
|
|
|
out = str(tmp_path / "temp.tga")
|
|
|
|
with Image.open(test_file) as im:
|
|
|
|
assert im.info["orientation"] == -1
|
|
|
|
|
|
|
|
im.save(out, orientation=1)
|
|
|
|
with Image.open(out) as test_im:
|
|
|
|
assert test_im.info["orientation"] == 1
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_horizontal_orientations() -> None:
|
2021-11-11 13:53:28 +03:00
|
|
|
# These images have been manually hexedited to have the relevant orientations
|
|
|
|
with Image.open("Tests/images/rgb32rle_top_right.tga") as im:
|
|
|
|
assert im.load()[90, 90][:3] == (0, 0, 0)
|
|
|
|
|
|
|
|
with Image.open("Tests/images/rgb32rle_bottom_right.tga") as im:
|
|
|
|
assert im.load()[90, 90][:3] == (0, 255, 0)
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_save_rle(tmp_path: Path) -> None:
|
2020-03-22 22:54:54 +03:00
|
|
|
test_file = "Tests/images/rgb32rle.tga"
|
|
|
|
with Image.open(test_file) as im:
|
|
|
|
assert im.info["compression"] == "tga_rle"
|
|
|
|
|
|
|
|
out = str(tmp_path / "temp.tga")
|
|
|
|
|
|
|
|
# Save
|
|
|
|
im.save(out)
|
|
|
|
with Image.open(out) as test_im:
|
|
|
|
assert test_im.size == (199, 199)
|
|
|
|
assert test_im.info["compression"] == "tga_rle"
|
|
|
|
|
|
|
|
# Save without compression
|
|
|
|
im.save(out, compression=None)
|
|
|
|
with Image.open(out) as test_im:
|
|
|
|
assert "compression" not in test_im.info
|
|
|
|
|
|
|
|
# RGBA save
|
|
|
|
im.convert("RGBA").save(out)
|
|
|
|
with Image.open(out) as test_im:
|
|
|
|
assert test_im.size == (199, 199)
|
|
|
|
|
|
|
|
test_file = "Tests/images/tga_id_field.tga"
|
|
|
|
with Image.open(test_file) as im:
|
|
|
|
assert "compression" not in im.info
|
|
|
|
|
|
|
|
# Save with compression
|
|
|
|
im.save(out, compression="tga_rle")
|
|
|
|
with Image.open(out) as test_im:
|
|
|
|
assert test_im.info["compression"] == "tga_rle"
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_save_l_transparency(tmp_path: Path) -> None:
|
2020-03-22 22:54:54 +03:00
|
|
|
# There are 559 transparent pixels in la.tga.
|
|
|
|
num_transparent = 559
|
|
|
|
|
|
|
|
in_file = "Tests/images/la.tga"
|
|
|
|
with Image.open(in_file) as im:
|
|
|
|
assert im.mode == "LA"
|
|
|
|
assert im.getchannel("A").getcolors()[0][0] == num_transparent
|
|
|
|
|
|
|
|
out = str(tmp_path / "temp.tga")
|
|
|
|
im.save(out)
|
|
|
|
|
|
|
|
with Image.open(out) as test_im:
|
|
|
|
assert test_im.mode == "LA"
|
|
|
|
assert test_im.getchannel("A").getcolors()[0][0] == num_transparent
|
2018-06-14 12:18:08 +03:00
|
|
|
|
2020-03-22 22:54:54 +03:00
|
|
|
assert_image_equal(im, test_im)
|