scanIterator visits the same pixels as scan, in the same order, but as an iterator you loop over with for...of.
Use it when you want to break out early, continue, or keep your loop body in the surrounding function instead of a callback.
Signature
image.scanIterator( x?: number, y?: number, w?: number, h?: number,): Generator<{ x: number; y: number; idx: number; image: typeof image }>Each step yields the pixel’s coordinates, its index in image.bitmap.data and the image itself.
Pixels are produced lazily as you loop, so stopping early skips the rest.
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 - x |
Width of the region in pixels. |
h |
number |
image.height - y |
Height of the region in pixels. |
Unlike scan, each value has its own default, and w and h default to the rest of the image: image.scanIterator(10, 10) covers everything from (10, 10) to the bottom-right corner.
The region is clamped to the image, so only pixels inside it are yielded.
Example
Find the first fully transparent pixel and stop there:
import { Jimp } from "jimp";
const image = await Jimp.read("logo.png");
let firstHole = null;
for (const { x, y, idx } of image.scanIterator()) { if (image.bitmap.data[idx + 3] === 0) { firstHole = { x, y }; break; }}Change pixels in a region
Writing to image.bitmap.data works the same as in scan. This turns a 100 × 100 block grey:
const { data } = image.bitmap;
for (const { idx } of image.scanIterator(0, 0, 100, 100)) { const grey = Math.round((data[idx] + data[idx + 1] + data[idx + 2]) / 3); data[idx] = grey; data[idx + 1] = grey; data[idx + 2] = grey;}Await inside the loop
Because the loop body isn’t a callback, you can use await in it, for example to yield to the event loop every few rows while processing a large image:
for (const { x, y, idx } of image.scanIterator()) { if (x === 0 && y % 100 === 0) { await new Promise((resolve) => setTimeout(resolve)); } image.bitmap.data[idx] = 255 - image.bitmap.data[idx];}Try it
Pick what the loop does for each pixel and drag out the region on the image. Turn off “region only” to loop over the whole image.
for (const { x, y, idx } of image.scanIterator(72, 54, 216, 162)) {Loop over every pixel, or every pixel in a region, with for...of.
import { Jimp } from "jimp"; const image = await Jimp.read("dice.png"); // Change an option to run it await image.write("output.png");
Notes
- It’s slower than
scan. Creating an object for every pixel adds overhead; over a whole imagescanIteratortakes about two to three times as long asscan. For effects that touch every pixel and never stop early, preferscan. - Values wrap, they don’t clamp, exactly as in
scan: clamp computed values withlimit255. - Regions are clamped to the image. Region values are rounded to whole pixels, and the part of the region outside the image is skipped. A region that doesn’t overlap the image yields nothing.
- Arguments must be numbers when given. Otherwise it throws
x and y must be numbersorw and h must be numbersas soon as you call it, before the loop starts. - Don’t resize inside the loop. The region is fixed when the loop starts, but each index is computed from the current width, so changing the image’s size mid-loop gives wrong indexes.
- In a plugin.
scanIterator(image, x, y, w, h)is also exported from@jimp/utils. There all four region values are required. - Changed in 2.0: regions are clamped to the image, and
wandhdefault to the rest of the image fromxandy; before, they defaulted to the full image size, soscanIterator(x, y)ran past the edges and yielded indexes outside the buffer.