jimp icon indicating copy to clipboard operation
jimp copied to clipboard

Failing to read large files

Open pillowfication opened this issue 3 years ago • 30 comments

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?

pillowfication avatar Jul 24 '20 09:07 pillowfication

I get this issue in 0.16.0 but not 0.9.8.

IanStewart-Celayix avatar Aug 20 '20 18:08 IanStewart-Celayix

same issue heare (0.16.1)

Grewamor avatar Sep 07 '20 08:09 Grewamor

Same issue with 0.16.1

gregholland avatar Sep 08 '20 18:09 gregholland

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.

kde12327 avatar Sep 09 '20 10:09 kde12327

Having the same issue, 10mb jpgs turn into 1.1gb+ ram usage and crash with Error: maxMemoryUsageInMB limit exceeded Happens on newest jimp and the newest windows server

master117 avatar Sep 21 '20 19:09 master117

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"

bergiusj avatar Sep 22 '20 12:09 bergiusj

Same problem here, any update?

joelcoronah avatar Sep 24 '20 16:09 joelcoronah

I get this issue in 0.16.0 but not 0.9.8.

worked for me.

ajitStephen avatar Sep 25 '20 05:09 ajitStephen

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

chibuike-vincent avatar Oct 23 '20 12:10 chibuike-vincent

Downgrading to 0.9.8 worked for me as well.

githubBrandon avatar Oct 29 '20 02:10 githubBrandon

if error is maxResolutionInMP limit exceeded by 36MP

 this.bitmap = this.constructor.decoders[_mime](data, {
        maxResolutionInMP:400
      });

friddle avatar Dec 23 '20 03:12 friddle

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

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

hisham avatar Feb 08 '21 00:02 hisham

@hisham where do we put this code? I converted it to regular js like so

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

But I get JPEG undefined. Can you help? Thank you

ianwieds avatar Mar 09 '21 08:03 ianwieds

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);
}

hisham avatar Mar 09 '21 16:03 hisham

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]
	}
}

1Map avatar Mar 16 '21 10:03 1Map

Any update? problem is here: node_modules/@jimp/core/dist/utils/image-bitmap.js -> node_modules/jpeg-js/lib/decoder.js

isikhi avatar Mar 29 '21 14:03 isikhi

Having this same problem too. Is there any major difference between the 0.9 and 0.16 versions?

koji98 avatar Apr 20 '21 22:04 koji98

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
})

tomprogers avatar Sep 06 '21 18:09 tomprogers

same problem, still happening for 16mb jpegs

tomprogers hack worked great for me, but having jimp use 200mb of memory for a 10mb jpeg is not ideal

smirea avatar Sep 08 '21 22:09 smirea

this is lots of fun with 100mb pro photos

Dakuan avatar Oct 04 '21 10:10 Dakuan

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 avatar Nov 12 '21 14:11 kane-mason

What should be the maximum size range (px or size) of an image? How can I calculate the range of image sizes?

kahilkubilay avatar Apr 06 '22 05:04 kahilkubilay

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

rcupic avatar Jun 24 '22 06:06 rcupic

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

ajitStephen avatar Jun 24 '22 07:06 ajitStephen

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

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

rcupic avatar Jun 24 '22 07:06 rcupic