justadudewhohacks / opencv4nodejs

Nodejs bindings to OpenCV 3 and OpenCV 4
MIT License
4.94k stars 820 forks source link

Set PNG compression level in imwrite #541

Open fk242 opened 5 years ago

fk242 commented 5 years ago

I was trying to set PNG compression level when writing image to a file with imwriteAsync, but haven't found a way to do it.

In Python, that would be:

cv2.imwrite(filename, img, [cv2.IMWRITE_PNG_COMPRESSION, 9])

Is it possible to set this option in opencv4nodejs? Thank you.

stoefln commented 5 years ago

@fk242 thats how I do it:

const encodeParams = [cv.IMWRITE_PNG_COMPRESSION, quality]
const buffer = await cv.imencodeAsync('.png', mat, encodeParams)
fs.writeFileSync(filepath, buffer)
fk242 commented 5 years ago

That works, thank you!

lemoun commented 3 years ago

Strangely, compression through imwrite doesn't work after the resize() method has been called on the image file. @stoefln solution works well in this case.

stoefln commented 2 years ago

Just noticed the same problem. Seems to be a bug.