question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bitmap manipulation increasing the file size considerably.

See original GitHub issue

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?

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:14

github_iconTop GitHub Comments

3reactions
grahambcommented, Oct 3, 2019

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
3reactions
matamune94commented, Mar 15, 2019

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 ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bitmaps and File Size | Understand How Color Works in ...
Increasing bit depth increases file size by a simple multiplier. Therefore, a 24-bit image is three times as large as an 8-bit image,...
Read more >
Bad image quality after resizing/scaling bitmap - Stack Overflow
Use createScaledBitmap will make your image looks very bad. I've met this problem and I've resolved it. Below code will fix the problem:...
Read more >
Reducing Image File Sizes - Technology Services
Saving these file types as a jpeg (. jpg) can reduce file size by 90% - making them much more easily sent through...
Read more >
A Beginners Guide to Bitmaps - Paul Bourke
A common operation that reduces the size of large 24 bit bitmaps is to convert them to indexed colour with an optimised palette,...
Read more >
Vector vs Bitmap - teelaunch blog
texture. Texture in vector artwork is often times hard to achieve and has the potential to increase the file size tremendously. I have...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found