
JavaScript native image processing
Good day, reader!
Somehow I needed image processing in a project running on node.js. And so I downloaded the file, put it in a folder and connected it as a module. But no, there were no such people in nature. That's why I had to use node-imagemagick . But now the post is not about this library.
A post about what I wanted to make such a library that I downloaded, uploaded a file to the project, connected the module and everything works! Well done. True support for gif 'ok is not implemented, but I hope for a huge community that will be interested and help to complete the library.
He called it simple, imageLib.js , and posted it on github , though under MIT.
I did not measure the speed, and there was no reason to, it’s clear that it works slower than all existing ones. But for the sake of the idea, it was worth it.
And here is an example of reading a PNG image and then inserting a canvas 200x200 pixels in size with a black frame and a rectangle in 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');
});
});
Using the library, you can read and write JPEG, PNG images, and resize them. You can also find out the color and alpha channel of the pixel, or overwrite it. Well and accordingly copying one picture to another.
A full list of functions and their examples can be found on the module page: