fix: keep every frame of animated GIF uploads (v2.21.1)
optimize_image() re-saved animated GIFs without save_all=True, so Pillow wrote only the first frame. Save all frames, and skip resized variants for animated GIFs (they would be stills; feeds and syndication fall back to the original). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c87de892cf
commit
18d667db74
5 files changed
+86
-23
No files matched your search
+20
-13
@@ -316,10 +316,11 @@ def optimize_image(
|
||||
if original_format != 'GIF':
|
||||
img = ImageOps.exif_transpose(img)
|
||||
|
||||
# For animated GIFs, serialize and return (already validated in validate_image)
|
||||
# For animated GIFs, serialize every frame and return (already validated in
|
||||
# validate_image). Without save_all Pillow writes only the first frame (v2.21.1)
|
||||
if original_format == 'GIF' and getattr(img, 'is_animated', False):
|
||||
output = io.BytesIO()
|
||||
img.save(output, format='GIF')
|
||||
img.save(output, format='GIF', save_all=True)
|
||||
return img, img.size[0], img.size[1], output.getvalue()
|
||||
|
||||
# Iterative optimization loop
|
||||
@@ -687,17 +688,23 @@ def save_media(file_data: bytes, filename: str) -> Dict:
|
||||
temp_original_path.write_bytes(optimized_bytes)
|
||||
|
||||
# Step 2: Generate variants to temp directory
|
||||
variant_metadata, file_moves = generate_all_variants(
|
||||
optimized_img,
|
||||
full_dir,
|
||||
base_filename,
|
||||
file_ext,
|
||||
0, # media_id not yet known
|
||||
year,
|
||||
month,
|
||||
optimized_bytes,
|
||||
db
|
||||
)
|
||||
# Animated GIFs get none (v2.21.1): a resized variant would be a
|
||||
# still of the first frame, and feeds and syndication fall back to
|
||||
# the original when a media item has no variants
|
||||
if getattr(optimized_img, 'is_animated', False):
|
||||
variant_metadata, file_moves = [], []
|
||||
else:
|
||||
variant_metadata, file_moves = generate_all_variants(
|
||||
optimized_img,
|
||||
full_dir,
|
||||
base_filename,
|
||||
file_ext,
|
||||
0, # media_id not yet known
|
||||
year,
|
||||
month,
|
||||
optimized_bytes,
|
||||
db
|
||||
)
|
||||
timing_variants = time.monotonic() - t_start
|
||||
|
||||
# Step 3: Begin transaction and save to database
|
||||
|
||||
Reference in new issue
Block a user