From 18d667db74d3f5873b3817df12c101b6d859465c Mon Sep 17 00:00:00 2001 From: Phil Date: Sat, 28 Nov 2026 18:57:08 -0700 Subject: [PATCH] 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 --- CHANGELOG.md | 6 +++ .../2026-11-28-animated-gif-uploads.md | 23 ++++++++++ starpunk/__init__.py | 4 +- starpunk/media.py | 33 ++++++++------ tests/test_media_upload.py | 43 +++++++++++++++---- 5 files changed, 86 insertions(+), 23 deletions(-) create mode 100644 docs/design/v2.21.1/2026-11-28-animated-gif-uploads.md diff --git a/CHANGELOG.md b/CHANGELOG.md index 235263b..f356dd8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [2.21.1] - 2026-11-28 + +### Fixed + +- Animated GIFs were stored as just their first frame, so they showed as still images. Upload optimization now keeps every frame and its timing. Animated GIFs also no longer get resized variants (thumb/small/medium/large), which would have been stills too; feeds and crossposts use the animated original. GIFs uploaded before this release stay still; upload them again + ## [2.21.0] - 2026-11-28 ### Added diff --git a/docs/design/v2.21.1/2026-11-28-animated-gif-uploads.md b/docs/design/v2.21.1/2026-11-28-animated-gif-uploads.md new file mode 100644 index 0000000..364d1b6 --- /dev/null +++ b/docs/design/v2.21.1/2026-11-28-animated-gif-uploads.md @@ -0,0 +1,23 @@ +# v2.21.1: Animated GIF uploads stay animated + +## Why + +https://starpunk.thesatelliteoflove.com/note/20261128164646 showed its GIF as +a still image. The stored file had one frame, but still carried the loop +extension and a 150 ms frame duration, so it had been animated when uploaded. + +`optimize_image()` notices animated GIFs so it can skip resizing, then +re-serialized them with `img.save(output, format='GIF')`. Without +`save_all=True` Pillow writes only the current frame. The one test for this, +`test_gif_animation_preserved`, used a single-frame GIF, so it couldn't catch it. + +## Changes + +| Area | Change | +|---|---| +| `media.optimize_image` | Animated GIFs are saved with `save_all=True`: every frame, per-frame durations and the loop count are kept | +| `media.save_media` | Animated GIFs get no thumb/small/medium/large variants. Those would be stills of the first frame, and syndication sends the `large` variant when there is one. With no variants, feeds and syndication use the original, as they already do for pre-v1.4.0 media | +| Tests | `test_gif_animation_preserved` uses a real 3-frame GIF with different durations; `test_save_animated_gif_stays_animated` checks the stored file and that no variants are made | + +Animated GIFs uploaded earlier are already stored as one frame and can't be +recovered. Upload them again to fix them. diff --git a/starpunk/__init__.py b/starpunk/__init__.py index 8b736d6..9899fa0 100644 --- a/starpunk/__init__.py +++ b/starpunk/__init__.py @@ -484,5 +484,5 @@ def create_app(config=None): # Package version (Semantic Versioning 2.0.0) # See docs/standards/versioning-strategy.md for details -__version__ = "2.21.0" -__version_info__ = (2, 21, 0) +__version__ = "2.21.1" +__version_info__ = (2, 21, 1) diff --git a/starpunk/media.py b/starpunk/media.py index 3853238..ef03177 100644 --- a/starpunk/media.py +++ b/starpunk/media.py @@ -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 diff --git a/tests/test_media_upload.py b/tests/test_media_upload.py index 078a822..477e191 100644 --- a/tests/test_media_upload.py +++ b/tests/test_media_upload.py @@ -47,6 +47,15 @@ def create_test_image(width=800, height=600, format='PNG'): return buffer.getvalue() +def create_animated_gif(width=244, height=134): + """A looping 3-frame GIF with a different duration per frame""" + frames = [Image.new('RGB', (width, height), color=c) for c in ('red', 'green', 'blue')] + buffer = io.BytesIO() + frames[0].save(buffer, format='GIF', save_all=True, append_images=frames[1:], + duration=[100, 200, 300], loop=0) + return buffer.getvalue() + + def create_test_heic(width=800, height=600): """ Generate test HEIC image using pillow-heif @@ -339,15 +348,20 @@ class TestImageOptimization: assert optimized_bytes is not None def test_gif_animation_preserved(self): - """Test GIF animation preservation (per Q12)""" - # For v1.2.0: Just verify GIF is handled without error - # Full animation preservation is complex - gif_data = create_test_image(800, 600, 'GIF') - optimized, width, height, optimized_bytes = optimize_image(gif_data) + """Every frame, its timing and the loop survive optimization (v2.21.1)""" + gif_data = create_animated_gif() + img, _, _, _ = validate_image(gif_data, 'anim.gif') + optimized, width, height, optimized_bytes = optimize_image(img, len(gif_data)) - assert width > 0 - assert height > 0 - assert optimized_bytes is not None + assert (width, height) == (244, 134) + out = Image.open(io.BytesIO(optimized_bytes)) + assert out.n_frames == 3 + assert out.info['loop'] == 0 + durations = [] + for i in range(out.n_frames): + out.seek(i) + durations.append(out.info['duration']) + assert durations == [100, 200, 300] class TestMediaSave: @@ -371,6 +385,19 @@ class TestMediaSave: media_path = Path(app.config['DATA_PATH']) / 'media' / media_info['path'] assert media_path.exists() + def test_save_animated_gif_stays_animated(self, app): + """The stored GIF keeps every frame, and no still variants are made (v2.21.1)""" + gif_data = create_animated_gif(1400, 800) # wide enough for every variant size + + with app.app_context(): + media_info = save_media(gif_data, 'anim.gif') + + media_dir = Path(app.config['DATA_PATH']) / 'media' + stored = Image.open(media_dir / media_info['path']) + assert stored.n_frames == 3 + assert media_info['variants'] == [] + assert not list((media_dir / media_info['path']).parent.glob('*_thumb.gif')) + def test_uuid_filename(self, app): """Test UUID-based filename generation (per Q5)""" image_data = create_test_image(800, 600, 'PNG')