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
- Support for row and column labels
- Unicode text support with emoji
- Automatic image scaling and alignment
- Configurable column label alignment (left, center, right)
- Adjustable top padding for label spacing
- White background
Installation
Add this to your Cargo.toml
:
[]
= "0.1.10"
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.
Layout Engine and Debugging
The library includes a powerful layout engine for debugging and visualizing image grid layouts. This is particularly useful when developing applications that use the grid plotting functionality.
Layout System Components
The layout system consists of several key components:
LayoutRect
- Represents a rectangular region with position (x, y) and dimensions (width, height)LayoutElement
- Represents different types of elements in the layout:Image
- An image element with its position and source pathRowLabel
- A row label with its position and textColumnLabel
- A column label with its position and textPadding
- A padding region with its position and description
Debug Visualization
The layout engine can generate debug visualizations that show:
- Images (Light Blue) - Shows where each image is positioned
- Row Labels (Light Red) - Shows row label positioning and dimensions
- Column Labels (Light Green) - Shows column label positioning and dimensions
- Padding Areas (Light Gray) - Shows padding regions for spacing and alignment
Each element is outlined with a dark border for clear separation.
Using the Layout Engine
use ;
use PathBuf;
Layout Algorithm
The layout engine follows these steps:
-
Canvas Setup
- Calculates total dimensions based on images and labels
- Creates padding areas for labels if needed
-
Element Placement
- Places column labels at the top with proper alignment
- Positions row labels on the left side
- Centers images within their grid cells
- Adds padding between elements for spacing
-
Debug Rendering
- Creates a color-coded visualization
- Shows exact positions and dimensions of all elements
- Highlights padding and alignment areas
Benefits of Layout Debugging
- Visualize spacing and alignment issues
- Debug label positioning problems
- Understand how the grid layout is calculated
- Verify padding and margins are correct
- Ensure images are properly centered
Layout Features
-
Column Label Alignment: Control how column labels are positioned relative to their images:
Left
: Align labels with the left edge of the imageCenter
: Center labels over the image (default)Right
: Align labels with the right edge of the image
-
Adjustable Top Padding: Control the vertical space reserved for labels:
- Default is 40 pixels
- Can be increased for larger labels or decreased for compact layouts
- Automatically applied only when labels are present