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

Formats

The image formats built into jimp, the WASM and community format plugins, and what each one can read and write.

Jimp reads and writes images through format plugins: small objects that know how to decode one MIME type into pixels and encode pixels back. The jimp package includes six formats written in plain JavaScript, so it runs anywhere without native code. For WebP, AVIF, JPEG XL, JPEG 2000 and SVG, or for smaller JPEGs and PNGs, you add WASM format plugins.

Built-in formats

These formats are in the jimp package and in its defaultFormats export:

Format MIME type Package Read Write Transparency
PNG image/png @jimp/js-png yes yes Full alpha
JPEG image/jpeg @jimp/js-jpeg yes yes None
GIF image/gif @jimp/js-gif yes yes On or off: only fully transparent pixels
BMP image/bmp, image/x-ms-bmp @jimp/js-bmp yes yes Read from 16 and 32-bit files; never written
TIFF image/tiff @jimp/js-tiff yes yes Full alpha
ICO image/x-icon, image/vnd.microsoft.icon @jimp/js-ico yes yes Full alpha

When reading, Jimp recognizes the format from the file’s contents. When writing, you choose it: with a MIME type for getBuffer and getBase64, or with the file extension for write.

GIF, TIFF and PNG can also hold several images: the frames of an animation (animated PNGs are called APNGs), or the pages of a scan. See Animated GIFs and multi-page TIFFs.

Try each format on an image with a transparent background:

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

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"); // Click a method above to add it here await image.write("output.png");

PNG

Lossless, with full transparency. The best default for anything that isn’t a photo, and for intermediate files you’ll edit again.

  • Encoder and decoder: @jimp/pngjs, Jimp’s fork of pngjs.
  • Encode options: colorType, filterType, deflateLevel, deflateStrategy, bitDepth, bgColor and icc. By default Jimp picks the smallest format that loses nothing: a palette for images with 256 colors or fewer, grayscale (down to 1 bit per pixel) for gray ones, and no alpha channel for opaque ones. See PNG options.
  • Decode options: checkCRC (default true) throws when a chunk the image needs has the wrong checksum. Like browsers, Jimp ignores bytes after the end of the image, which some tools write, and reads files whose end marker is missing. 16-bit PNGs are decoded to 8 bits per channel.
  • Animation: Jimp.read reads the image that viewers without APNG support show, usually the first frame. Jimp.readFrames reads every frame of an APNG. Jimp doesn’t write APNGs.
  • Color profiles: an iCCP profile is read on decode and written back on encode. See Color Spaces.

JPEG

Lossy and small, for photos. No transparency.

  • Encoder and decoder: @jimp/jpeg-js, Jimp’s maintained fork of jpeg-js. Decoded pixels match libjpeg-turbo, which most browsers and image tools use, exactly.
  • Encode options: quality, from 1 to 100, default 100; chromaSubsampling, which halves the color resolution ("4:2:0") below quality 90 by default; progressive, default false; exif, default false, which writes back the EXIF data of the JPEG the image was read from; and icc, default true. Huffman tables are always optimized for the image. See JPEG options.
  • Decode options: maxResolutionInMP (default 100), maxMemoryUsageInMB (default 512), tolerantDecoding, allowTruncated and colorTransform. See Reading Large Images.
  • EXIF: on read, Jimp parses the EXIF data and rotates the pixels to match the orientation tag, unless you pass exifRotate: false. See EXIF.
  • Color profiles: an ICC profile in the APP2 segments is read on decode and written back on encode.
  • Damaged files: files missing the end-of-image marker, with stray bytes between header segments, a marker that lost its 0xFF or a wrong segment length are decoded anyway, and corrupt image data is recovered from like libjpeg does. Files cut off before the end of the image data throw, unless you pass allowTruncated: true to decode them with the missing part gray, like browsers show them. Lossless and hierarchical JPEGs, which are rare, aren’t supported.

GIF

Up to 256 colors, and animation.

  • Decoder: Jimp’s own, which draws frames the way browsers do. Jimp.read reads the first frame of an animated GIF; Jimp.readFrames reads them all.
  • Encoder: Jimp’s own. getBuffer and write write a single frame; Jimp.encodeFrames and Jimp.writeFrames write an animation. Images with 256 colors or fewer get an exact palette; others are reduced to 256 colors with Wu’s quantization algorithm, without dithering. For control over the palette, call quantize or dither first.
  • Transparency: pixels with an alpha of 0 become transparent. Every other pixel is written as opaque.
  • Encode options: none. Width and height can be at most 65,535. For animations, loop.
  • Decode options: maxResolutionInMP (default 100). With readFrames it limits all frames together.
  • Damaged files: a file cut off part-way through a frame keeps the pixels decoded so far, as browsers show it, and so does a frame with corrupt pixel data. Bytes after the end of the file are ignored.

BMP

Uncompressed bitmaps, mostly for compatibility with older software.

  • Encoder and decoder: bmp-ts.
  • Writing: uncompressed, without transparency. bitPP picks 24 bits per pixel (the default), 16 (5 bits per channel), 32 (with an alpha byte that’s always opaque), or 1, 4 or 8 with a palette of up to 2, 16 or 256 colors.
  • Palettes: pass the colors in palette, and each pixel is written as the nearest one. Without palette, an image with few enough colors keeps them exactly. Otherwise 1-bit files are black and white, and 4 and 8-bit files get a palette from Wu’s quantization algorithm. Pixels aren’t dithered; for that, call dither or quantize first.
  • Reading: 1, 4, 8, 16, 24 and 32-bit files, including run-length encoded ones. Alpha is read from 16 and 32-bit files only.
  • Encode options: bitPP, palette, hr and vr (resolution in pixels per meter), reserved1 and reserved2. See BMP options.
  • Decode options: maxResolutionInMP (default 100).
  • Two MIME types: @jimp/js-bmp registers the same encoder and decoder as image/bmp and as image/x-ms-bmp (its msBmp export), for code that uses the older name. Files are always read as image/bmp.

TIFF

Lossless and uncompressed, used in print and scanning workflows.

  • Encoder and decoder: utif2. Jimp.read reads the first page of a multi-page TIFF; Jimp.readFrames reads them all.
  • Writing: uncompressed 8-bit RGBA, so files are large: 4 bytes per pixel. Jimp.writeFrames writes several pages, which can have different sizes.
  • Reading: the common compression schemes and color models, including CMYK.
  • Encode options: none.
  • Decode options: maxResolutionInMP (default 100). With readFrames it limits all pages together.

ICO

Windows icons and cursors, mostly used for favicons. An ICO file holds several images, usually the same icon at different sizes.

  • Reading: .ico and .cur files, with images stored as PNG or as BMP (1 to 32 bits per pixel, with the transparency mask). Jimp decodes the largest image unless you pick one.
  • Writing: .ico files, each image stored as a 32-bit PNG, so transparency is kept. By default the icon holds one image, scaled down to fit in 256 × 256 if it’s larger.
  • Decode options: size decodes the smallest image whose longest side is at least that size (or the largest, if they’re all smaller), and index decodes the image at that position.
  • Encode options: sizes writes one image per size, each scaled to fit in a square of that size with the aspect ratio kept. See ICO options.
  • Two MIME types: like BMP, @jimp/js-ico registers image/x-icon and image/vnd.microsoft.icon (its msIcon export). Files are read as image/x-icon, and JimpMime.ico is "image/x-icon".
favicon.ts
import { Jimp } from "jimp";
// Read the 32 × 32 image of a multi-size icon
const icon = await Jimp.read("favicon.ico", { "image/x-icon": { size: 32 } });
// Write a favicon with three sizes
const logo = await Jimp.read("logo.png");
await logo.write("favicon.ico", { sizes: [16, 32, 48] });

To list the images in a file, install @jimp/js-ico and call readIcoEntries. It returns { index, width, height, bitDepth, format } for each image, where format is "png" or "bmp":

entries.ts
import { readFile } from "node:fs/promises";
import { readIcoEntries } from "@jimp/js-ico";
readIcoEntries(await readFile("favicon.ico"));
// [{ index: 0, width: 16, height: 16, bitDepth: 32, format: "png" }, ...]

Animated GIFs and multi-page TIFFs

A Jimp image is always a single picture. To work with every frame of an animated GIF or every page of a TIFF, read them as a list of images, change them, and write the list back:

example.ts
import { Jimp } from "jimp";
const { frames, loop } = await Jimp.readFrames("animation.gif");
for (const { image } of frames) {
image.resize({ w: 200 });
}
await Jimp.writeFrames("small.gif", frames, { loop });
Format readFrames encodeFrames / writeFrames
GIF Every frame as a complete image, with its delay in ms and the loop count An animation: same-size frames, a palette and delay each, and a loop count
PNG (APNG) Every frame as a complete image, with its delay in ms and the loop count One frame; more throws
TIFF Every page, with its own size One page per image, sizes can differ
Every other format One frame One frame; more throws

GIF and APNG frames that only update part of the image are drawn over the previous ones, following their disposal methods, so each frame you get is what a viewer would show. See Jimp.readFrames. The WASM formats, including WebP, AVIF and JPEG XL, read and write a single frame.

Transparency and image.background

Before encoding JPEG, BMP, GIF or TIFF, Jimp draws the image over image.background, which is transparent unless you set it. For JPEG and BMP that means transparent areas turn black. Set a background color to choose what they become:

example.ts
image.background = 0xffffffff; // white
await image.write("on-white.jpg");

PNG, ICO and all the WASM formats except JPEG 2000 skip this step.

WASM format plugins

These plugins wrap codecs compiled to WebAssembly: from Squoosh via jSquash for WebP, AVIF, JPEG, PNG and JPEG XL, OpenJPEG for JPEG 2000, and resvg for SVG. They aren’t part of the jimp package. Install the ones you need along with @jimp/core:

npm install @jimp/core @jimp/wasm-webp
Package MIME type Codec Read Write Transparency
@jimp/wasm-webp image/webp libwebp yes yes Full alpha
@jimp/wasm-avif image/avif libavif yes yes Full alpha
@jimp/wasm-jpeg image/jpeg MozJPEG yes yes None
@jimp/wasm-png image/png Squoosh PNG and OxiPNG yes yes Full alpha
@jimp/wasm-jxl image/jxl libjxl yes yes Full alpha
@jimp/wasm-jpeg2000 image/jp2, image/j2c, image/jpx OpenJPEG yes JP2 and J2C, not JPX None
@jimp/wasm-svg image/svg+xml resvg yes no Full alpha

JPEG XL, JPEG 2000 and SVG have their own sections in the WASM plugins guide, with their decode options and limits: JPEG XL, JPEG 2000 and SVG.

Adding a format

Create your own Jimp class with createJimp, passing the default formats and plugins plus the new format:

jimp.ts
import { createJimp } from "@jimp/core";
import { defaultFormats, defaultPlugins } from "jimp";
import webp from "@jimp/wasm-webp";
export const Jimp = createJimp({
formats: [...defaultFormats, webp],
plugins: defaultPlugins,
});
example.ts
import { Jimp } from "./jimp";
const image = await Jimp.read("photo.webp");
await image.write("photo.png");
await image.write("small.webp", { quality: 75 });

The custom class has the same API, and TypeScript knows the new MIME type and its options. The codecs are loaded the first time an image is encoded or decoded in that format, so the first call is slower.

Replacing a built-in format

@jimp/wasm-jpeg and @jimp/wasm-png use the same MIME types as the built-in JPEG and PNG formats. When two formats share a MIME type, Jimp uses the first one in the list, so put the WASM format before the defaults:

jimp.ts
import { createJimp } from "@jimp/core";
import { defaultFormats, defaultPlugins } from "jimp";
import jpeg from "@jimp/wasm-jpeg";
export const Jimp = createJimp({
formats: [jpeg, ...defaultFormats],
plugins: defaultPlugins,
});

With [...defaultFormats, jpeg], the built-in JPEG format would still be used for reading and writing.

Encode options

The options are specific to each codec. They’re validated, so a value out of range throws a validation error listing the problem, for example Too big: expected number to be <=100, and so does an option the encoder doesn’t know. JPEG XL and JPEG 2000 clamp out-of-range numbers instead. The WebP, JPEG and PNG plugins also take icc, which works as for the built-in formats: the image’s ICC profile is written unless you pass false. AVIF and JPEG XL output is always converted to sRGB without a profile.

Option Type Default Description
quality number 100 0 to 100. For lossy, the image quality. For lossless, the compression effort.
lossless 0 | 1 0 1 for lossless encoding. The default is lossy, even at quality 100.
nearLossless number 100 Near-lossless preprocessing, 0 (most loss) to 100 (off).
method number 4 Speed and size trade-off, 0 (fastest) to 6 (slowest, smallest).
alphaQuality number 100 Quality of the alpha channel, 0 to 100 (lossless).
alphaCompression 0 | 1 1 1 compresses the alpha channel losslessly, 0 stores it uncompressed.
alphaFiltering 0 | 1 | 2 1 Predictive filtering for the alpha channel: none, fast or best.
exact 0 | 1 0 1 keeps the RGB values of transparent pixels. By default they may change to compress better.
targetSize number 0 If not 0, aim for this many bytes.
targetPSNR number 0 If not 0, aim for this quality in dB. Takes precedence over targetSize.
snsStrength number 50 Spatial noise shaping, 0 to 100.
filterStrength number 60 Deblocking filter strength, 0 (off) to 100.
filterSharpness number 0 Filter sharpness, 0 (sharpest) to 7.
filterType 0 | 1 1 Filter type: 0 simple, 1 strong.
autoFilter 0 | 1 0 1 adjusts the filter strength automatically.
segments number 4 Number of segments, 1 to 4.
partitions number 0 log2 of the number of token partitions, 0 to 3.
pass number 1 Entropy analysis passes, 1 to 10.
preprocessing 0 | 1 0 1 applies segment-smooth preprocessing.
partitionLimit number 0 Quality loss allowed to fit the 512 KB limit on prediction modes, 0 to 100.
emulateJpegSize 0 | 1 0 1 maps the options to give a size similar to a JPEG of the same quality.
useSharpYuv 0 | 1 0 1 uses a slower, sharper RGB to YUV conversion.
lowMemory 0 | 1 0 1 uses less memory and encodes more slowly. It has no effect on lossless encoding.
icc boolean true Write the image’s ICC profile. false writes sRGB without one, converting the colors first.

Community plugins

Other people publish format plugins for Jimp too. They plug into createJimp the same way as the official ones. They’re maintained by their authors, not by the Jimp team, and the Jimp team doesn’t test them, so check that one works in your setup before you rely on it.

Package Format Author
jimp-heic HEIC (image/heic), read and write @Ralac07

HEIC is the format iPhones save photos in. Jimp doesn’t include it, as there’s no pure JavaScript HEVC decoder and HEVC has patent licensing concerns. jimp-heic uses the elheif WASM build of libheif:

npm install @jimp/core jimp-heic
jimp.ts
import { createJimp } from "@jimp/core";
import { defaultFormats, defaultPlugins } from "jimp";
import heic from "jimp-heic";
export const Jimp = createJimp({
formats: [...defaultFormats, heic],
plugins: defaultPlugins,
});
const image = await Jimp.read("photo.heic");
await image.write("photo.jpg");

Writing Plugins shows how to write a format plugin of your own.

See also