Skip to content

Add encoding for animated PNGs#3039

Open
RunDevelopment wants to merge 2 commits into
image-rs:mainfrom
RunDevelopment:apng-encoder
Open

Add encoding for animated PNGs#3039
RunDevelopment wants to merge 2 commits into
image-rs:mainfrom
RunDevelopment:apng-encoder

Conversation

@RunDevelopment

@RunDevelopment RunDevelopment commented Jun 14, 2026

Copy link
Copy Markdown
Member

Closes #1474.

This PR adds a simple APNG encoder and an example showing how to encode animations using PNG and GIF (see below).

The "encoder" is really just two new functions on PngEncoder: encode_frames and try_encode_frames. The names and API were inspired by GifEncoder.

The code is relatively simple. I just wired up what the png crate already supports. The code converting our delays to PNG delays is a little more complex though. The basic idea is that If the delay fraction cannot be represented exactly, then saturate the numerator or denominator to u16::MAX. This works but doesn't get the best possible approximation in general. IMO, this function should be a method on Delay (e.g. as to_seconds_ratio_u16), but we can talk about that later.


The animation created by the new example looks like this btw:

PNG (58 KB) vs GIF (12 KB) with 50 frames of animation

Hidden by default because I find moving things annoying while reading

spinning spinning

Gif looks worse because 1-bit alpha. This is also the reason why the PNG is larger. Most of the information of the animation is in the alpha channel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

APNG encoder

1 participant