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:
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.
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,bgColorandicc. 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(defaulttrue) 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.readreads the image that viewers without APNG support show, usually the first frame.Jimp.readFramesreads every frame of an APNG. Jimp doesn’t write APNGs. - Color profiles: an
iCCPprofile 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, from1to100, default100;chromaSubsampling, which halves the color resolution ("4:2:0") below quality 90 by default;progressive, defaultfalse;exif, defaultfalse, which writes back the EXIF data of the JPEG the image was read from; andicc, defaulttrue. Huffman tables are always optimized for the image. See JPEG options. - Decode options:
maxResolutionInMP(default100),maxMemoryUsageInMB(default512),tolerantDecoding,allowTruncatedandcolorTransform. 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
APP2segments 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
0xFFor 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 passallowTruncated: trueto 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.readreads the first frame of an animated GIF;Jimp.readFramesreads them all. - Encoder: Jimp’s own.
getBufferandwritewrite a single frame;Jimp.encodeFramesandJimp.writeFrameswrite 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, callquantizeorditherfirst. - Transparency: pixels with an alpha of
0become 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(default100). WithreadFramesit 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.
bitPPpicks 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. Withoutpalette, 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, callditherorquantizefirst. - 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,hrandvr(resolution in pixels per meter),reserved1andreserved2. See BMP options. - Decode options:
maxResolutionInMP(default100). - Two MIME types:
@jimp/js-bmpregisters the same encoder and decoder asimage/bmpand asimage/x-ms-bmp(itsmsBmpexport), for code that uses the older name. Files are always read asimage/bmp.
TIFF
Lossless and uncompressed, used in print and scanning workflows.
- Encoder and decoder: utif2.
Jimp.readreads the first page of a multi-page TIFF;Jimp.readFramesreads them all. - Writing: uncompressed 8-bit RGBA, so files are large: 4 bytes per pixel.
Jimp.writeFrameswrites several pages, which can have different sizes. - Reading: the common compression schemes and color models, including CMYK.
- Encode options: none.
- Decode options:
maxResolutionInMP(default100). WithreadFramesit 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:
.icoand.curfiles, 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:
.icofiles, 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:
sizedecodes the smallest image whose longest side is at least that size (or the largest, if they’re all smaller), andindexdecodes the image at that position. - Encode options:
sizeswrites 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-icoregistersimage/x-iconandimage/vnd.microsoft.icon(itsmsIconexport). Files are read asimage/x-icon, andJimpMime.icois"image/x-icon".
import { Jimp } from "jimp";
// Read the 32 × 32 image of a multi-size iconconst icon = await Jimp.read("favicon.ico", { "image/x-icon": { size: 32 } });
// Write a favicon with three sizesconst 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":
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:
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:
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:
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,});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:
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. |
| Option | Type | Default | Description |
|---|---|---|---|
cqLevel |
number |
0 |
Quantizer level. Lower is higher quality and a bigger file; 0 is the best quality. |
cqAlphaLevel |
number |
0 |
Quantizer level for the alpha channel. |
speed |
number |
6 |
Encoder speed. Higher is faster and gives larger files. |
subsample |
number |
1 |
Chroma subsampling, 0 to 4. |
denoiseLevel |
number |
0 |
Denoising strength. |
sharpness |
number |
0 |
Sharpness. |
tune |
"auto" | "psnr" | "ssim" |
"auto" |
Which quality metric the encoder optimizes for. |
chromaDeltaQ |
boolean |
false |
Use a different quantizer for the color channels. |
tileColsLog2 |
number |
0 |
log2 of the number of tile columns, 0 to 10. |
tileRowsLog2 |
number |
0 |
log2 of the number of tile rows, 0 to 10. |
The numeric options accept 0 to 100, except where noted. Jimp’s defaults favor quality over size. Raise cqLevel for smaller files; @jsquash/avif, which Jimp uses, defaults to 33.
| Option | Type | Default | Description |
|---|---|---|---|
quality |
number |
75 |
0 to 100. |
exif |
boolean |
false |
Write the EXIF data of the JPEG the image was read from, as with the built-in JPEG format. |
icc |
boolean |
true |
Write the image’s ICC profile, as with the built-in JPEG format. |
progressive |
boolean |
true |
Write a progressive JPEG. |
baseline |
boolean |
false |
Force baseline-compatible quantization tables. |
optimizeCoding |
boolean |
true |
Compute optimal Huffman tables. |
arithmetic |
boolean |
false |
Use arithmetic coding, which few decoders support. |
smoothing |
number |
0 |
Smoothing, 0 to 100. |
jpegColorSpace |
"ycbcr" | "rgb" | "grayscale" |
"ycbcr" |
Color space inside the JPEG. |
quantTable |
number |
3 |
Which of MozJPEG’s quantization table presets to use. |
autoSubsample |
boolean |
true |
Choose chroma subsampling automatically. |
chromaSubsample |
number |
2 |
Chroma subsampling factor when autoSubsample is off, 0 to 4. |
separateChromaQuality |
boolean |
false |
Use chromaQuality for the color channels. |
chromaQuality |
number |
75 |
Quality of the color channels when separateChromaQuality is on. |
trellisLoops |
number |
1 |
Trellis quantization passes. |
trellisMultipass |
boolean |
false |
Trellis quantization option. |
trellisOptZero |
boolean |
false |
Trellis quantization option. |
trellisOptTable |
boolean |
false |
Trellis quantization option. |
Unlike the built-in JPEG format, image.background isn’t applied first. To control the color of transparent areas, composite the image onto a solid new Jimp before encoding.
| Option | Type | Default | Description |
|---|---|---|---|
optimize |
object |
none | Run OxiPNG on the encoded PNG to make it smaller. Without it, the PNG isn’t optimized. |
optimize.level |
number |
2 |
Optimization level, 0 to 6. Higher is smaller and slower; above 4 is rarely worth it. |
optimize.interlace |
boolean |
false |
Write an interlaced PNG. Interlacing makes the file bigger. |
optimize.optimiseAlpha |
boolean |
false |
Allow the RGB values of transparent pixels to change to compress better. |
icc |
boolean |
true |
Write the image’s ICC profile, as with the built-in PNG format. |
const bytes = await image.getBuffer("image/png", { optimize: { level: 3 } });| Option | Type | Default | Description |
|---|---|---|---|
quality |
number |
90 |
0 to 100. 90 is visually lossless for most images, and 100 is lossless. |
lossless |
boolean |
false |
Store every pixel exactly, including alpha. quality is ignored. |
effort |
number |
7 |
1 (fastest) to 9 (slowest, smallest files). |
progressive |
boolean |
false |
Order the data so a partly downloaded file can be shown at a lower quality first. Makes files a little larger. |
lossyModular |
boolean |
false |
Use the modular mode for lossy images too, instead of VarDCT. It can do better on graphics, and worse on photos. |
epf |
number |
-1 |
Edge-preserving filter strength for lossy images, 0 (off) to 3. -1 picks one from the quality. |
decodingSpeedTier |
number |
0 |
0 to 4. Higher makes lossy images faster to decode, and larger. |
photonNoiseIso |
number |
0 |
Add synthetic film grain to lossy images, like a photo taken at this ISO. 0 adds none. |
Numbers outside their range are clamped. Output is 8-bit sRGB with alpha. The decode option is maxResolutionInMP (default 100); see JPEG XL for the decoder’s limits.
| Option | Type | Default | Description |
|---|---|---|---|
compressionRatio |
number |
1 |
Target size as a ratio of the uncompressed RGB pixels, for example 20 for 20:1. 1 is lossless. |
decompositions |
number |
5 |
Wavelet levels, which is how many times decoders can halve the image with reduce. Limited to what the image’s size allows. |
Output is always 8-bit RGB without alpha: transparent pixels are drawn over image.background, and images in another color space are converted to sRGB.
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 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
- Reading and writing: the read, encode and write APIs
Jimp.readFramesandJimp.writeFrames: animated GIFs and multi-page TIFFsgetBufferoptions: every option of the built-in encoders- Reading Large Images: decode options and size limits
- Using WebP and other WASM plugins
- Custom Jimp: pick only the formats and plugins you need
- Writing Plugins: add a format of your own
- Color Spaces: which formats keep ICC profiles