From 7224b00c513c6bc491f0270382efaf71c6cacc2a Mon Sep 17 00:00:00 2001 From: Alexander Piskun <13381981+bigcat88@users.noreply.github.com> Date: Wed, 8 May 2024 15:42:53 +0300 Subject: [PATCH] corrected processing of images in P mode with transparency=0 (#238) Signed-off-by: Alexander Piskun --- CHANGELOG.md | 6 ++++++ pillow_heif/misc.py | 2 +- tests/write_test.py | 18 +++++++++++++++++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bd91bf3e..3581b5f9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ All notable changes to this project will be documented in this file. +## [0.x.x - 2024-0x-xx] + +### Fixed + +- Processing of the images in `P` mode with `transparency` = 0. #238 + ## [0.16.0 - 2024-04-02] This release contains breaking change for monochrome images. diff --git a/pillow_heif/misc.py b/pillow_heif/misc.py index 9dee13a3..0267fda7 100644 --- a/pillow_heif/misc.py +++ b/pillow_heif/misc.py @@ -282,7 +282,7 @@ def _xmp_from_pillow(img: Image.Image) -> Optional[bytes]: def _pil_to_supported_mode(img: Image.Image) -> Image.Image: # We support "YCbCr" for encoding in Pillow plugin mode and do not call this function. if img.mode == "P": - mode = "RGBA" if img.info.get("transparency") else "RGB" + mode = "RGBA" if img.info.get("transparency", None) is not None else "RGB" img = img.convert(mode=mode) elif img.mode == "I": img = img.convert(mode="I;16L") diff --git a/tests/write_test.py b/tests/write_test.py index 2cdf0fe7..09b16cc0 100644 --- a/tests/write_test.py +++ b/tests/write_test.py @@ -8,7 +8,7 @@ import helpers import pytest from packaging.version import parse as parse_version -from PIL import Image, ImageSequence +from PIL import Image, ImageDraw, ImageSequence import pillow_heif @@ -594,6 +594,22 @@ def test_input_chroma_value(): assert im.info["chroma"] == 420 +@pytest.mark.parametrize("save_format", ("HEIF", "AVIF")) +def test_transparency_parameter(save_format): + im = Image.new("P", size=(64, 64)) + draw = ImageDraw.Draw(im) + draw.rectangle(xy=[(0, 0), (32, 32)], fill=255) + draw.rectangle(xy=[(32, 32), (64, 64)], fill=255) + + buf_png = BytesIO() + im.save(buf_png, format="PNG", transparency=0) + im_png = Image.open(buf_png) + buf_out = BytesIO() + im_png.save(buf_out, format=save_format, quality=-1) + + helpers.assert_image_equal(im_png.convert("RGBA"), Image.open(buf_out)) + + def test_invalid_encoder(): im_rgb = helpers.gradient_rgba() buf = BytesIO()