Batch convert and optimize images to JPEG format with automatic resizing, compression, and EXIF-based rotation.
- Supported Formats:
.jpg,.jpeg,.png,.webp,.tif,.tiff,.bmp,.gif,.heic,.heif - Auto-Rotation: Rotates images based on EXIF data for correct orientation
- Resizing: Optional width and height constraints with aspect ratio preservation
- Compression: Configurable JPEG quality (0-100, default: 78) using mozjpeg
- Background Flattening: Converts transparent PNGs with white background
- Structure Preservation: Maintains directory structure in output
- Dry-Run Mode: Preview optimization without writing files
- Compression Reporting: Shows compression savings percentage
- Node.js 14.x or higher
cd img-opt
npm installThis installs the required sharp image processing library and dependencies.
node optimize-images.js [OPTIONS]| Argument | Default | Description |
|---|---|---|
--in |
./Original |
Input directory containing images to optimize |
--out |
./Optimized |
Output directory for optimized images |
--quality |
78 |
JPEG quality (0-100, lower = smaller file size) |
--maxWidth |
1800 |
Maximum image width in pixels (0 = no limit) |
--maxHeight |
0 |
Maximum image height in pixels (0 = no limit) |
--overwrite |
false |
Overwrite existing files in output directory |
--dryRun |
false |
Preview changes without writing files |
Basic optimization with defaults:
node optimize-images.jsThis optimizes images from ./Original to ./Optimized with quality 78 and max width 1800px.
Custom quality and dimensions:
node optimize-images.js --in "./input" --out "./output" --quality 85 --maxWidth 2400 --maxHeight 1600Preview changes without writing:
node optimize-images.js --in "./input" --out "./output" --dryRunOverwrite existing optimized files:
node optimize-images.js --in "./input" --out "./output" --overwriteThe optimizer creates a new directory (specified by --out) containing:
- Optimized JPEG files in the same directory structure as the input
- All images converted to
.jpgformat - Reduced file sizes compared to originals
- Compression statistics printed to console
Users place original images in the Original folder, and optimized results are saved to the Optimized folder.
- Quality Setting: Use 78-82 for web delivery, 85-90 for high-quality output
- Dimensions: 1800px width is suitable for most web displays; reduce for mobile optimization
- Testing: Use
--dryRunfirst to preview results without committing changes - Backup: Keep original images; this tool only writes to the output directory