Files
romm/backend/utils/images.py
zurdi 0318f078ae refactor(covers): simplify animated cover handling (simplify)
Simplify pass over the animated cover change:
- Encode animated small covers as WebP whatever the source, which drops
  the per-format GIF/APNG blending params and makes their WebP copy a
  plain file copy.
- Fold the is_animated gate into frame_durations and decode the small
  cover's frames in the same pass that reads their durations.
- Write uploaded animated artwork through the atomic write_file.
- Share test fixture constants and the animation encoder, and pin the
  per-frame pixel budget with a GIF whose canvas grows mid-animation.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-18 11:00:14 +02:00

69 lines
2.2 KiB
Python

from collections.abc import Callable
from PIL import Image, ImageSequence
# Multi-frame formats browsers play as animations (MPO/TIFF pages are not)
ANIMATED_FORMATS = frozenset({"GIF", "PNG", "WEBP"})
# Pixels decoded across all frames past which an animation keeps only its
# first frame, since re-encoding one holds every frame in memory.
MAX_ANIMATION_PIXELS = 100_000_000
# Largest loop count a WebP animation can store
_MAX_WEBP_LOOP = 0xFFFF
def is_animated(img: Image.Image) -> bool:
"""True for a multi-frame image that browsers play as an animation."""
n_frames = getattr(img, "n_frames", 1)
return (
img.format in ANIMATED_FORMATS
and 1 < n_frames
and n_frames * img.width * img.height <= MAX_ANIMATION_PIXELS
)
def frame_durations(
img: Image.Image, on_frame: Callable[[Image.Image], None] | None = None
) -> list[float] | None:
"""Display time in milliseconds of each frame, leaving img on its first frame.
Args:
on_frame: Called with each frame once decoded.
Returns:
None for a still image, or an animation that fails to decode or
outgrows MAX_ANIMATION_PIXELS.
"""
if not is_animated(img):
return None
durations: list[float] = []
pixels = 0
try:
for frame in ImageSequence.Iterator(img):
# Counted per frame since a GIF canvas can grow mid-animation
pixels += frame.width * frame.height
if pixels > MAX_ANIMATION_PIXELS:
return None
# WebP only fills in a frame's duration once the frame is decoded
frame.load()
if on_frame:
on_frame(frame)
durations.append(frame.info.get("duration", 0))
except OSError, SyntaxError, ValueError, Image.DecompressionBombError:
return None
finally:
img.seek(0)
return durations
def webp_loop(img: Image.Image) -> int:
"""WebP loop count (0 is forever) that plays img as often as browsers do."""
loop = img.info.get("loop")
if img.format != "GIF":
return loop or 0
# A GIF counts replays after the first play, and plays once without a count
if loop is None:
return 1
return min(loop + 1, _MAX_WEBP_LOOP) if loop else 0