scale multiplies both sides of the image by one factor, so the aspect ratio never changes.
Use it for relative sizes such as “half size” or “double size”.
Signature
image.scale(f: number): thisimage.scale(options: { f: number; mode?: ResizeStrategy; premultiplyAlpha?: boolean }): thisscale changes the image in place and returns it, so you can chain more calls after it.
Options
Pass the factor on its own, or an object to also choose the algorithm or how alpha is handled.
| Option | Type | Default | Description |
|---|---|---|---|
f |
number |
required | The factor. 0.5 halves both sides, 2 doubles them. Must be 0 or more. |
mode |
ResizeStrategy |
built-in resizer | The resampling algorithm. See resize strategies. |
premultiplyAlpha |
boolean |
true |
Treat alpha as opacity. Pass false when alpha holds other data, to keep the color of transparent pixels. See Alpha that isn’t opacity. |
Example
import { Jimp } from "jimp";
const image = await Jimp.read("photo.jpg");
// 800 x 600 becomes 400 x 300image.scale(0.5);
await image.write("photo-half.jpg");Choose the algorithm
import { Jimp, ResizeStrategy } from "jimp";
const icon = await Jimp.read("icon.png");
icon.scale({ f: 4, mode: ResizeStrategy.NEAREST_NEIGHBOR });
const photo = await Jimp.read("photo.jpg");
photo.scale({ f: 0.25, mode: ResizeStrategy.LANCZOS });Try it
Drag f to change the factor, and pick a strategy to compare algorithms.
image.scale(0.5)Multiply the width and height by the same factor.
Loading Jimp…Drop an image to load it
index.js
import { Jimp } from "jimp"; const image = await Jimp.read("dice.png"); // Change an option to run it await image.write("output.png");
Notes
- Same as
resize.scale(f)isresize({ w: image.width * f, h: image.height * f }), andmodeandpremultiplyAlphaare passed on. The new sizes are rounded to whole pixels, and a side that rounds to0becomes1. - Negative factors fail validation.
scale({ f: -1 })reportsToo small: expected number to be >=0;scale(-1)is checked byresizeinstead and reportsInvalid input. - Added in 2.0: the
premultiplyAlphaoption, and theLANCZOS,LANCZOS2andMITCHELLstrategies. - To fit a box rather than multiply by a factor, use
scaleToFit. It works out the factor for you.