10 releases

0.3.4 Feb 14, 2024
0.3.3 Feb 14, 2024
0.3.1 Feb 21, 2023
0.3.0 Dec 24, 2022
0.1.3 Oct 28, 2019

#175 in Images

Download history 19/week @ 2024-08-12 5/week @ 2024-08-19 15/week @ 2024-08-26 4/week @ 2024-09-02 11/week @ 2024-09-09 4/week @ 2024-09-16 50/week @ 2024-09-23 22/week @ 2024-09-30 15/week @ 2024-10-14 1/week @ 2024-10-21 26/week @ 2024-11-04 12/week @ 2024-11-18 16/week @ 2024-11-25

54 downloads per month
Used in 4 crates

MIT license

50KB
363 lines

apng

apng at crates.io apng at docs.rs License apng at GitHub Actions

apng is animated png encoder for Rust, and made in pure Rust.

Example usage

use apng::{Encoder, Frame, PNGImage};
use std::fs::File;
use std::io::BufWriter;
use std::path::Path;

fn main() {
    let files = vec![
        "rust_logo1.png",
        "rust_logo2.png",
        "rust_logo3.png",
        "rust_logo4.png",
        "rust_logo5.png",
        "rust_logo6.png",
    ];

    let mut png_images: Vec<PNGImage> = Vec::new();
    for f in files.iter() {
        png_images.push(apng::load_png(f).unwrap());
    }

    let path = Path::new(r"sample/out.png");
    let mut out = BufWriter::new(File::create(path).unwrap());

    let config = apng::create_config(&png_images, None).unwrap();
    let mut encoder = Encoder::new(&mut out, config).unwrap();
    let frame = Frame {
        delay_num: Some(1),
        delay_den: Some(2),
        ..Default::default()
    };

    match encoder.encode_all(png_images, Some(&frame)) {
        Ok(_n) => println!("success"),
        Err(err) => eprintln!("{}", err),
    }
}

Customize each frame speed

    let mut i = 1;
    for image in png_images.iter() {
        i += 1;
        let frame = Frame {
            delay_num: Some(1),
            delay_den: Some(i), // 2, 3, 4, 5, 6, 7
            ..Default::default()
        };
        encoder.write_frame(image, frame).unwrap();
    }

Sample code is here.

License

MIT

See also

Reference

Dependencies

~3MB
~65K SLoC