| Crates.io | refilelabs-image |
| lib.rs | refilelabs-image |
| version | 0.2.5 |
| created_at | 2025-02-19 19:20:42.290185+00 |
| updated_at | 2025-03-12 00:19:28.210772+00 |
| description | Wasm-based image processing library developed by re;file labs |
| homepage | https://refilelabs.com/image |
| repository | https://github.com/refilelabs/image |
| max_upload_size | |
| id | 1561742 |
| size | 56,673 |
A Rust library for advanced image manipulation and format conversion. This crate provides tools for loading image metadata, converting images, and retrieving raw pixel data.
It is used under the hood at re;file labs' image tools to power the different image processing features.
Add this to your Cargo.toml:
[dependencies]
refilelabs-image = "0.1.0" # Replace with actual version
The main API functions are only available when the crate is not compiled with the
wasm
feature. These functions are designed for native Rust usage.
convert_image(file: &[u8], src_type: &str, target_type: &str, convert_settings: &Option<Settings>) -> Result<Vec<u8>, WasmImageError>Converts an image from one format to another.
file (&[u8]): The image file to convertsrc_type (&str): The MIME type of the source image (e.g., "image/png")target_type (&str): The target MIME type (e.g., "image/webp")convert_settings (&Option<Settings>): Settings for the conversionResult<Vec<u8>, WasmImageError>: The converted image data or an errorload_metadata(file: &[u8], src_type: &str) -> Result<Metadata, WasmImageError>Loads the metadata of an image file.
file (&[u8]): The image file to analyzesrc_type (&str): The MIME type of the source imageResult<Metadata, WasmImageError>: An object containing image metadata or an errorget_pixels(file: &[u8], src_type: &str) -> Result<ImageData, WasmImageError>Converts an image file to raw RGBA pixel data.
file (&[u8]): The image file to convertsrc_type (&str): The MIME type of the source imageResult<ImageData, WasmImageError>: An object containing raw pixel data and image properties or an errorMetadataRepresents metadata of an image.
width (u32): Image widthheight (u32): Image heightother (Option<HashMap<String, String>>): Additional metadataImageDataRepresents raw image data.
width (u32): Image widthheight (u32): Image heightaspect_ratio (f32): Aspect ratiocolor_depth (u8): Color depthpixels (Vec<u8>): Raw RGBA pixel valuesSettingsSettings for conversion.
use refilelabs_image::{convert_image, get_pixels, load_metadata, Settings};
use std::fs;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Read image file
let file = fs::read("input.png")?;
let src_type = "image/png";
let target_type = "image/webp";
// Get metadata
let metadata = load_metadata(&file, src_type)?;
println!("Image dimensions: {}x{}", metadata.width, metadata.height);
// Get pixel data
let image_data = get_pixels(&file, src_type)?;
println!("Image has {} pixels", image_data.pixels.len() / 4);
// Convert image
let converted = convert_image(&file, src_type, target_type, &None)?;
fs::write("output.webp", converted)?;
Ok(())
}
MIT