jimp: Failing to read large files

I’m trying to use Jimp to process images that are about 70-100 MB in size. However, I get the following error

// image is 87.7 MB
Jimp.read('big-image', (err, img) => {
  console.log(err)
})
> Error: maxMemoryUsageInMB limit exceeded by at least 226MB

Is there any way around this?

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 17
  • Comments: 30

Commits related to this issue

Most upvoted comments

i found the solutions above would only partially work and further transforms like crop would error

This is what worked for me

const cachedJpegDecoder = Jimp.decoders['image/jpeg']
Jimp.decoders['image/jpeg'] = (data) => {
  const userOpts = { maxMemoryUsageInMB: 1024 }
  return cachedJpegDecoder(data, userOpts)
}

I have same problem. So I tried fix something. [(https://developer.aliyun.com/mirror/npm/package/jpeg-js)] On this site, there are maxMemoryUsageInMB option on decode function.

And I change \node_modules@jimp\core\dist\utils\image-bitmap.js:196 line.

this.bitmap = this.constructor.decoders[_mime](data); to this.bitmap = this.constructor.decoders[_mime](data, {maxMemoryUsageInMB: 2000});

Then I can read the large image. Try this one.

The solutions described here weren’t working for me. I tried 1024, 2048, even 4096. In every case, the error said the limit has been exceeded by 466MP:

throw new Error(`maxResolutionInMP limit exceeded by ${exceededAmount}MP`);
                    ^

Error: maxResolutionInMP limit exceeded by 466MP
    at constructor.parse (/Users/me/projects/my-app/node_modules/jpeg-js/lib/decoder.js:738:21)
    at Object.decode (/Users/me/projects/my-app/node_modules/jpeg-js/lib/decoder.js:1109:11)
    at Object.Jimp.decoders.image/jpeg (/Users/me/projects/my-app/src/ingest-images.js:11:14)
    at Jimp.parseBitmap (/Users/me/projects/my-app/node_modules/@jimp/core/dist/utils/image-bitmap.js:196:53)
    at Jimp.parseBitmap (/Users/me/projects/my-app/node_modules/@jimp/core/dist/index.js:431:32)
    at /Users/me/projects/my-app/node_modules/@jimp/core/dist/index.js:373:15

The solution was to override the maxResolutionInMP value in addition to the memory value. This works for me:

const Jimp = require('jimp')

// add support for very large image files
const JPEG = require('jpeg-js')
Jimp.decoders['image/jpeg'] = (data) => JPEG.decode(data, {
	maxMemoryUsageInMB: 6144,
	maxResolutionInMP: 600
})

You can fix by overriding the jpeg-js decoder jimp uses as follows:

Jimp.decoders['image/jpeg'] = (data: Buffer) => JPEG.decode(data, { maxMemoryUsageInMB: 1024 });

Same issue with 0.16.1

I’m having the same issue.

Our problem has only been reported from users with a specific phone model (Huawei P30). We’re using JIMP to compress and resize images taken from a native mobile application.

Running version: "jimp": "^0.16.0"

I get this issue in 0.16.0 but not 0.9.8.

i found the solutions above would only partially work and further transforms like crop would error

This is what worked for me

const cachedJpegDecoder = Jimp.decoders['image/jpeg']
Jimp.decoders['image/jpeg'] = (data) => {
  const userOpts = { maxMemoryUsageInMB: 1024 }
  return cachedJpegDecoder(data, userOpts)
}

@kane-mason’s solution if you’re using typescript you might have to add <any> pre to jimp.decoders[“image/jpeg”] as the default constructor only expects one argument.

const cachedJpegDecoder = <any>jimp.decoders['image/jpeg']

Just a question. How will you handle memory for ‘image/tiff’ images ?

Is the following correct for NodeJS ?

const jimpJPEG = require('jpeg-js')
const jimpTIFF = require('utif')

jimp.decoders['image/jpeg'] = (data) => {
	return jimpJPEG.decode(data, {
		maxMemoryUsageInMB: 1024
	})
}
jimp.decoders['image/tiff'] = (data) => {
	var ifds = jimpTIFF.decode(data, {
		maxMemoryUsageInMB: 1024
	})
	var page = ifds[0]
	jimpTIFF.decodeImages(data, ifds)
	var rgba = jimpTIFF.toRGBA8(page)
	return {
		data: Buffer.from(rgba),
		width: page.t256[0],
		height: page.t257[0]
	}
}

You put it before you use any Jimp operations - i.e.:

import JPEG from 'jpeg-js';

constructor() {     
Jimp.decoders['image/jpeg'] = (data: Buffer) => JPEG.decode(data, { maxMemoryUsageInMB: 1024 });
 }

myMethod(image: Buffer) {
    const jimpImage = await Jimp.read(jpegBuffer);
}

Downgrading to 0.9.8 worked for me as well.

try using https://github.com/lovell/sharp

Yup, I have migrated to https://sharp.pixelplumbing.com/

I have tried to read buffer of an 2.5MB image and got this error.

I have same problem. So I tried fix something. [(https://developer.aliyun.com/mirror/npm/package/jpeg-js)] On this site, there are maxMemoryUsageInMB option on decode function.

And I change \node_modules@jimp\core\dist\utils\image-bitmap.js:196 line.

this.bitmap = this.constructor.decoders[_mime](data); to this.bitmap = this.constructor.decoders[_mime](data, {maxMemoryUsageInMB: 2000});

Then I can read the large image. Try this one.

This works locally but not working with the live app. How do I fix it