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
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,): thisWith 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:
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:
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:
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:
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.
image.scan((x, y, idx) => {Run a function for every pixel, or every pixel in a region. Pick what the function does.
import { Jimp } from "jimp"; const image = await Jimp.read("dice.png"); // Change an option to run it await image.write("output.png");
Notes
thisis the image whenfis a regularfunction, as in v0:function (x, y, idx) { this.bitmap.data[idx] = 0; }. Arrow functions keep their ownthis, so refer to the image by name.- Values wrap, they don’t clamp.
bitmap.datais aBuffer, so assigning300stores44and-1stores255. Clamp computed values withlimit255and round fractions yourself; fractional values are truncated. - You can’t stop early. The return value of
fis ignored. To stop at the first match, usescanIteratorwithbreak. - 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 callsfzero times. - Missing arguments in the five-argument form throw
y must be a number,w must be a number,h must be a numberorcb must be a function. - Speed.
scanis much faster than callinggetPixelColorandsetPixelColorfor every pixel. A plainforloop overimage.bitmap.datain steps of 4 is faster still, if you don’t needxandy. - In a plugin. The same function is exported as
scan(image, f)andscan(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.