【问题标题】:How to replace all colours except black in image Node.JS如何替换图像Node.JS中除黑色以外的所有颜色
【发布时间】:2020-12-20 21:28:53
【问题描述】:

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

到目前为止我的代码:

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");
  });

此代码输出:

输入图片为:

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

【问题讨论】:

  • 你有什么具体的错误吗?
  • 你为什么同时使用sharpjimp
  • 我无法查看您帖子中的链接,请直接将其嵌入问题中
  • @paroxyzm 没有任何错误,我只是不知道如何在这里实现我的目标
  • @Klaycon 完成。还添加了起始图像

标签: node.js typescript image image-processing silhouette


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-20
    • 2011-07-12
    • 1970-01-01
    • 2019-11-16
    相关资源
    最近更新 更多