image-size-cli
v1.0.0
Published
CLI tool to resize images — supports BMP and PPM, bilinear & nearest-neighbor interpolation, aspect-ratio-aware fit modes. Zero dependencies.
Maintainers
Readme
image-size-cli
Resize images from the command line. Zero dependencies — only Node.js built-ins.
Supports BMP and PPM formats with bilinear and nearest-neighbor interpolation.
Install
npm install -g image-size-cliUsage
image-size-cli <input> [options]# Resize to 800px wide (aspect ratio preserved)
image-size-cli photo.bmp --width 800
# Resize to exactly 400×300 (stretches if needed)
image-size-cli photo.bmp -w 400 -h 300 --fit=fill
# Scale to 50%
image-size-cli photo.bmp --scale 50 -o small.bmp
# Cover a 200×200 thumbnail box
image-size-cli photo.bmp -w 200 -h 200 --fit=cover -o thumb.bmp
# Nearest-neighbor (pixel art / icons)
image-size-cli sprite.bmp --scale 400 --algorithm nearest
# Cross-format: BMP → PPM
image-size-cli photo.bmp --width 320 -o photo.ppmOptions
| Flag | Short | Description |
|---|---|---|
| --width <px> | -w | Target width in pixels |
| --height <px> | -h | Target height in pixels |
| --scale <pct> | -s | Scale percentage (e.g. 50 = 50%) |
| --output <path> | -o | Output file (default: <name>_resized.<ext>) |
| --fit <mode> | -f | contain · cover · fill (default: contain) |
| --algorithm <a> | -a | bilinear (default) · nearest |
| --help | | Show help |
Fit modes
| Mode | Behaviour |
|---|---|
| contain | Fit inside the target box, preserve aspect ratio (may add empty edges) |
| cover | Cover the target box, preserve aspect ratio (may overflow) |
| fill | Stretch to exact target dimensions |
Supported formats
| Format | Read | Write | |---|---|---| | BMP (24-bit, 32-bit) | ✓ | ✓ (24-bit) | | PPM (P6 binary) | ✓ | ✓ |
API
Use as a library:
import { resizeImage, calculateDimensions, bilinear } from 'image-size-cli';
// High-level (reads + writes files)
const result = resizeImage('photo.bmp', 'small.bmp', { width: 400, fit: 'contain' });
console.log(result); // { srcWidth: 1920, srcHeight: 1080, dstWidth: 400, dstHeight: 225 }
// Dimension calculator (pure)
const dims = calculateDimensions(1920, 1080, { width: 800 });
// { width: 800, height: 450 }
calculateDimensions(1920, 1080, { width: 200, height: 200, fit: 'cover' });
// { width: 200, height: 113 } ← both axes ≥ 200
// Low-level pixel resize (pure, Uint8Array → Uint8Array)
import { parseBmp, encodeBmp, bilinear, nearestNeighbor } from 'image-size-cli';
import { readFileSync, writeFileSync } from 'fs';
const src = parseBmp(readFileSync('photo.bmp'));
const px = bilinear(src.pixels, src.width, src.height, 320, 240, 3);
writeFileSync('out.bmp', encodeBmp(320, 240, px));CommonJS
const { resizeImage, calculateDimensions } = require('image-size-cli');How it works
Bilinear interpolation — samples 4 neighbouring pixels and blends them proportionally. Smooth results for photos and artwork.
Nearest-neighbor — copies the closest pixel. Sharp, blocky results — ideal for pixel art, icons, or sprites.
BMP is parsed/written manually (14-byte file header + 40-byte BITMAPINFOHEADER). Supports 24-bit and 32-bit input, always writes 24-bit.
PPM (P6 binary) is a trivial format: ASCII header + raw RGB bytes. Fast and simple.
License
MIT
