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

Reading and writing

Load images from files, URLs, buffers and raw pixels, and encode them again in any supported format.

Every Jimp program has the same shape: get an image into memory, change its pixels, then encode it into a file format again. This page covers the first and last steps: the ways to create an image, the ways to get it out, and the properties that connect them.

example.ts
import { Jimp } from "jimp";
const image = await Jimp.read("input.jpg"); // decode
image.resize({ w: 640 }).greyscale(); // edit the pixels
await image.write("output.png"); // encode

In between, an image is just a grid of RGBA pixels in memory. It isn’t a JPEG or a PNG any more, which is why you can read one format and write another.

Getting an image

There are four ways to create an image. Pick by what you have:

You have Use Async
A file path or a URL Jimp.read(pathOrUrl) yes
The bytes of an image file, or a Blob Jimp.fromBuffer(buffer) yes
Raw RGBA pixels, such as canvas ImageData Jimp.fromBitmap({ width, height, data }) no
Nothing yet: you want a blank canvas new Jimp({ width, height, color }) no

From a path or URL

Jimp.read fetches http:, https:, data: and blob: URLs. In Node, any other string is a file path, relative to the current working directory:

example.ts
const local = await Jimp.read("images/photo.jpg");
const remote = await Jimp.read("https://example.com/photo.jpg");

A missing file throws Node’s own error, such as ENOENT: no such file or directory, open 'images/photo.jpg'. Jimp.read also accepts the bytes of an image, in which case it does the same as Jimp.fromBuffer.

From bytes in memory

When the file is already in memory (an upload, a database blob, a response body), decode it with Jimp.fromBuffer. It takes a Buffer, an ArrayBuffer, a Uint8Array (or any other ArrayBufferView) or a Blob:

example.ts
const response = await fetch("https://example.com/photo.jpg");
const image = await Jimp.fromBuffer(await response.arrayBuffer());

Jimp detects the format from the bytes, so you never pass a MIME type when reading.

From raw pixels

Jimp.fromBitmap wraps pixels that are already decoded: 4 bytes per pixel, in red, green, blue, alpha order.

example.ts
const image = Jimp.fromBitmap({
width: 2,
height: 1,
data: [0xff0000ff, 0x0000ffff], // a red and a blue pixel
});

From scratch

new Jimp creates a blank image filled with one color:

example.ts
const canvas = new Jimp({ width: 800, height: 400, color: "#fff7e0" });

Image properties

Whichever way you create it, an image has the same properties:

Property Type Description
image.width number Width in pixels. Read-only; use resize or crop to change it.
image.height number Height in pixels.
image.bitmap { width: number; height: number; data: Buffer } The pixels: data holds 4 bytes (R, G, B, A) per pixel, row by row. See Colors and pixels.
image.mime string | undefined The MIME type the image was decoded from, such as "image/jpeg". undefined for images from new Jimp or fromBitmap. clone copies it.
image.background number An RGBA color, 0x00000000 (transparent) unless you set it. Used to fill new space in rotate and contain, and behind formats without alpha.
image.colorSpace "srgb" | "display-p3" | "custom" The color space of the pixels, from the image’s ICC profile. "srgb" for images without one. Read-only; convert with toColorSpace.
image.iccProfile Buffer | undefined The ICC profile read from a JPEG, PNG, WebP or JPEG 2000 file, written back to JPEG, PNG and WebP output. See Color Spaces.
example.ts
const image = await Jimp.read("photo.jpg");
console.log(image.width, image.height); // 4000 3000
console.log(image.mime); // "image/jpeg"
console.log(image.bitmap.data.length); // 48000000, 4 bytes per pixel

new Jimp({ color }) sets background to the fill color. You can also assign it at any time:

example.ts
image.background = 0xffffffff; // white

Getting the image out

There are three ways to encode an image. All of them are async and none of them change the image.

You want Use Where
A file on disk image.write(path, options?) Node only
The encoded bytes, as a Buffer image.getBuffer(mime, options?) everywhere
A data: URL for an <img> or for embedding image.getBase64(mime, options?) everywhere
example.ts
import { Jimp, JimpMime } from "jimp";
const image = await Jimp.read("photo.jpg");
await image.write("copy.png"); // the extension picks the format
const bytes = await image.getBuffer(JimpMime.png);
const url = await image.getBase64("image/jpeg", { quality: 80 });

Animations and pages

A Jimp image is one picture. Jimp.readFrames reads every frame of an animated GIF or PNG, or page of a multi-page TIFF, as a list of images, and Jimp.writeFrames or Jimp.encodeFrames write a list back:

example.ts
const { frames, loop } = await Jimp.readFrames("animation.gif");
for (const { image } of frames) {
image.flip({ horizontal: true });
}
await Jimp.writeFrames("flipped.gif", frames, { loop });

MIME types and JimpMime

getBuffer and getBase64 take the output format as a MIME type string. JimpMime has a constant for each of the default formats, which saves typing and catches typos:

example.ts
import { JimpMime } from "jimp";
JimpMime.png; // "image/png"
JimpMime.jpeg; // "image/jpeg"
JimpMime.gif; // "image/gif"
JimpMime.bmp; // "image/bmp"
JimpMime.tiff; // "image/tiff"
JimpMime.ico; // "image/x-icon"

write doesn’t need one: it looks up the MIME type from the file extension.

Encoder options

Each format takes its own options as the last argument of write, getBuffer and getBase64. The ones you’ll use most:

example.ts
import { Jimp, PNGColorType } from "jimp";
const image = await Jimp.read("photo.jpg");
// JPEG: quality 1 to 100 (default 100), and keep the camera's EXIF data
await image.write("small.jpg", { quality: 75, exif: true });
// PNG: drop the alpha channel
await image.write("flat.png", { colorType: PNGColorType.COLOR });
// BMP: 16 bits per pixel, and 72 DPI (in pixels per meter)
await image.write("print.bmp", { bitPP: 16, hr: 2835, vr: 2835 });
// ICO: one image per icon size
await image.write("favicon.ico", { sizes: [16, 32, 48] });

GIF and TIFF take no options. The full lists are on the getBuffer page, and Formats covers what each format can store.

Transparency and the background

Before encoding JPEG, BMP, GIF or TIFF, Jimp draws the image over image.background. JPEG and BMP have no alpha channel at all, so with the default transparent background, transparent areas come out black. PNG, ICO and the WASM formats other than JPEG 2000 skip this step and keep the alpha channel as it is. Set a background color to control what they become:

example.ts
const logo = await Jimp.read("logo.png"); // transparent background
logo.background = 0xffffffff;
await logo.write("logo.jpg"); // on white instead of black

Try it below: the dice image has a transparent background. Switch to JPEG or BMP, then turn on set background.

Try getBuffer()
image.bitmap = (await Jimp.fromBuffer(await image.getBuffer("image/jpeg", { quality: 10 }))).bitmap

Encode the image in a format and decode it again, to see what the format keeps: JPEG quality loss, GIF's 256 colors, lost transparency, ICO's 256 pixel limit.

10
set background
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");

Color profiles

A JPEG, PNG or WebP with an ICC color profile, such as an iPhone photo in Display P3, keeps its profile when you write JPEG, PNG or WebP again. For formats that can’t carry a profile, Jimp encodes a copy converted to sRGB, so the colors look the same everywhere. Pass icc: false to write sRGB without a profile. See Color Spaces.

Read options

Jimp.read, Jimp.fromBuffer and Jimp.readFrames take a second argument with options for the decoders, keyed by MIME type. They’re mostly used to raise the size limits that protect against huge or malicious files:

example.ts
const image = await Jimp.read("panorama.jpg", {
"image/jpeg": { maxResolutionInMP: 150, maxMemoryUsageInMB: 4096 },
});

See Reading Large Images for every format’s options and defaults.

The same object takes exifRotate, which applies to every format. Pass false to keep a JPEG’s pixels as stored instead of rotating them to match its EXIF orientation; see EXIF.

Node and the browser

The same API works in both, with a few differences:

Task Node Browser
Read a file path Jimp.read("photo.jpg") Not available: there’s no file system. Every string is fetched as a URL.
Read a URL Jimp.read(url), using the global fetch Jimp.read(url). Other origins need CORS headers.
Read an upload Jimp.fromBuffer(buffer) Jimp.fromBuffer(file): a File is a Blob
Save a file image.write("out.png") Not available: write throws. Use getBuffer and a Blob download.
Show the result Write a file, or send the bytes from getBuffer img.src = await image.getBase64("image/png")
Pass the bytes on getBuffer returns a Node Buffer getBuffer returns a Buffer from the buffer package, a Uint8Array subclass

The browser guide covers uploads, canvas and web workers in more detail.

See also