1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-02-12 15:42:40 +00:00

fix(python): prevent regeneration of JPEG homescreen image if not necessary

[no changelog]
This commit is contained in:
tychovrahe 2025-01-23 16:32:13 +01:00 committed by TychoVrahe
parent 3bdd7f661e
commit 25ce4e3fc9

View File

@ -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: 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: if filename.suffix in (".jpg", ".jpeg") and not PIL_AVAILABLE:
click.echo("Warning: Image library is missing, skipping image validation.") click.echo("Warning: Image library is missing, skipping image validation.")
return filename.read_bytes() return filename.read_bytes()
@ -144,6 +145,7 @@ def image_to_jpeg(filename: Path, width: int, height: int, quality: int = 90) ->
default=True, default=True,
): ):
image = image.resize((width, height), Image.Resampling.LANCZOS) image = image.resize((width, height), Image.Resampling.LANCZOS)
needs_regeneration = True
else: else:
raise click.ClickException( raise click.ClickException(
f"Wrong size of image - should be {width}x{height}" 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": if image.mode != "RGB":
image = image.convert("RGB") image = image.convert("RGB")
needs_regeneration = True
buf = io.BytesIO() if filename.suffix in (".jpg", ".jpeg"):
image.save(buf, format="jpeg", progressive=False, quality=quality) if image.info.get("progressive"): # pyright: ignore[reportAttributeAccessIssue]
return buf.getvalue() 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: def _should_remove(enable: Optional[bool], remove: bool) -> bool: