Skip to content

Instantly share code, notes, and snippets.

@raspberrypisig
Created January 3, 2026 01:43
Show Gist options
  • Select an option

  • Save raspberrypisig/507553fbf10356bfebf1a3856f3b4d98 to your computer and use it in GitHub Desktop.

Select an option

Save raspberrypisig/507553fbf10356bfebf1a3856f3b4d98 to your computer and use it in GitHub Desktop.
#!/usr/bin/env cargo
//! ```cargo
//! [dependencies]
//! image = "0.25"
//! ```
use image::codecs::gif::{GifEncoder, Repeat};
use image::{Delay, Frame, Rgba, RgbaImage};
use std::fs::File;
use std::time::Duration;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// 1. Setup the output file
let file_out = File::create("rgb_cycle.gif")?;
let mut encoder = GifEncoder::new(file_out);
// 2. Configure the animation loop (Repeat::Infinite means loop forever)
encoder.set_repeat(Repeat::Infinite)?;
// 3. Define our animation parameters
let width = 32;
let height = 32;
// Colors (Red, Green, Blue) with Alpha channel (255)
let colors = vec![
Rgba([255, 0, 0, 255]), // Red
Rgba([0, 255, 0, 255]), // Green
Rgba([0, 0, 255, 255]), // Blue
];
// 500ms delay
let delay = Delay::from_saturating_duration(Duration::from_millis(500));
// 4. Create and write frames
let frames: Vec<Frame> = colors
.into_iter()
.map(|color| {
let mut image_buffer = RgbaImage::new(width, height);
for pixel in image_buffer.pixels_mut() {
*pixel = color;
}
Frame::from_parts(image_buffer, 0, 0, delay)
})
.collect();
// 5. Encode
encoder.encode_frames(frames.into_iter())?;
println!("Success! 'rgb_cycle.gif' has been created.");
Ok(())
}
set shell := ["sh", "-c"]
set windows-shell := ["powershell", "-c"]
run:
cargo -Z script gif_maker.rs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment