Skip to content

Commit

Permalink
corrected processing of images in P mode with transparency=0 (#238)
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Piskun <[email protected]>
  • Loading branch information
bigcat88 authored May 8, 2024
1 parent deb60e3 commit 7224b00
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion pillow_heif/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
18 changes: 17 additions & 1 deletion tests/write_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 7224b00

Please sign in to comment.