gpt4 book ai didi

node.js - 如何替换图像Node.JS中除黑色以外的所有颜色

转载 作者:行者123 更新时间:2023-12-05 06:12:12 30 4
gpt4 key购买 nike

我正在尝试创建图像的轮廓。我已经将背景设为黑色,但无法将所有其他颜色转换为灰色。

到目前为止我的代码:

import sharp from "sharp";
import jimp from "jimp";

sharp("input.png")
.flatten()
.toFile("output.jpg")
.then(async (data) => {
const image = await jimp.read("output.jpg");

image.scan(0, 0, image.bitmap.width, image.bitmap.height, (x, y, idx) => {
if (image.bitmap.data[idx] >= 2) {
image.bitmap.data[idx] = 86;
image.bitmap.data[idx + 1] = 101;
image.bitmap.data[idx + 2] = 115;
image.bitmap.data[idx + 3] = image.bitmap.data[idx + 3];
}
});

image.write("output.jpg");
});

此代码输出:result

输入图像是:enter image description here

我看过像 replace-color 这样的包,但它们似乎旨在替换一种颜色,而不是全部,而只是一种颜色。非常感谢任何帮助

最佳答案

问题是我正在将格式从 png 更改为 jpg。将其保留为 png 可以解决问题

关于node.js - 如何替换图像Node.JS中除黑色以外的所有颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63693201/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com