Thanks to visit codestin.com
Credit goes to lib.rs

5 releases

Uses new Rust 2024

0.1.4 Apr 24, 2025
0.1.3 Apr 18, 2025
0.1.2 Apr 18, 2025
0.1.1 Apr 17, 2025
0.1.0 Apr 17, 2025

#819 in Images

Codestin Search App Codestin Search App

254 downloads per month

MIT license

4MB
160 lines

img2avif

Convert images to AVIF format.

Usage

This library provides functions to convert image data into the AVIF format.

use std::fs::File;
use img2avif::{convert, convert_with_quality_speed};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let file = File::open("path/to/your/image.png")?; // Supports PNG, JPEG, WebP, etc.

    // Use default quality (85) and speed (6)
    let avif_data = convert(file)?;

    std::fs::write("output.avif", avif_data)?;

    println!("Successfully converted image to AVIF!");
    Ok(())
}

If you want to customize quality and speed:

use std::fs::File;
use img2avif::convert_with_quality_speed;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let file = File::open("path/to/your/image.jpg")?;

    // quality: 0-100, speed: 0-10
    let avif_data = convert_with_quality_speed(file, 90, 1)?;

    std::fs::write("output.avif", avif_data)?;

    Ok(())
}

See src/lib.rs for the function implementation and tests for more examples.

Dependencies

  • image (version 0.25.6 with "avif" feature)

Building and Testing

# Build the project
cargo build

# Run tests
cargo test

License

This project is licensed under the MIT License. See the Cargo.toml file for details.

Dependencies

~25MB
~576K SLoC