Skip to content
docs
GitHub 4.5M /week v2.2.0
Pixels

scan()

Run a function for every pixel in the image or a region.

From @jimp/core

scan calls your function once for every pixel, row by row from the top-left corner, passing the pixel’s coordinates and its index in image.bitmap.data. It’s the building block for custom effects and pixel analysis: most of Jimp’s built-in plugins use it.

Signature

scan.d.ts
image.scan(f: (x: number, y: number, idx: number) => any): this
image.scan(
x: number,
y: number,
w: number,
h: number,
f: (x: number, y: number, idx: number) => any,
): this

With one argument, scan visits the whole image. With five, it visits only the w × h region whose top-left corner is at x, y. scan runs synchronously and returns the image, so you can chain more calls after it.

Parameters

Parameter Type Default Description
x number 0 Left edge of the region.
y number 0 Top edge of the region.
w number image width Width of the region in pixels.
h number image height Height of the region in pixels.
f function required Called with (x, y, idx) for every pixel.

Give all four region values or none of them; the defaults apply when you pass only f. The region is clamped to the image, so f is only called for pixels inside it.

Inside f, the pixel’s bytes are image.bitmap.data[idx] (red), [idx + 1] (green), [idx + 2] (blue) and [idx + 3] (alpha), each from 0 to 255. Change them to change the pixel.

Example

Swap the red and blue channels of every pixel:

example.ts
import { Jimp } from "jimp";
const image = await Jimp.read("photo.png");
image.scan((x, y, idx) => {
const { data } = image.bitmap;
const red = data[idx];
data[idx] = data[idx + 2];
data[idx + 2] = red;
});
await image.write("swapped.png");

Scan a region

Pass the region before the function. This makes a 100 × 50 block, starting at (20, 10), half transparent. If the block reaches past the edge of the image, only the part inside the image is changed:

example.ts
image.scan(20, 10, 100, 50, (x, y, idx) => {
image.bitmap.data[idx + 3] = Math.round(image.bitmap.data[idx + 3] / 2);
});

Collect information

scan doesn’t have to change anything. This computes the average color of the opaque pixels:

example.ts
let r = 0, g = 0, b = 0, count = 0;
image.scan((x, y, idx) => {
const { data } = image.bitmap;
if (data[idx + 3] === 255) {
r += data[idx];
g += data[idx + 1];
b += data[idx + 2];
count++;
}
});
const average = count ? { r: r / count, g: g / count, b: b / count } : null;

Use x and y

The coordinates let an effect depend on position. This fades the image out toward the left edge:

example.ts
image.scan((x, y, idx) => {
const { data } = image.bitmap;
data[idx + 3] = Math.round(data[idx + 3] * (x / image.width));
});

Try it

Pick what the function does for each pixel, and turn on “region only” to drag out a region. The code panel shows the full scan call.

Try scan()
image.scan((x, y, idx) => {

Run a function for every pixel, or every pixel in a region. Pick what the function does.

region only
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

  • this is the image when f is a regular function, as in v0: function (x, y, idx) { this.bitmap.data[idx] = 0; }. Arrow functions keep their own this, so refer to the image by name.
  • Values wrap, they don’t clamp. bitmap.data is a Buffer, so assigning 300 stores 44 and -1 stores 255. Clamp computed values with limit255 and round fractions yourself; fractional values are truncated.
  • You can’t stop early. The return value of f is ignored. To stop at the first match, use scanIterator with break.
  • Regions are clamped to the image. Region values are rounded to whole pixels, and the part of the region outside the image is dropped: scan(-10, -10, 30, 30, f) visits the 20 × 20 pixels from (0, 0). A region that doesn’t overlap the image at all calls f zero times.
  • Missing arguments in the five-argument form throw y must be a number, w must be a number, h must be a number or cb must be a function.
  • Speed. scan is much faster than calling getPixelColor and setPixelColor for every pixel. A plain for loop over image.bitmap.data in steps of 4 is faster still, if you don’t need x and y.
  • In a plugin. The same function is exported as scan(image, f) and scan(image, x, y, w, h, f) from @jimp/utils, with the same clamping. clampRegion(image, x, y, w, h), also from @jimp/utils, returns the clamped region as { x, y, w, h } if you need it yourself.
  • Changed in 2.0: regions are clamped to the image; before, pixels past the right edge got the indexes of the next row, and pixels above, below or left of the image got indexes outside the buffer.