From e9862529c0aa0f3197f9f8bdaf38cf4b8adacad0 Mon Sep 17 00:00:00 2001 From: Aleksandr Karpinskii Date: Mon, 19 Aug 2024 13:07:20 +0400 Subject: [PATCH] Fix buggy_la_mode --- HeifImagePlugin.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/HeifImagePlugin.py b/HeifImagePlugin.py index e79f97f..e0b0d7f 100644 --- a/HeifImagePlugin.py +++ b/HeifImagePlugin.py @@ -166,6 +166,9 @@ def check_heif_magic(data): return pyheif.check(data) != pyheif.heif_filetype_no +is_buggy_la_mode = '1.17.0' <= pyheif.libheif_version() <= '1.18.2' + + def _save(im, fp, filename): # Save it before subsequent im.save() call info = im.encoderinfo @@ -174,10 +177,14 @@ def _save(im, fp, filename): # disbled due to errors in libheif encoder raise IOError("cannot write mode P as HEIF") + if im.mode == '1': + # to circumvent `heif-enc` bug + im = im.convert('L') + + if im.mode == 'LA' and is_buggy_la_mode: + im = im.convert('RGBA') + with tempfile.NamedTemporaryFile(suffix='.png') as tmpfile: - if im.mode == '1': - # to circumvent `heif-enc` bug - im = im.convert('L') im.save( tmpfile, format='PNG', optimize=False, compress_level=0, icc_profile=info.get('icc_profile', im.info.get('icc_profile')),