Pillow/Tests/test_file_webp_alpha.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

154 lines
4.2 KiB
Python
Raw Normal View History

from __future__ import annotations
2024-01-20 14:23:03 +03:00
from pathlib import Path
import pytest
2014-01-21 01:05:30 +04:00
from PIL import Image
from .helper import (
assert_image_equal,
assert_image_similar,
assert_image_similar_tofile,
hopper,
)
_webp = pytest.importorskip("PIL._webp", reason="WebP support not installed")
2014-01-21 01:05:30 +04:00
def setup_module() -> None:
2020-02-18 16:50:34 +03:00
if _webp.WebPDecoderBuggyAlpha():
pytest.skip("Buggy early version of WebP installed, not testing transparency")
2014-01-21 01:05:30 +04:00
def test_read_rgba() -> None:
2020-02-18 16:50:34 +03:00
"""
Can we read an RGBA mode file without error?
Does it have the bits we expect?
"""
2014-01-21 01:05:30 +04:00
2020-02-18 16:50:34 +03:00
# Generated with `cwebp transparent.png -o transparent.webp`
file_path = "Tests/images/transparent.webp"
with Image.open(file_path) as image:
assert image.mode == "RGBA"
assert image.size == (200, 150)
assert image.format == "WEBP"
image.load()
image.getdata()
2014-01-21 01:05:30 +04:00
2020-02-18 16:50:34 +03:00
image.tobytes()
2014-01-21 01:05:30 +04:00
assert_image_similar_tofile(image, "Tests/images/transparent.png", 20.0)
2014-01-21 01:05:30 +04:00
def test_write_lossless_rgb(tmp_path: Path) -> None:
2020-02-18 16:50:34 +03:00
"""
Can we write an RGBA mode file with lossless compression without error?
Does it have the bits we expect?
"""
2014-01-21 01:05:30 +04:00
2020-02-18 16:50:34 +03:00
temp_file = str(tmp_path / "temp.webp")
# temp_file = "temp.webp"
2014-01-21 01:05:30 +04:00
2020-02-18 16:50:34 +03:00
pil_image = hopper("RGBA")
2014-01-21 01:05:30 +04:00
2020-02-18 16:50:34 +03:00
mask = Image.new("RGBA", (64, 64), (128, 128, 128, 128))
# Add some partially transparent bits:
pil_image.paste(mask, (0, 0), mask)
2014-01-21 01:05:30 +04:00
2020-02-18 16:50:34 +03:00
pil_image.save(temp_file, lossless=True)
2014-01-21 01:05:30 +04:00
2020-02-18 16:50:34 +03:00
with Image.open(temp_file) as image:
image.load()
2014-01-21 01:05:30 +04:00
2020-02-18 16:50:34 +03:00
assert image.mode == "RGBA"
assert image.size == pil_image.size
assert image.format == "WEBP"
image.load()
image.getdata()
2014-01-21 01:05:30 +04:00
2020-02-18 16:50:34 +03:00
assert_image_equal(image, pil_image)
2014-01-21 01:05:30 +04:00
def test_write_rgba(tmp_path: Path) -> None:
2020-02-18 16:50:34 +03:00
"""
Can we write a RGBA mode file to WebP without error.
Does it have the bits we expect?
"""
2014-01-21 01:05:30 +04:00
2020-02-18 16:50:34 +03:00
temp_file = str(tmp_path / "temp.webp")
2014-01-21 01:05:30 +04:00
2020-02-18 16:50:34 +03:00
pil_image = Image.new("RGBA", (10, 10), (255, 0, 0, 20))
pil_image.save(temp_file)
2014-01-21 01:05:30 +04:00
2020-02-18 16:50:34 +03:00
if _webp.WebPDecoderBuggyAlpha():
return
2014-01-21 01:05:30 +04:00
2020-02-18 16:50:34 +03:00
with Image.open(temp_file) as image:
image.load()
2020-02-18 16:50:34 +03:00
assert image.mode == "RGBA"
assert image.size == (10, 10)
assert image.format == "WEBP"
image.load()
image.getdata()
# Early versions of WebP are known to produce higher deviations:
# deal with it
if _webp.WebPDecoderVersion() <= 0x201:
assert_image_similar(image, pil_image, 3.0)
else:
assert_image_similar(image, pil_image, 1.0)
def test_keep_rgb_values_when_transparent(tmp_path: Path) -> None:
2022-11-18 00:58:07 +03:00
"""
Saving transparent pixels should retain their original RGB values
when using the "exact" parameter.
2022-11-18 00:58:07 +03:00
"""
image = hopper("RGB")
2022-11-18 00:58:07 +03:00
# create a copy of the image
# with the left half transparent
half_transparent_image = image.copy()
new_alpha = Image.new("L", (128, 128), 255)
new_alpha.paste(0, (0, 0, 64, 128))
half_transparent_image.putalpha(new_alpha)
2022-11-18 00:58:07 +03:00
# save with transparent area preserved
temp_file = str(tmp_path / "temp.webp")
half_transparent_image.save(temp_file, exact=True, lossless=True)
2022-11-18 00:58:07 +03:00
with Image.open(temp_file) as reloaded:
assert reloaded.mode == "RGBA"
assert reloaded.format == "WEBP"
2022-11-18 00:58:07 +03:00
# even though it is lossless, if we don't use exact=True
# in libwebp >= 0.5, the transparent area will be filled with black
# (or something more conducive to compression)
2022-11-19 09:18:27 +03:00
assert_image_equal(reloaded.convert("RGB"), image)
2022-11-18 00:58:07 +03:00
def test_write_unsupported_mode_PA(tmp_path: Path) -> None:
2020-02-18 16:50:34 +03:00
"""
Saving a palette-based file with transparency to WebP format
should work, and be similar to the original file.
"""
temp_file = str(tmp_path / "temp.webp")
file_path = "Tests/images/transparent.gif"
with Image.open(file_path) as im:
im.save(temp_file)
with Image.open(temp_file) as image:
assert image.mode == "RGBA"
assert image.size == (200, 150)
assert image.format == "WEBP"
image.load()
image.getdata()
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(file_path) as im:
2020-02-18 16:50:34 +03:00
target = im.convert("RGBA")
assert_image_similar(image, target, 25.0)