Skip to content
docs
GitHub 4.5M /week v2.2.0
Size & shape

scale()

Scale the width and height by the same factor.

From @jimp/plugin-resize

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

scale.d.ts
image.scale(f: number): this
image.scale(options: { f: number; mode?: ResizeStrategy; premultiplyAlpha?: boolean }): this

scale 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

example.ts
import { Jimp } from "jimp";
const image = await Jimp.read("photo.jpg");
// 800 x 600 becomes 400 x 300
image.scale(0.5);
await image.write("photo-half.jpg");

Choose the algorithm

example.ts
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.

Try scale()
image.scale(0.5)

Multiply the width and height by the same factor.

0.5
Loading Jimp…Drop an image to load it
PNG
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) is resize({ w: image.width * f, h: image.height * f }), and mode and premultiplyAlpha are passed on. The new sizes are rounded to whole pixels, and a side that rounds to 0 becomes 1.
  • Negative factors fail validation. scale({ f: -1 }) reports Too small: expected number to be >=0; scale(-1) is checked by resize instead and reports Invalid input.
  • Added in 2.0: the premultiplyAlpha option, and the LANCZOS, LANCZOS2 and MITCHELL strategies.
  • To fit a box rather than multiply by a factor, use scaleToFit. It works out the factor for you.