R
R
Roman Andreevich2018-12-18 07:00:47
JavaScript
Roman Andreevich, 2018-12-18 07:00:47

How to change png color in node js?

Colleagues, good day. Actually who faced.
I make a request to another server and save a png image to my server. It is simple, a transparent background and a figure of the same color.
After saving, I give it to the client. And there are many such pictures. Is it possible to change the color of this picture when saving????
I have an implementation on canvas, but I need it on nodejs.
Image example:
5c1870eb0ca6c415076037.png
One color and transparent background!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GilbertAmethyst, 2018-12-18
@GilbertAmethyst

This will probably help you: https://github.com/oliver-moran/jimp/tree/master/p...
You need a lib first: https://github.com/oliver-moran/jimp
And there is a ready-made replacement solution colors using this plugin, here:
https://www.npmjs.com/package/replace-color

A
AngReload, 2018-12-18
@AngReload

'use strict';

const request = require('request');
const fs = require('fs');
const PNG = require('pngjs').PNG;

const MY_COLOR = [0, 128, 255];

request('https://hsto.org/webt/5c/18/70/5c1870eb0ca6c415076037.png')
  .pipe(new PNG({
    filterType: 4
  }))
  .on('parsed', function () {
    for (var y = 0; y < this.height; y++) {
      for (var x = 0; x < this.width; x++) {
        var idx = (this.width * y + x) << 2;
        this.data[idx + 0] = MY_COLOR[0];
        this.data[idx + 1] = MY_COLOR[1];
        this.data[idx + 2] = MY_COLOR[2];
        this.data[idx + 3] = this.data[idx + 3];
      }
    }

    this.pack().pipe(fs.createWriteStream('result.png'));
  });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question