📜 ⬆️ ⬇️

JavaScript image processing by native functions

Good day, reader!


Somehow I needed in the project running on node.js, image processing. And to download the file, put it in a folder and connect it as a module. But no, there were none in nature. Therefore, I had to use node-imagemagick . But now the post is not about this library.

The post that I wanted to make such a library that I downloaded, put the file into the project, connected the module and everything works! Well done. True gif 'ok support is not implemented, but I hope for a huge community that will be interested and will help to complete the library.

I called it simple, imageLib.js , and put it on github , though under MIT.

I didn’t make speed measurements, and there’s no need, it’s clear that it works slower than all existing ones. But for the idea it was worth it.
')
Here is an example of reading a PNG image and then inserting a 200x200 pixel canvas with a black frame and a rectangle into this image:

var imageLib = require('./imageLib.js'); imageLib('./images/trees.png').pngToData(function() { var that = this; imageLib(200, 200).create(function() { var x, y; for(x=20; x < 180; x++) { for(y=20; y < 40; y++) { this.setPixel(x, y, 255, 0, 0, 255); // black } } for(y=0; y < this.height; y++) { this.setPixel(0, y, 255, 0, 0, 255); this.setPixel(this.width-1, y, 255, 0, 0, 255); } for(x=0; x < this.width; x++) { this.setPixel(x, 0, 255, 0, 0, 255); this.setPixel(x, this.height-1, 255, 0, 0, 255); } this.pasteTo(that, 10, 10, true); that.toPng('./images/trees_samp.png'); }); }); 


With the help of the library, you can read and write JPEG, PNG images, change their size. You can also find out the color and alpha channel of a pixel, or overwrite it. Well, respectively, copying one picture to another.

A complete list of functions and their examples can be found on the module page:

github.com GitHub.com Link: github.com/lampaa/imageLib

nodejs Link to node.js: node.js

Source: https://habr.com/ru/post/187452/


All Articles