getPixelIndex returns the offset of a pixel’s first byte in image.bitmap.data.
Use it when you read or write the raw bytes of individual pixels, especially near the edges, where its edge handling decides what happens to coordinates outside the image.
Signature
image.getPixelIndex(x: number, y: number, edgeHandling?: Edge): numbergetPixelIndex only computes a number; it doesn’t read or change the image.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
x |
number |
required | Column, counted from 0 at the left edge. |
y |
number |
required | Row, counted from 0 at the top edge. |
edgeHandling |
Edge |
Edge.EXTEND |
What to do with coordinates outside the image. See Edge handling. |
The pixel’s red, green, blue and alpha bytes are at the returned index and the three after it.
Inside the image the index is always (y * image.width + x) * 4.
Example
import { Jimp } from "jimp";
const image = await Jimp.read("photo.png");const { data } = image.bitmap;
const idx = image.getPixelIndex(10, 20);
const red = data[idx];const green = data[idx + 1];const blue = data[idx + 2];const alpha = data[idx + 3];
data[idx + 3] = 128; // make the pixel half transparentEdge handling
The third argument says what to do when x or y is outside the image.
It takes a value of the Edge enum, which you import from jimp:
| Member | Value | Coordinates outside the image |
|---|---|---|
Edge.EXTEND |
1 |
Clamped to the nearest edge pixel. The default. |
Edge.WRAP |
2 |
Wrapped around to the other side, as if the image were tiled. |
Edge.CROP |
3 |
Not clamped: the result is -1, so you can skip the pixel or treat it as transparent. |
import { Jimp, Edge } from "jimp";
const image = new Jimp({ width: 3, height: 2 });
image.getPixelIndex(1, 1); // 16image.getPixelIndex(-1, 0); // 0, clamped to (0, 0)image.getPixelIndex(-1, 0, Edge.WRAP); // 8, wrapped to (2, 0)image.getPixelIndex(-1, 0, Edge.CROP); // -1, outside the imageCheck whether a point is inside the image
Edge.CROP is a quick bounds check:
const inside = image.getPixelIndex(x, y, Edge.CROP) !== -1;Try it
Every pixel is read from (x - shift x, y - shift y) with getPixelIndex.
Switch edgeHandling to see how each mode fills the pixels that come from outside the image: EXTEND repeats the edge, WRAP tiles the image and CROP leaves them transparent.
const source = image.clone()Shift the image by reading each pixel from somewhere else. The edge mode decides what fills the gap.
import { Jimp } from "jimp"; const image = await Jimp.read("dice.png"); // Change an option to run it await image.write("output.png");
Notes
- Coordinates are rounded to the nearest whole pixel before the index is computed.
-1means outside. OnlyEdge.CROPreturns-1.Edge.EXTENDandEdge.WRAPalways return an index inside the image (unless the image is empty).Edge.WRAPwraps any distance, in every direction: on an image 3 pixels wide,-4and5both wrap to column2.0or no value meansEdge.EXTEND.- Arguments must be numbers. Anything else, including
NaN, throwsx and y must be numbers. getPixelColorusesEdge.EXTEND, so it reads the nearest edge pixel for coordinates outside the image.setPixelColorusesEdge.CROPand skips them.scanandscanIteratoralready pass each pixel’s index to you, so you rarely needgetPixelIndexinside them.- Changed in 2.0:
Edgeis exported fromjimpas a value, soEdge.WRAPworks at run time; before, it was a type-only export. - Changed in 2.0:
Edge.WRAPwraps coordinates any distance outside the image; before, a coordinate more than one image width or height to the left or top returned-1. - Changed in 2.0:
NaNcoordinates throw; before, they returned index0.