Trim¶
Automatically detect and remove uniform borders from images, keeping the content centered and optionally adding consistent padding. This is useful for cleaning up product shots, removing white backgrounds, or standardizing hero banners.
Parameters¶
trim¶
Enable trimming and select the detection mode.
Type: string | boolean
Values: auto, color, alpha, colorunlessalpha, true
Default: None (no trimming applied)
| Value | Description |
|---|---|
auto |
Automatic edge detection using mean difference and standard deviation thresholds |
color |
Trim based on a specific background color (sampled from top-left pixel or specified with trim-color) |
alpha |
Trim based on transparent pixels (requires image with alpha channel) |
colorunlessalpha |
Use alpha-based trimming if the image has transparency, otherwise fall back to color-based |
true |
Alias for color mode |
trim-color¶
Specify the background color to trim. If not provided, the color is sampled from the top-left pixel.
Type: string
Format: Hex color (e.g., ffffff, f5f5f5, FF0000)
Default: Sampled from top-left pixel
trim-tol¶
Color tolerance threshold for color mode. Higher values trim more aggressively.
Type: float
Range: 0 - 100
Default: 0
trim-pad¶
Add uniform padding around the trimmed content. This helps maintain consistent spacing after trimming.
Type: integer
Range: 0 - any positive value (pixels)
Default: 0
trim-md¶
Mean difference threshold for auto mode. Controls how different a pixel must be from the background to be considered content.
Type: float
Range: 0 - 100
Default: 11
trim-sd¶
Standard deviation threshold for auto mode. Controls variance tolerance in edge detection.
Type: float
Range: 0 - 100
Default: 10
trim-alpha¶
Alpha threshold for alpha and colorunlessalpha modes. Pixels with alpha values below this threshold are considered transparent and will be trimmed.
Type: integer
Range: 0 - 255
Default: 0
Usage¶
Auto Mode (Hero Banners)¶
Automatically detects and trims uniform edges from hero banners or marketing images, adding 36px padding to the result.Color Mode (Product Shots)¶
Trims a light gray (#f5f5f5) background from studio product shots with 18% tolerance, adding 24px padding.Alpha Mode (Transparent UI Elements)¶
Trims transparent pixels from PNG images with alpha channels, keeping pixels with alpha > 16 and adding 12px padding.ColorUnlessAlpha (Universal Preset)¶
A single preset that handles both transparent PNGs and JPEGs with white backgrounds. Uses alpha trimming for PNGs with transparency, falls back to color trimming for JPEGs.Use Cases¶
- E-commerce Product Images: Remove inconsistent white/gray backgrounds from product photography
- User-Uploaded Content: Automatically clean up scanned documents or screenshots with borders
- Hero Banners: Standardize marketing images by removing letterboxing or pillarboxing
- Logo Processing: Trim transparent areas around logos for consistent sizing
- Batch Processing: Apply uniform trimming rules across large image collections
Combining with Other Parameters¶
Trim operations are applied early in the processing pipeline, before resizing. This allows you to:
Trim then Resize¶
First trim the borders, then resize the cleaned content to fit specific dimensions.Trim with Format Conversion¶
Clean up white borders and convert to optimized WebP format.Trim with Effects¶
Trim borders, then apply blur and brightness adjustments.Performance Notes¶
- Trim detection analyzes the entire image border, which adds minimal processing overhead
- Results are cached at the edge for subsequent requests
- For best results, use specific
trim-colorvalues when the background color is known - The
automode works well for images with subtle gradients or compression artifacts in borders
Mode Selection Guide¶
| Image Type | Recommended Mode | Key Parameters |
|---|---|---|
| Product photos on white | color |
trim-color=ffffff, trim-tol=10-20 |
| Product photos on gray | color |
trim-color=f5f5f5, trim-tol=15-25 |
| PNGs with transparency | alpha |
trim-alpha=0-32 |
| Mixed JPEG/PNG uploads | colorunlessalpha |
Set both trim-color and trim-alpha |
| Hero banners with noise | auto |
trim-md=15-20, trim-sd=10-15 |