mapbox / pixelmatch

The smallest, simplest and fastest JavaScript pixel-level image comparison library
ISC License
6.15k stars 305 forks source link

Image Size doesn't match #100

Closed MrBiscuit closed 2 years ago

MrBiscuit commented 2 years ago

I get countless Image data size does not match width/height and Image sizes do not match error even calling the function as simple as pixelmatch(new Uint8Array(2),new Uint8Array(2),null) assigning width height or not

Even passing in the same image for both img1 and img2 arguments, will forever raise this Image data size does not match width/height error no matter what.

Please help, I never get this to work

MrBiscuit commented 2 years ago
Screen Shot 2021-11-03 at 12 04 34 PM Screen Shot 2021-11-03 at 12 06 37 PM
mourner commented 2 years ago

The input for image data assumes 4 values (R, G, B, A) for each pixel. This is why the library has this condition on input:

https://github.com/mapbox/pixelmatch/blob/b9261a447515f5aff37a15cfab9f4a491868f720/index.js#L23

Make sure your input images data lenth equals width * height * 4.

afar-ublox commented 4 months ago

I was using sharp package; and during test i was getting "Image Size doesn't match" Error and i fixed it using .ensureAlpha() referenced from https://github.com/mapbox/pixelmatch/issues/114 e.g

const boxedBuffer1 = await sharp1 .resize({ width: maximumWidth, height: maximumHeight, fit: 'contain', position: 'left top' }) .raw() .ensureAlpha() .toBuffer();