将灰度图像转换为具有可定义阈值的 1 位 black/white,保持透明度?

Convert a greyscale image to 1-bit black/white with definable threshold, maintaining transparency?

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

Existing questions miss out the transparency part of my question.

此外,我需要在 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

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

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

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