使用 100% 阈值可靠地将图像水平压缩到 1 列像素

Reliably Compressing the image horizontally to 1 column of pixels with 100% thresholding

我正在寻找一种可靠地将图像水平压缩到一列像素的方法,使得:

下面是原始输入图像:

我能做的最好的就是使用这段代码:

magick convert input.png -alpha off -threshold 99% -scale 1x75! -threshold 99% output.png

但是输出不完美,因为像素列中存在间隙:

...我想要的输出是:

避免使用 magick convert ...,因为它会给您带来旧的 v6 行为,而现在我们有了 v7,这通常不是一个好主意。我建议您使用 magick 而不是 magick convert.

所以,总而言之,只需使用:

  • magick INPUT operations OUTPUT - 代替 v6 convert INPUT operations OUTPUT
  • magick identify ...
  • magick compare ...
  • magick compose ...

关于你的问题,在初始阈值之后:

magick logo.png -alpha off -threshold 99% result.png

你还剩下这个:

问题是在 1278 像素的一行中只有 2 个黑色像素并不多,它们在噪声中消失了。我的建议是 运行 200x1 像素区域的统计最小值。这意味着任何黑色像素左右 200 像素内的任何像素都变成黑色,如下所示:

magick logo.png -alpha off -threshold 99% -statistic minimum 200x1 result.png

现在有很多黑色在最终图像中充分显着地显示出来:

magick logo.png -alpha off -threshold 99% -statistic minimum 200x1 -scale 1x\! -threshold 99% result.png


而不是硬编码的 200,你可以让它成为图像宽度的 1/8 以适应图像的宽度并提供合理的性能(使区域变宽会减慢向下处理):

magick logo.png -alpha off -threshold 99% -statistic minimum "%[fx:int(w/8)]x1"  result.png