jimp icon indicating copy to clipboard operation
jimp copied to clipboard

Bitmap manipulation increasing the file size considerably.

Open RishabhAsthana opened this issue 6 years ago • 14 comments

I am using Jimp for a project and this involves manipulating the bitmap by simple bitwise operations. I use a PNG image as a source, and manipulate its bitmap by changing the lower 2-bits every byte, without ever adding any more bytes. What this leads to is that my input is around 86KB but the same bitmap, when written to an image (using the write() method), is coming out to be about 235KB. Is there any way I can avoid this? I would like to have access to same the bitmap which I was manipulating instead of having a new one created (of a much larger size). Are there any options to preserve the original compression, and image size?

Update: I went back and tried reading the same image, and then just writing it back without any manipulation and I am still noticing the size bloat.

This is the function I am using in my workflow:

function test(){
	Jimp.read("in.png", function (err, image) {
	    if (err) throw err;
        this.write("out.png");
    });
}

Update 2: It seems the original image had a bit-depth of 8 but the output image has a bit-depth of 32, which explains the bloating. Any way to stop this from happening?

RishabhAsthana avatar Apr 24 '18 17:04 RishabhAsthana

I am experiencing this problem too, have you found the way to fix?

DennisJames avatar Aug 21 '18 11:08 DennisJames

@DennisJames @asthana4 any chance you could share a sample image?

hipstersmoothie avatar Sep 05 '18 21:09 hipstersmoothie

@hipstersmoothie test here is one of my sample with size 8.21KB , i run code like : var image = await Jimp.read(file); image.write(newPath + 'test2.' + image.getExtension(), function (err, res) { console.log(err, res); }); then i get a png with size 21.8KB,

DennisJames avatar Sep 07 '18 03:09 DennisJames

The png options werent getting passed correctly so I've opened #604. Once released the following code will allow you to get smaller files.

const fs = require('fs');
const Jimp = require('jimp');

test();

async function test() {
    const image = await Jimp.read('./exampleImages/panda.png');

    await image
      .deflateStrategy(0)
      .filterType(Jimp.PNG_FILTER_NONE)
      .colorType(0)
      .writeAsync('test.png');

    const orig = fs.statSync('./exampleImages/panda.png');
    const result = fs.statSync('test.png');

    console.log(`${orig.size / 1000.0}kb`, `${result.size / 1000.0}kb`);
}

Output:

8.413kb 8.19kb

Note: colorType 0 makes it greyscale. This works for this image but maybe not other. Play around with the functions listed here

EDIT: Released in v0.5.0

hipstersmoothie avatar Sep 08 '18 10:09 hipstersmoothie

any update to fix this issue ?

mojodev avatar Nov 10 '18 10:11 mojodev

converting the image to png will make it bigger unless you use the png options. have you tried my above code @mojodev

hipstersmoothie avatar Nov 11 '18 19:11 hipstersmoothie

converting the image to png will make it bigger unless you use the png options. have you tried my above code @mojodev

how about jpeg ? when I write image to buffer , it increasing the file size 168kb > 800kb ? how can fix ?

matamune94 avatar Mar 15 '19 14:03 matamune94

I'm running into the same issue when reading in an image from Base64-encoded data and writing it back out without any manipulation. Doing so results in a file twice the size of the original.

const jimp = require('jimp')
const fs = require('fs')
const rawData = fs.readFileSync('/tmp/1.bin', 'utf-8');
const image = await jimp.read(Buffer.from(rawData, 'base64');
const newBuffer = await image.getBufferAsync(jimp.AUTO);

console.log(rawData.length); // 17196
console.log(newBuffer.getString('base64'); // 35060

grahamb avatar Oct 03 '19 04:10 grahamb

same issue - no manipulation at all but file size doubles

kane-mason avatar Sep 09 '20 19:09 kane-mason

any update to fix this issue ?

Eriickson avatar Nov 04 '20 10:11 Eriickson

Any updates for this issue?

eZubia avatar Dec 03 '20 20:12 eZubia

Any updates ?

dobrovolskaa avatar May 05 '21 11:05 dobrovolskaa

waiting for updates.. 🙂

emmaakachukwu avatar Jul 20 '21 18:07 emmaakachukwu

Any update?

arturferreira avatar Mar 07 '22 00:03 arturferreira