From 25ce4e3fc9fd409e9fcfbad1a3562c862977bb85 Mon Sep 17 00:00:00 2001 From: tychovrahe Date: Thu, 23 Jan 2025 16:32:13 +0100 Subject: [PATCH] fix(python): prevent regeneration of JPEG homescreen image if not necessary [no changelog] --- python/src/trezorlib/cli/settings.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/python/src/trezorlib/cli/settings.py b/python/src/trezorlib/cli/settings.py index 946f3fbffc..00e4178c44 100644 --- a/python/src/trezorlib/cli/settings.py +++ b/python/src/trezorlib/cli/settings.py @@ -124,6 +124,7 @@ def image_to_toif(filename: Path, width: int, height: int, greyscale: bool) -> b def image_to_jpeg(filename: Path, width: int, height: int, quality: int = 90) -> bytes: + needs_regeneration = False if filename.suffix in (".jpg", ".jpeg") and not PIL_AVAILABLE: click.echo("Warning: Image library is missing, skipping image validation.") return filename.read_bytes() @@ -144,6 +145,7 @@ def image_to_jpeg(filename: Path, width: int, height: int, quality: int = 90) -> default=True, ): image = image.resize((width, height), Image.Resampling.LANCZOS) + needs_regeneration = True else: raise click.ClickException( f"Wrong size of image - should be {width}x{height}" @@ -151,10 +153,18 @@ def image_to_jpeg(filename: Path, width: int, height: int, quality: int = 90) -> if image.mode != "RGB": image = image.convert("RGB") + needs_regeneration = True - buf = io.BytesIO() - image.save(buf, format="jpeg", progressive=False, quality=quality) - return buf.getvalue() + if filename.suffix in (".jpg", ".jpeg"): + if image.info.get("progressive"): # pyright: ignore[reportAttributeAccessIssue] + needs_regeneration = True + + if needs_regeneration: + buf = io.BytesIO() + image.save(buf, format="jpeg", progressive=False, quality=quality) + return buf.getvalue() + else: + return filename.read_bytes() def _should_remove(enable: Optional[bool], remove: bool) -> bool: