Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How convert jimp object to image buffer in node?

So, I use some lib to join countable images in specific way to one single image.

This lib use Jimp library to done it and after all joins return a Jimp object. Like this:

Jimp {
  _background: 0,
  bitmap: {
    data: <Buffer 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ... 2359246 more bytes>,
    width: 768,
    height: 768
  }
}

After this joining I need convert this Jimp object to node Buffer object without writing image as file to disc and return Buffer to use it in next steps.

Is it possible? I cant find anything in Jimp DOCs. And when I try write Jimp.bitmap.data to file, this image is corrupted...

Thanks!

like image 518
Roman Shmandrovskyi Avatar asked Mar 16 '20 16:03

Roman Shmandrovskyi


People also ask

What is JIMP image?

JavaScript Image Manipulation Program (Jimp) allows you to easily manipulate and transform your images into any required format, style, or dimension. It also optimizes images for minimal file size, ensures high visual quality for an improved user experience, and reduces bandwidth.

What is JIMP Nodejs?

Jimp is a node module used to do image processing which is provided by the npm installer. The Jimp – Javascript Image Manipulation Program is a library written entirely in JavaScript for Node, without any external or native dependencies.

How do I manipulate images in Jimp using the API?

Jimp offers both callback- and Promise-based APIs for manipulating images. For the purpose of this post, we’ll use Jimp’s Promise API. The static Jimp.read ​ method accepts an image as an input. The input could be the location of an image file in the file system, a web address (URL), dimension (width and height), Jimp instance, or buffer.

How do I read an image from a Jimp file?

The static Jimp.read ​ method accepts an image as an input. The input could be the location of an image file in the file system, a web address (URL), dimension (width and height), Jimp instance, or buffer. Then, it returns a Promise.

How to reduce image quality on the fly with Jimp?

I'll be using Jimp to show you how to reduce image quality on the fly by reducing the size of the Buffer data we receive and convert it into an actual image. You can simply reduce the quality of your image by 50% with just a few lines of code. I am going to use a base64 string as my data source.

What do you use Jimp for?

I would love to give some extra credits to the creators of Jimp because it solves nearly everything you would want to do with an actual image in nodejs and has zero dependency. I've had zero issues using it in an electron application. Understand how images work in nodejs on different stages. Convert from Buffer to base64.


Video Answer


1 Answers

So, I found solution. There is a method in Jimp lib for this. But no one row from DOCs not describe this. Working solution:

const Jimp = require('jimp');

const img = Jimp.read('img.png');

img.getBuffer(Jimp.MIME_PNG, (err, buffer) => {
  console.log(buffer);
});

And console output:

<Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 03 00 ... 211452 more bytes>

That's works pretty good for me.

like image 114
Roman Shmandrovskyi Avatar answered Oct 24 '22 03:10

Roman Shmandrovskyi