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

Reading Large Images

Pass decode options to Jimp, including the size limits for each format.

Jimp decodes images with JavaScript implementations of each format and keeps every pixel in memory. To avoid running out of memory on huge or malicious files, most decoders refuse images above a size limit. If you need to read an image larger than the defaults, pass decode options to Jimp.read or Jimp.fromBuffer.

How much memory an image needs

A decoded image takes four bytes per pixel (red, green, blue and alpha), however small the file is.

Image Megapixels Decoded size
1920 × 1080 2.1 8 MB
6000 × 4000 (a 24 MP photo) 24 96 MB
10,000 × 10,000 100 400 MB

The decoder needs working memory on top of that, and every clone needs as much again. A small, highly compressed file can still describe an enormous image, which is why the limits exist.

Passing decode options

The second argument to Jimp.read and Jimp.fromBuffer 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:

example.ts
import { Jimp } from "jimp";
const image = await Jimp.read("huge-photo.jpg", {
"image/jpeg": { maxResolutionInMP: 150, maxMemoryUsageInMB: 4096 },
"image/gif": { maxResolutionInMP: 150 },
"image/tiff": { maxResolutionInMP: 150 },
"image/bmp": { maxResolutionInMP: 150 },
});

The keys and option names are typed: TypeScript only accepts options for the formats your Jimp class supports. The same object also takes exifRotate, which isn’t a size option and applies to every format.

Options by format

Resolution limits are in megapixels (width × height ÷ 1,000,000). A 100 megapixel limit allows a 10,000 × 10,000 image.

JPEG (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 files as far as possible instead of throwing.
allowTruncated false Decode files that were cut off, leaving the missing part gray, 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. The decoder counts its working buffers as well as the pixels: about 5.5 MB per megapixel for a typical photo (a baseline JPEG with 4:2:0 subsampling), and up to 13 MB for a progressive JPEG without subsampling (16 MB if it’s a rare 12-bit one). So the default of 512 MB is enough for any JPEG of up to 32 megapixels, and for most photos of up to 90:

Largest JPEG to read maxResolutionInMP maxMemoryUsageInMB
32 megapixels 100 (default) 512 (default)
100 megapixels 100 (default) 2048
150 megapixels 150 3072
250 megapixels 250 4096

The limit isn’t memory Jimp sets aside: a JPEG only uses what it needs, so a higher limit doesn’t slow down smaller images. It does let larger files through, so keep it as low as your images allow if you read untrusted uploads.

GIF (image/gif)

Option Default Description
maxResolutionInMP 100 Largest image, in megapixels, that will be decoded. Each frame is also checked.

With Jimp.readFrames, every frame is a full-size image, so the limit applies to all the frames together: a 1000 × 1000 GIF can have up to 100 frames. Pass maxFrames to decode fewer.

TIFF (image/tiff)

Option Default Description
maxResolutionInMP 100 Largest image, in megapixels, that will be decoded. Each tile is also checked.

With Jimp.readFrames, the limit applies to all the pages together. Pass maxFrames to decode fewer.

BMP (image/bmp)

Option Default Description
maxResolutionInMP 100 Largest image, in megapixels, that will be decoded.

PNG (image/png)

PNG has no size limit unless you set one.

Option Default Description
maxResolutionInMP none Largest image, in megapixels, that will be decoded. Read from the PNG’s header. Each frame of an APNG is checked.
checkCRC true Throw if a chunk’s checksum doesn’t match.

ICO (image/x-icon)

ICO has no size limit unless you set one. Its other options pick which image in the file to decode:

Option Default Description
maxResolutionInMP none Largest image, in megapixels, that will be decoded. Checked against the chosen image’s header.
size none Decode the smallest image whose longest side is at least this many pixels, or the largest if they’re all smaller.
index none Decode the image at this position in the file.

Without size or index, the largest image is decoded.

JPEG 2000 (image/jp2, image/j2c, image/jpx)

The @jimp/wasm-jpeg2000 plugin can decode a large image at a lower resolution without decoding it in full:

Option Default Description
maxResolutionInMP 100 Largest image, in megapixels, that will be decoded, after reduce.
reduce 0 Decode at a lower resolution. Each step halves the width and height, so 3 decodes at 1/8 size.
layers 0 Decode only the first quality layers, for a quick lower-quality preview. 0 decodes all of them.
example.ts
// A 400 megapixel scan, decoded at 1/4 of the width and height (25 megapixels)
const preview = await Jimp.read("huge-scan.jp2", { "image/jp2": { reduce: 2 } });

SVG (image/svg+xml)

The @jimp/wasm-svg plugin renders the SVG at the size it sets, or at the size you ask for:

Option Default Description
maxResolutionInMP 100 Largest image, in megapixels, that will be rendered. Checked before rendering.
width, height the SVG’s size Render at this width or height, keeping the aspect ratio.
scale 1 Render at a multiple of the SVG’s size.

WebP, AVIF and WASM PNG (image/webp, image/avif, image/png)

@jimp/wasm-webp, @jimp/wasm-avif and @jimp/wasm-png have no size limit unless you set one. They read the size from the file’s header, so a file that’s too large is rejected before the codec runs:

Option Default Description
maxResolutionInMP none Largest image, in megapixels, that will be decoded.

An AVIF can hold several images (a grid and its tiles, a thumbnail); the limit applies to the largest. With a limit set, a WebP or AVIF whose size can’t be read from its header (such as a WebP without a chunk header) is refused rather than decoded unchecked.

Other WASM formats

@jimp/wasm-jpeg takes no decode options, and Jimp applies no size limit to it.

When an image is too large

When an image is over a limit, decoding throws before any pixel memory is allocated, and the promise from Jimp.read or Jimp.fromBuffer rejects:

Format Error message
JPEG maxResolutionInMP limit exceeded by <n>MP
JPEG maxMemoryUsageInMB limit exceeded by at least <n>MB
GIF GIF dimensions <w>x<h> exceed the maximum resolution of <n> megapixels
GIF frames GIF has <count> frames of <w>x<h>, which together exceed the maximum resolution of <n> megapixels. Decode fewer frames with maxFrames, or raise maxResolutionInMP
TIFF TIFF dimensions <w>x<h> exceed the maximum resolution of <n> megapixels
TIFF pages TIFF pages together exceed the maximum resolution of <n> megapixels. Decode fewer pages with maxFrames, or raise maxResolutionInMP
BMP BMP dimensions <w>x<h> exceed the maximum resolution of <n> megapixels
PNG PNG dimensions <w>x<h> exceed the maximum resolution of <n> megapixels
WebP WebP dimensions <w>x<h> exceed the maximum resolution of <n> megapixels
AVIF AVIF dimensions <w>x<h> exceed the maximum resolution of <n> megapixels
WebP, AVIF Couldn't read the WebP's dimensions to check maxResolutionInMP (or the AVIF’s)
ICO ICO dimensions <w>x<h> exceed the maximum resolution of <n> megapixels
JPEG 2000 JPEG 2000 dimensions <w>x<h> exceed the maximum resolution of <n> megapixels. Use the reduce option to decode it at a lower resolution.
SVG SVG dimensions <w>x<h> exceed the maximum resolution of <n> megapixels. Use the width, height or scale option to render it smaller.

If your app accepts uploads, keep the defaults (or lower them) and turn the error into a friendly message:

upload.ts
import { Jimp } from "jimp";
async function readUpload(data: ArrayBuffer) {
try {
return await Jimp.fromBuffer(data, {
"image/jpeg": { maxResolutionInMP: 40, maxMemoryUsageInMB: 1024 },
"image/gif": { maxResolutionInMP: 40 },
"image/tiff": { maxResolutionInMP: 40 },
"image/bmp": { maxResolutionInMP: 40 },
"image/png": { maxResolutionInMP: 40 },
"image/x-icon": { maxResolutionInMP: 40 },
});
} catch (error) {
if (/maxResolutionInMP|maxMemoryUsageInMB|exceed/.test((error as Error).message)) {
throw new Error("That image is too large. Upload one under 40 megapixels.");
}
throw error;
}
}

Working with large images

Once a large image is loaded, a few habits keep memory and time down:

  • Shrink early. Call resize or scaleToFit straight after reading, before any other method. Every later step then works on fewer pixels.
  • Clone sparingly. Each clone copies all the pixels. Clone the small version, not the original.
  • Process one image at a time. Running many large images through Promise.all holds all of them in memory at once.

See also Reading and writing for the other ways to load images, and Errors and validation for Jimp’s other error messages.