7 stable releases
Uses new Rust 2024
| new 1.0.7 | Mar 3, 2026 |
|---|---|
| 1.0.5 | Mar 2, 2026 |
| 1.0.4 | Feb 19, 2026 |
| 1.0.3 | Dec 20, 2025 |
| 1.0.1 | Nov 1, 2025 |
#149 in Images
105KB
2.5K
SLoC
shrinkray
A high-performance Rust library for image processing powered by libvips.
Features
- Fast image processing - Built on libvips for exceptional performance
- Fluent API - Intuitive builder pattern for chaining operations
- Format conversion - Support for JPEG, PNG, WebP, and AVIF
- Resizing & cropping - Multiple fit modes (crop, clip, max)
- Filters & effects - Vintage, sepia, monochrome, duotone, and more
- Image adjustments - Sharpen, blur, tint, rotation, and trimming
- Type-safe - Strong typing for colours, formats, and options
Installation
cargo add shrinkray
System Dependencies
- libvips 8.17.0 or higher (for older 8.16.x libvips versions use shrinkray 1.0.1)
- libaom (for AVIF support via libheif)
- libheif
- libjpeg
- libpng
- libwebp
For more information on installing the required dependencies, see the Ubuntu and Alpine base images.
Usage
Basic Example
use shrinkray::ImageProcessor;
use shrinkray::options::{Format, Fit};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Read input image
let image_bytes = std::fs::read("input.jpg")?;
// Process the image
let result = ImageProcessor::new(&image_bytes)
.resize(800, 600)
.quality(85)
.format(Format::Webp)
.fit(Fit::Crop)
.process()?;
// Save the result
std::fs::write("output.webp", result.bytes())?;
Ok(())
}
Create Thumbnails
let thumbnail = ImageProcessor::new(&image_bytes)
.thumbnail(200) // 200x200 square thumbnail
.process()?;
Apply Filters
let filtered = ImageProcessor::new(&image_bytes)
.vintage(80)
.sharpen(40)
.process()?;
Duotone Effect
let duotone = ImageProcessor::new(&image_bytes)
.duotone(
0, 50, 100, // Shadow colour (dark blue)
255, 165, 0 // Highlight colour (orange)
)
.duotone_alpha(75) // 75% opacity
.process()?;
Rotation and Trimming
let trimmed = ImageProcessor::new(&image_bytes)
.rotate(90)
.trim() // Auto-trim transparent/whitespace borders
.process()?;
Custom Aspect Ratio
let cropped = ImageProcessor::new(&image_bytes)
.aspect_ratio(16, 9)
.width(1920)
.fit(Fit::Crop)
.process()?;
Device Pixel Ratio (Retina Support)
let retina = ImageProcessor::new(&image_bytes)
.width(400)
.height(300)
.device_pixel_ratio(2) // Output will be 800x600
.process()?;
Available Options
Resizing
width(i32)- Set output widthheight(i32)- Set output heightresize(i32, i32)- Set both width and heightthumbnail(i32)- Create square thumbnailfit(Fit)- Resize behavior (Crop, Clip, Max)device_pixel_ratio(i32)- DPR multiplieraspect_ratio(i32, i32)- Target aspect ratio
Format & Quality
format(Format)- Output format (Jpeg, Png, Webp, Avif)quality(i32)- Quality 1-100 (default: 75)lossless(bool)- Enable lossless compression
Adjustments
rotate(u16)- Rotate 90, 180, or 270 degreessharpen(u8)- Sharpening intensity 1-100blur(u8)- Blur amount 1-100trim()- Auto-trim borderstrim_colour(u8, u8, u8)- Trim specific colourbackground(u8, u8, u8)- Background colour for padding/flattening
Filters
grayscale()- Convert to grayscalemonochrome(u8)- Monochrome with intensitysepia(u8)- Sepia tone filtervintage(u8)- Vintage film effectpolaroid(u8)- Polaroid effectkodachrome(u8)- Kodachrome film simulationtechnicolor(u8)- Technicolor effecttint(u8, u8, u8)- Colour tint overlayduotone(u8, u8, u8, u8, u8, u8)- Duotone with shadow and highlight coloursduotone_alpha(u8)- Duotone opacity 1-100
Safety Limits
max_megapixels(f64)- Maximum input image sizemax_output_resolution(u32)- Maximum output dimension
ProcessedImage API
The process() method returns a ProcessedImage with these methods:
bytes(&self) -> &[u8]- Get image bytes as a sliceinto_bytes(self) -> Vec<u8>- Consume and get owned bytesformat(&self) -> Format- Get the output formatmime_type(&self) -> &'static str- Get MIME type string
Full Server Implementation
This library powers the shrinkray image server, which provides:
- HTTP/S3/file backend routing
- HMAC signature verification
- Prometheus metrics
- OpenTelemetry tracing
- Kubernetes deployment configs
See the main repository for the complete server implementation.
License
MIT License - see LICENSE for details.
Dependencies
~13–27MB
~393K SLoC