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

getPixelIndex()

Get the position of a pixel in the bitmap's data buffer.

From @jimp/core

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

getPixelIndex.d.ts
image.getPixelIndex(x: number, y: number, edgeHandling?: Edge): number

getPixelIndex 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

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

Edge 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.
example.ts
import { Jimp, Edge } from "jimp";
const image = new Jimp({ width: 3, height: 2 });
image.getPixelIndex(1, 1); // 16
image.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 image

Check whether a point is inside the image

Edge.CROP is a quick bounds check:

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

Try getPixelIndex()
const source = image.clone()

Shift the image by reading each pixel from somewhere else. The edge mode decides what fills the gap.

90px
45px
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

  • Coordinates are rounded to the nearest whole pixel before the index is computed.
  • -1 means outside. Only Edge.CROP returns -1. Edge.EXTEND and Edge.WRAP always return an index inside the image (unless the image is empty).
  • Edge.WRAP wraps any distance, in every direction: on an image 3 pixels wide, -4 and 5 both wrap to column 2.
  • 0 or no value means Edge.EXTEND.
  • Arguments must be numbers. Anything else, including NaN, throws x and y must be numbers.
  • getPixelColor uses Edge.EXTEND, so it reads the nearest edge pixel for coordinates outside the image. setPixelColor uses Edge.CROP and skips them.
  • scan and scanIterator already pass each pixel’s index to you, so you rarely need getPixelIndex inside them.
  • Changed in 2.0: Edge is exported from jimp as a value, so Edge.WRAP works at run time; before, it was a type-only export.
  • Changed in 2.0: Edge.WRAP wraps 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: NaN coordinates throw; before, they returned index 0.