Crates.io | photo |
lib.rs | photo |
version | 3.1.1 |
created_at | 2023-06-02 19:30:13.558731+00 |
updated_at | 2025-05-05 18:53:38.119789+00 |
description | Image utility library |
homepage | |
repository | https://github.com/FreddyWordingham/Photo |
max_upload_size | |
id | 881153 |
size | 1,200,167 |
Photo
A lightweight, highly-generic Rust library for image manipulation with rich format support and transformation operations.
Photo is a utility library for image manipulation in Rust, providing a flexible and ergonomic API for loading, manipulating, and saving images in various color formats. The library is built on top of ndarray
and supports operations on images represented as 2D arrays of color values.
Colour
trait from the chromatic
cratendarray
: Use the powerful n-dimensional array library for efficient image operationsAdd this to your Cargo.toml
:
[dependencies]
photo = "3.1.0"
Here's a simple example of loading an image, modifying it, and saving it:
use chromatic::HsvAlpha;
use ndarray::Array2;
use photo::Image;
use std::{fs::create_dir_all, path::Path};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let input_path = Path::new("input/image.png");
// Load the image
let mut img: Array2<HsvAlpha<f32>> = Array2::load(input_path)?;
// Modify the image - shift hue by 60 degrees
img.mapv_inplace(|pixel| {
HsvAlpha::new(
pixel.hue() + 60.0,
pixel.saturation(),
pixel.value(),
pixel.alpha(),
)
});
// Save the modified image
create_dir_all("output")?;
img.save(Path::new("output/modified.png"))?;
Ok(())
}
Photo supports various color types through the chromatic
crate:
// RGB colors
use chromatic::Rgb;
let img: Array2<Rgb<f32>> = Array2::load("rgb_image.png")?;
// RGBA colors
use chromatic::RgbAlpha;
let img: Array2<RgbAlpha<f32>> = Array2::load("rgba_image.png")?;
// HSV colors
use chromatic::Hsv;
let img: Array2<Hsv<f32>> = Array2::load("hsv_image.png")?;
// HSV with alpha
use chromatic::HsvAlpha;
let img: Array2<HsvAlpha<f32>> = Array2::load("hsva_image.png")?;
// Grayscale
use chromatic::Gray;
let img: Array2<Gray<f32>> = Array2::load("gray_image.png")?;
Leverage ndarray
's powerful operations for image manipulation:
use ndarray::s;
// Extract a region of the image
let region = img.slice(s![100..300, 200..400]);
// Flip the image horizontally
let flipped = img.slice(s![.., ..;-1]);
// Rotate the image 90 degrees
let rotated = img.t();
Apply transformations to each pixel:
// Invert an RGB image
use chromatic::Rgb;
let inverted = rgb_image.mapv(|px: Rgb<f32>| {
Rgb::new(
1.0 - px.red(),
1.0 - px.green(),
1.0 - px.blue()
)
});
// Adjust brightness
let brightened = img.mapv(|px: HsvAlpha<f32>| {
HsvAlpha::new(
px.hue(),
px.saturation(),
(px.value() * 1.2).min(1.0), // Increase brightness by 20%
px.alpha()
)
});
The library provides a comprehensive error type PngError
that covers various failure modes:
chromatic
: Color manipulation libraryndarray
: N-dimensional array manipulationnum-traits
: Numeric trait abstractionspng
: PNG format encoding/decodingvista
: (dev dependency) for visualization in the terminal