Seeking a way to convert colors in a png image using GraphicsMagick, my current hardcoded code is:
await gm("input.png")
.fill("green")
.opaque("blue")
.fill("red")
.opaque("yellow")
.write("output.png", function (err) {
if (err) console.log(err);
});
While this works fine, I wish to make it more flexible by eliminating hardcoding. How can I achieve this using a buffer?
I attempted the following approach but it doesn't work as intended:
let colors: { op: string; fi: string }[] = [
{ op: "blue", fi: "red" },
{ op: "yellow", fi: "green" },
];
let imageFileBuffer = await fs.readFileSync("input.png");
colors.forEach(async (element) => {
console.log(element);
await gm(imageFileBuffer).fill(element.fi).opaque(element.op);
});
await gm(imageFileBuffer).write("output.png", function (
err
) {
if (err) console.log(err);
});
Can anyone point out what might be wrong with this approach?