From 58f14c2bb0695f7bb9755b37921ecb2d12d76e00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Kerbiriou?= Date: Fri, 11 Oct 2024 05:10:34 +0200 Subject: [PATCH] Use the mode from the jpeg stream (#70) * disable jpeg encode warning when lossless_jpeg=True * use the mode from the reconstructed jpeg --- pillow_jxl/JpegXLImagePlugin.py | 27 +++++++++++++++++---------- test/test_plugin.py | 2 +- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/pillow_jxl/JpegXLImagePlugin.py b/pillow_jxl/JpegXLImagePlugin.py index 158738c..58f4e69 100644 --- a/pillow_jxl/JpegXLImagePlugin.py +++ b/pillow_jxl/JpegXLImagePlugin.py @@ -33,9 +33,15 @@ def _open(self): if self.jpeg: with Image.open(BytesIO(self._data)) as im: self._data = im.tobytes() - self._size = (self._jxlinfo.width, self._jxlinfo.height) - self.rawmode = self._jxlinfo.mode - self.info["icc_profile"] = icc_profile + self._size = im.size + self.rawmode = im.mode + self.info = im.info + icc_profile = im.info.get("icc_profile", icc_profile) + else: + self._size = (self._jxlinfo.width, self._jxlinfo.height) + self.rawmode = self._jxlinfo.mode + if icc_profile: + self.info["icc_profile"] = icc_profile # NOTE (Isotr0py): PIL 10.1.0 changed the mode to property, use _mode instead if parse(PIL.__version__) >= parse("10.1.0"): self._mode = self.rawmode @@ -98,7 +104,7 @@ def _save(im, fp, filename, save_all=False): effort = info.get("effort", 7) use_container = info.get("use_container", False) use_original_profile = info.get("use_original_profile", False) - jpeg_encode = info.get("lossless_jpeg", True) + jpeg_encode = info.get("lossless_jpeg", None) num_threads = info.get("num_threads", -1) enc = Encoder( @@ -113,12 +119,13 @@ def _save(im, fp, filename, save_all=False): ) # FIXME (Isotr0py): im.filename maybe None if parse stream # TODO (Isotr0py): This part should be refactored in the near future - if im.format == "JPEG" and im.filename and jpeg_encode: - warnings.warn( - "Using JPEG reconstruction to create lossless JXL image from JPEG. " - "This is the default behavior for JPEG encode, if you want to " - "disable this, please set 'lossless_jpeg' to False." - ) + if im.format == "JPEG" and im.filename and (jpeg_encode or jpeg_encode is None): + if jpeg_encode is None: + warnings.warn( + "Using JPEG reconstruction to create lossless JXL image from JPEG. " + "This is the default behavior for JPEG encode, if you want to " + "disable this, please set 'lossless_jpeg'." + ) with open(im.filename, "rb") as f: data = enc(f.read(), im.width, im.height, jpeg_encode=True) else: diff --git a/test/test_plugin.py b/test/test_plugin.py index a94db8a..66c0f28 100644 --- a/test/test_plugin.py +++ b/test/test_plugin.py @@ -32,7 +32,7 @@ def test_encode(image): def test_jpeg_encode(): temp = tempfile.mktemp(suffix=".jxl") img_ori = Image.open("test/images/sample.jpg") - img_ori.save(temp, lossless=True) + img_ori.save(temp, lossless=True, lossless_jpeg=True) img_enc = Image.open(temp) assert img_ori.size == img_enc.size == (40, 50)