imx
A Rust library for image processing and manipulation, providing functionality for letterbox removal, transparency handling, JXL format support, and image grid plotting.
Features
- 🖼️ Image Processing
- Remove letterboxing from images with configurable threshold
- Remove transparency (convert to black)
- Get image dimensions
- Process images in batches
- Create image grid plots with labels
- Safe numeric conversions for image data
- 📸 Format Support
- JPEG/JPG
- PNG
- WebP
- JXL (JPEG XL) with automatic PNG conversion
- 🛠️ Utilities
- File type detection with magic number validation
- Async/await support with proper lifetime handling
- Error handling with detailed context
- Structured logging with info/warn levels
- Safe numeric type conversions (f32 ↔ i32 ↔ u32 ↔ u8)
- Unicode text rendering with emoji support
- Automatic image scaling and alignment
Installation
Add this to your Cargo.toml
:
[]
= "0.1.7"
Usage Examples
Image Processing
use ;
use Result;
use Path;
async
JXL Processing
The JXL processing functions require careful handling of lifetimes. Here are the recommended patterns:
use ;
use Result;
use ;
use Future;
use Pin;
// Define a type alias for the future to make the code cleaner
type BoxFut<'a> = ;
// Method 1: Using an inline closure with proper lifetime handling
async
// Method 2: Using a separate function with explicit lifetime parameter
async
// Direct JXL to PNG conversion
async
Create Image Grid Plots
use ;
use PathBuf;
use Result;
Safe Numeric Conversions
use ;
Check File Types
use ;
use Path;
Error Handling
All functions return Result
types with detailed error context. The library uses anyhow
for error handling:
use remove_letterbox_with_threshold;
use ;
use Path;
async
Best Practices
- Path Handling: Always use
Path
orPathBuf
types instead of raw strings for file paths. - JXL Processing: When using
process_jxl_file
, properly handle lifetimes using either:- A type alias for boxed futures (
BoxFut<'a>
) - Explicit lifetime parameters in separate functions
- A type alias for boxed futures (
- Error Handling: Use
.with_context()
to add meaningful error messages - Async Functions: Most image processing functions are async - use them with
.await
- Type Safety: Use the provided numeric conversion functions instead of raw casts
- Grid Plotting: Ensure consistent image dimensions for best results
- Labels: Unicode and emoji are supported in grid plot labels
Testing
Run the test suite:
The test suite includes:
- Unit tests for all major functions
- Integration tests with sample images
- Error handling tests
- Format-specific tests (JXL, PNG, etc.)
- Numeric conversion tests
- Grid plotting tests
- Lifetime handling tests
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License.