Jimp (JavaScript Image Manipulation Program) is a library for loading, editing and saving images in Node.js and the browser. It's written entirely in JavaScript, so it runs anywhere JavaScript does, with no native dependencies to install.
Jimp supports these image formats out of the box:
WEBP and AVIF are available as WASM plugins. See WEBP / WASM.
Jimp's format support is built on JavaScript implementations of each codec. They aren't optimized for speed, and decoding a large image can allocate a lot of memory up front.
By default, JPEG, GIF, TIFF and BMP images larger than 100 megapixels are rejected. See Reading Large Images to raise the limits.
Working with Jimp has three steps.
Load an image.
import { Jimp } from "jimp";
const image = await Jimp.read("test/image.png");Manipulate the image.
image.resize({ w: 100 });Save the image.
await image.write("test/output.png");Jimp.read accepts a file path or URL, a Buffer or an ArrayBuffer. write picks the output format from the file extension.
This playground runs the same steps in your browser. Click a method to apply it to the image. The code panel shows the equivalent program.
Manipulation methods change the image in place and return it, so you can call several in a row:
const image = await Jimp.read("test/image.png");
image
.resize({ w: 300 })
.rotate(90)
.greyscale();
await image.write("test/output.jpg");
To keep the original, call clone() first and edit the copy:
const thumbnail = image.clone().resize({ w: 64 });
Besides write, you can get the encoded image as a buffer or a data URL. All export methods return promises.
const buffer = await image.getBuffer("image/jpeg", { quality: 80 });
const dataUrl = await image.getBase64("image/png");
Jimp runs anywhere JavaScript does: in Node.js, in the browser and in web workers. The jimp package ships a pre-bundled browser build, so in the browser you import jimp as usual and your bundler picks the right file.
Every playground on this site is built this way. The one below uses the browser build from a CDN.
There are a few ways to get an image into Jimp in the browser.
The browser build at dist/browser/index.js is a self-contained ES module, so you can import it from a CDN without a bundler.
<script type="module">
import { Jimp } from "https://cdn.jsdelivr.net/npm/jimp@1/dist/browser/index.js";
const image = await Jimp.read("/some/url");
image.greyscale();
document.querySelector("img").src = await image.getBase64("image/png");
</script>
The same file is on unpkg at https://unpkg.com/jimp@1/dist/browser/index.js. jimp@1 always loads the latest v1 release. Pin an exact version such as jimp@1.7.0 so an update never changes your page.
The jimp/fonts export isn't self-contained, so it can't be imported from a CDN. Load fonts by URL with loadFont instead (see Using fonts).
Jimp.read fetches the image when you give it a URL.
import { Jimp } from "jimp";
// A file on the same domain
const image1 = await Jimp.read("/some/url");
// A file on a different domain (the server must allow CORS)
const image2 = await Jimp.read("https://some.other.domain/some/url");
Read the file into an ArrayBuffer and pass it to Jimp.fromBuffer. This example converts an upload to greyscale. The Upload button in the playgrounds works the same way.
function handleFile(e: Event) {
const input = e.target as HTMLInputElement;
const reader = new FileReader();
reader.onload = async (e) => {
const image = await Jimp.fromBuffer(e.target?.result as ArrayBuffer);
image.greyscale();
const base64 = await image.getBase64("image/jpeg");
};
reader.readAsArrayBuffer(input.files[0]);
}
input.addEventListener("change", handleFile);
Jimp.fromBitmap accepts ImageData, so you can read pixels from a canvas, edit them and write them back.
const canvas = document.getElementById("my-canvas");
const ctx = canvas.getContext("2d");
// Load the canvas into a Jimp instance
const image = Jimp.fromBitmap(
ctx.getImageData(0, 0, canvas.width, canvas.height)
);
// Manipulate the image
image.greyscale();
const imageData = new ImageData(
new Uint8ClampedArray(image.bitmap.data),
image.bitmap.width,
image.bitmap.height
);
// Write back to the canvas
ctx.putImageData(imageData, 0, 0);
Jimp can load bitmap fonts (.fnt files) from a URL with loadFont. In the browser you need to host the font files yourself: the fonts bundled with the Node.js version aren't available.
import { loadFont } from "jimp";
const font = await loadFont("/fonts/open-sans-32-black.fnt");
Pass the loaded font to image.print to draw text.
Image processing can block the main thread long enough to freeze your UI. Running Jimp in a worker keeps the page responsive.
First, define a worker. This is where you import Jimp and transform the image.
import { Jimp } from "jimp";
const ctx: Worker = self as any;
ctx.addEventListener("message", async (e) => {
// Load the image
const image = await Jimp.fromBuffer(e.data.image);
const options = e.data.options;
// Manipulate the image
if (options.blur) {
image.blur(options.blur);
}
// Send the result back
ctx.postMessage({ base64: await image.getBase64("image/png") });
});
Then send it an image from the main thread:
const fileData: ArrayBuffer = await file.arrayBuffer();
const worker = new Worker(new URL("./jimp.worker.ts", import.meta.url), {
type: "module",
});
worker.postMessage({
image: fileData,
options: { blur: 8 },
});
worker.addEventListener("message", (e) => {
img.src = e.data.base64;
});
Jimp decodes images with JavaScript implementations of each format. To avoid running out of memory on huge or malicious files, most decoders refuse images above a size limit. To read an image larger than the defaults, pass decode options to Jimp.read or Jimp.fromBuffer.
The second argument is an object keyed by MIME type. Jimp detects the image's type and passes only the matching options to its decoder, so you can set options for several formats at once.
import { Jimp } from "jimp";
const image = await Jimp.read("huge-photo.jpg", {
"image/jpeg": { maxResolutionInMP: 200, maxMemoryUsageInMB: 1024 },
"image/gif": { maxResolutionInMP: 200 },
"image/tiff": { maxResolutionInMP: 200 },
"image/bmp": { maxResolutionInMP: 200 },
});
When an image is over a limit, decoding throws an error instead of allocating memory for it.
Resolution limits are in megapixels (width × height ÷ 1,000,000). A 100 megapixel limit allows a 10,000 × 10,000 image.
image/jpeg)| Option | Default | Description |
|---|---|---|
maxResolutionInMP | 100 | Largest image, in megapixels, that will be decoded. |
maxMemoryUsageInMB | 512 | Largest amount of memory, in megabytes, the decoder may allocate. |
tolerantDecoding | true | Decode corrupt or truncated files as far as possible instead of throwing. |
colorTransform | auto | Whether to convert from YCbCr to RGB. By default this is chosen from the image's color model. |
A large JPEG can hit maxMemoryUsageInMB before maxResolutionInMP, so raise both when needed.
| Format | Option | Default | Description |
|---|---|---|---|
image/gif | maxResolutionInMP | 100 | Largest image that will be decoded. Each frame is also checked. |
image/tiff | maxResolutionInMP | 100 | Largest image that will be decoded. Each tile is also checked. |
image/bmp | maxResolutionInMP | 100 | Largest image that will be decoded. |
image/png)PNG has no size limit.
| Option | Default | Description |
|---|---|---|
checkCRC | true | Throw if a chunk's checksum doesn't match. |
Cameras and phones store metadata in JPEG files as EXIF: the camera make and model, the resolution (DPI), the date, the orientation and often the GPS location. Jimp reads EXIF from JPEG images. Other formats are read without it.
Cameras often store the pixels sideways and record how to turn them in the EXIF Orientation tag. When Jimp.read or Jimp.fromBuffer loads a JPEG with an orientation other than 1, Jimp rotates and flips the pixels so the image is upright, then sets the orientation to 1. image.width and image.height are the size after rotation.
import { Jimp } from "jimp";
// Stored as 3000x4000 with orientation 6 (rotate 90° clockwise)
const image = await Jimp.read("photo.jpg");
image.width; // 4000
image.height; // 3000
There's no option to turn this off yet (see #214). To find the original orientation, parse the EXIF yourself with exif-parser, which Jimp uses:
import { readFile } from "node:fs/promises";
import ExifParser from "exif-parser";
const buffer = await readFile("photo.jpg");
const { Orientation = 1 } = ExifParser.create(buffer).parse().tags;
The parsed EXIF is on image._exif, in the format exif-parser returns. It isn't part of Jimp's typed API, so TypeScript needs a cast.
const image = await Jimp.read("photo.jpg");
const { tags } = (image as any)._exif;
tags.Make; // "Canon"
tags.XResolution; // 240
tags.Orientation; // 1, as the pixels were rotated
_exif is only set for JPEG images. image.clone() copies it. Changing _exif doesn't change what Jimp writes.
By default Jimp writes no EXIF. To keep the metadata of a JPEG you read, pass exif: true when writing a JPEG:
const image = await Jimp.read("photo.jpg");
image.resize({ w: 800 });
await image.write("small.jpg", { quality: 90, exif: true });
// or
const buffer = await image.getBuffer("image/jpeg", { exif: true });
Jimp writes the original EXIF block with these changes:
Orientation is set to 1, as the pixels were already rotated on read.PixelXDimension and PixelYDimension are set to the image's current size.Everything else, including maker notes and GPS location, is copied unchanged. Leave the option off when publishing images whose location or camera details shouldn't be shared.
Only JPEG output can carry EXIF. PNG, WebP, TIFF, GIF and BMP output never includes it.
There are two kinds of plugins you can write for Jimp: image format plugins and image manipulation plugins. You can combine plugins to create a Custom Jimp.
A format plugin loads and saves images in one format. For example, @jimp/js-bmp reads and writes BMP files.
A format plugin is a function that returns a MIME type and two functions: encode and decode.
import { Format } from "@jimp/types";
import WEBP, { WEBPOptions } from "webp-js";
export default function webp() {
return {
mime: "image/webp",
// Encode the bitmap as a WEBP image
encode: (bitmap, options: WEBPOptions = {}) =>
WEBP.encode(bitmap, options.quality).data,
// Decode a WEBP image into a bitmap
decode: (data) => WEBP.decode(data),
} satisfies Format<"image/webp">;
}
A manipulation plugin exports a methods object. Each method takes the image as its first argument, changes it in place and returns it. For example, @jimp/plugin-blur adds blur and gaussian.
import { JimpClass } from "@jimp/types";
export const methods = {
/**
* Sets the blue channel of every pixel to 255.
*
* @example
* ```ts
* image.maxBlue();
* ```
*/
maxBlue<I extends JimpClass>(image: I) {
image.scan((x, y, idx) => {
image.bitmap.data[idx + 2] = 255;
});
return image;
},
};
Jimp's own methods follow this pattern, and plugins should too:
image), like blur(5).resize({ w: 100 }).If you don't need all the plugins and formats included in the jimp package, you can build your own Jimp with only the ones you use.
Pass the plugins and formats you want to createJimp from @jimp/core.
import { createJimp } from "@jimp/core";
import png from "@jimp/js-png";
import * as resize from "@jimp/plugin-resize";
const MyJimp = createJimp({
plugins: [resize.methods],
formats: [png],
});
const image = await MyJimp.read("test/image.png");
image.resize({ w: 100 });
await image.write("test/output.png");
Jimp's packages import Buffer from the buffer package instead of relying on the Node.js global. In Node.js, Deno and Bun this resolves to the built-in module, and bundlers targeting the browser use the npm package. So a custom Jimp works in browsers and in Cloudflare Workers without a Buffer polyfill.
The jimp package ships a pre-bundled browser build. A custom Jimp is bundled by your own bundler instead. @jimp/js-png has a browser export condition that uses the browser build of pngjs, which Vite, webpack, esbuild and Rollup pick up automatically.
Older versions of @jimp/core and the format plugins use the global Buffer. If you see ReferenceError: Buffer is not defined, update your Jimp packages.
A custom Jimp works in Workers without extra configuration. The WASM format plugins don't currently load in Workers on their own; see WEBP / WASM. If other dependencies need Node.js APIs, enable the nodejs_compat flag:
compatibility_flags = ["nodejs_compat"]
The default build of Jimp only includes image formats written in JavaScript. To use WEBP, or any format without a JavaScript implementation, add a WASM format plugin and create a custom Jimp.
The WASM plugins aren't dependencies of jimp, so install the ones you need alongside @jimp/core:
npm install jimp @jimp/core @jimp/wasm-webp
import { createJimp } from "@jimp/core";
import { defaultFormats, defaultPlugins } from "jimp";
import webp from "@jimp/wasm-webp";
// A custom Jimp that supports WEBP
const Jimp = createJimp({
formats: [...defaultFormats, webp],
plugins: defaultPlugins,
});
Vite 7 and earlier need some config for the WASM plugins. Vite 8 handles this on its own.
import { defineConfig } from "vite";
export default defineConfig({
// The @jsquash codecs load their wasm relative to their own files
optimizeDeps: {
exclude: ["@jsquash/avif", "@jsquash/jpeg", "@jsquash/png", "@jsquash/oxipng", "@jsquash/webp"],
},
// Needed for @jimp/wasm-avif's multi-threaded encoder
worker: { format: "es" },
});
optimizeDeps.exclude stops Vite pre-bundling the codecs away from their .wasm files. Without it you'll see errors like both async and sync fetching of the wasm failed.worker.format is only needed for @jimp/wasm-avif.Workers can't fetch .wasm files by URL. Import them in your worker instead and pass them to the plugin with the wasm option:
import { createJimp } from "@jimp/core";
import webp from "@jimp/wasm-webp";
import webpDecoder from "@jsquash/webp/codec/dec/webp_dec.wasm";
import webpEncoder from "@jsquash/webp/codec/enc/webp_enc_simd.wasm";
const Jimp = createJimp({
formats: [() => webp({ wasm: { decoder: webpDecoder, encoder: webpEncoder } })],
});
| Plugin | Format |
|---|---|
@jimp/wasm-avif | AVIF |
@jimp/wasm-jpeg | JPEG (MozJPEG) |
@jimp/wasm-png | PNG, with optional OxiPNG optimisation |
@jimp/wasm-webp | WEBP |
Jimp v1 makes the library easier to use in any environment and its API more consistent. Most changes fall into a few groups: everything is async, methods take options objects, and constants became named exports.
All export methods are async and callbacks are gone. They've also been renamed:
getBufferAsync → getBuffergetBase64Async → getBase64writeAsync → write// v0
Jimp.read("image.png", (err, image) => {
if (err) throw err;
image.write("output.png");
});
// v1
const image = await Jimp.read("image.png");
await image.write("output.png");
Jimp no longer has a default export. Use the named export:
import { Jimp } from "jimp";
@jimp/custom and @jimp/plugins are gone. To build a Jimp with only some plugins and formats, use createJimp from @jimp/core (see Custom Jimp).
Most methods took positional arguments in v0. In v1 they take an options object, which is easier to read and extend.
// v0
image.resize(100, 100);
// v1
image.resize({ w: 100, h: 100 });
The constructor takes an options object and no longer loads files. Use Jimp.read, Jimp.fromBuffer or Jimp.fromBitmap instead.
const blank = new Jimp({ width: 100, height: 100, color: 0xff0000ff });
const fromDisk = await Jimp.read("test/image.png");
const fromBuffer = await Jimp.fromBuffer(buffer);
Encoder options such as JPEG quality are passed when you export, instead of being set on the image:
// v0
await image.resize(512, Jimp.AUTO).quality(80).getBufferAsync(Jimp.MIME_JPEG);
// v1
await image.resize({ w: 512 }).getBuffer("image/jpeg", { quality: 80 });
Jimp.AUTO is no longer needed. MIME types moved to JimpMime, and the other constants are enums exported from jimp:
| v0 | v1 |
|---|---|
Jimp.MIME_JPEG, … | JimpMime.jpeg, … |
Jimp.RESIZE_BEZIER, … | ResizeStrategy.BEZIER, … |
Jimp.HORIZONTAL_ALIGN_LEFT, … | HorizontalAlign.LEFT, … |
Jimp.VERTICAL_ALIGN_TOP, … | VerticalAlign.TOP, … |
Jimp.BLEND_SOURCE_OVER | BlendMode.SRC_OVER |
Jimp.FONT_SANS_32_BLACK | SANS_32_BLACK from jimp/fonts |
In v0, brightness took a value from -1 to +1 where 0 meant no change. Since v1.1.2 it multiplies each channel, so 1 means no change. For darkening, 1 + v0Value gives the same result:
// v0 image.brightness(-0.25); // v1 image.brightness(0.75);
To reproduce v0's lightening exactly, use the tint action of color, e.g. color([{ apply: "tint", params: [25] }]).
Jimp.intToRGBA → import { intToRGBA } from "jimp"image.getWidth() / getHeight() → image.width / image.heightimage.getMIME() → image.mimeJimp.loadFont → import { loadFont } from "jimp"If webpack reports export 'methods' was not found in '@jimp/plugin-*', v1 is being bundled with v0 copies of its @jimp/* packages, usually because another dependency still uses Jimp v0. Run npm ls @jimp/core to find it, and make sure webpack's resolve.modules lists "node_modules" before any absolute path.