Skip to content

Commit

Permalink
fix palette images with bytes transparency conversion (#21)
Browse files Browse the repository at this point in the history
Co-authored-by: Iaroslav Fadeev <[email protected]>
  • Loading branch information
Jarikf and Iaroslav Fadeev authored Apr 16, 2022
1 parent 41a4704 commit 3528220
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
3 changes: 2 additions & 1 deletion pillow_heif/heif.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,8 @@ def add_from_pillow(self, pil_image: Image, load_one=False):
if k in frame.info:
additional_info[k] = frame.info[k]
if frame.mode == "P":
frame = frame.convert(mode="RGB")
mode = 'RGBA' if frame.info.get('transparency') else 'RGB'
frame = frame.convert(mode=mode)
# How here we can detect bit depth of Pillow image? pallete.rawmode or maybe something else?
__bit_depth = 8
self._add_frombytes(__bit_depth, frame.mode, frame.size, frame.tobytes(), add_info={**additional_info})
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 13 additions & 1 deletion tests/opener_encoder_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest
from PIL import Image, ImageSequence

from pillow_heif import options, register_heif_opener
from pillow_heif import options, register_heif_opener, from_pillow

imagehash = pytest.importorskip("hashes_test", reason="NumPy not installed")

Expand Down Expand Up @@ -102,3 +102,15 @@ def test_alpha_channel():
heic_pillow = Image.open(out_heic)
for i, frame in enumerate(ImageSequence.Iterator(png_pillow)):
compare_hashes([ImageSequence.Iterator(heic_pillow)[i], frame])


@pytest.mark.skipif(not options().hevc_enc, reason="No HEVC encoder.")
def test_palette_with_bytes_tranparency():
png_pillow = Image.open(
Path("images/jpeg_gif_png/palette_with_bytes_transparency.png"))
heif_file = from_pillow(png_pillow)
out_heic = BytesIO()
heif_file.save(out_heic, format="HEIF", quality=90, save_all=True)
heic_pillow = Image.open(out_heic)
assert heic_pillow.heif_file.has_alpha is True
assert heic_pillow.mode == "RGBA"

0 comments on commit 3528220

Please sign in to comment.