【问题标题】:Convert a greyscale image to 1-bit black/white with definable threshold, maintaining transparency?将灰度图像转换为具有可定义阈值的 1 位黑/白,保持透明度?
【发布时间】:2021-01-25 12:59:37
【问题描述】:

如何将灰度图像转换为具有可定义阈值的 1 位黑/白,但保持现有透明度/alpha?

现有问题错过了我问题的透明度部分。

另外,我需要在 macOS 的命令行上执行此操作。 ImageMagick 的convert 是一种选择,但不是唯一选择。

示例图片:

要求的行为:

  1. 低于可定义阈值的像素为黑色
  2. 高于可定义阈值的像素为白色
  3. 透明像素保持不变

我手动准备了这个“目标”图像:

我尝试过的:

  1. $ convert -threshold 50% in.png out.png

    • 所有超过阈值的东西都会变成白色
    • 低于阈值的所有内容都变得透明!
  2. $ convert -white-threshold 50% in.png out.png

    • 所有超过阈值的东西都会变成白色
    • 低于阈值的所有内容都变得透明!
  3. $ convert -black-threshold 50% in.png out.png

    • 所有超过不同阈值的东西都会变成白色
    • 什么都不会变黑!
  4. $ convert +dither -monochrome in.png out.png

    • 抖动禁用
    • 1 位转换锁定为 50%,但按预期执行
    • 但是:透明像素变黑了!
  5. $ convert -depth 1 -colors 3 -alpha set in.png out.png

    • 差不多了
    • 但是:阈值无法定义!

任何想法表示赞赏!

图片参考:http://www.studentshow.com/gallery/6097929/Pyramid-Module-Value-Grayscale

【问题讨论】:

    标签: command-line graphics imagemagick-convert


    【解决方案1】:

    这适用于我的 IM 6.9.11.34 Q16 Mac OSX Sierra。

    convert in.png -colorspace gray -channel rgb -threshold 50% +channel out.png
    

    (在上面,您指定阈值应仅应用于 rgb 通道,而不是通过 -channel rgb 的 alpha。在阈值之后,我再次打开所有通道)

    【讨论】:

    • 王牌!你能解释一下原因吗?
    • 我能理解这些论点,完美解决!谢谢。
    猜你喜欢
    • 2013-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 2012-11-23
    • 1970-01-01
    • 2011-01-25
    • 2012-06-15
    相关资源
    最近更新 更多