imagemagick:根据 r/g/b 条件有选择地填充像素?
imagmagick: selectively fill pixels based on r/g/b condition?
有没有一种方法可以根据使用像素的 R、G 或 B 值的算术表达式有选择地替换颜色?
示例:假设我有一个 RGB 图像 "foobar.png",我想将红色通道 <100 的所有像素更改为白色。
在伪代码中:
for (all pixels in image) { if (pixel.red < 100) then pixel = 0xffffff; }
有没有办法用 ImageMagick 解决这个问题?
您可以使用 FX
expressions.
假设我创建了一个测试图像...
convert -size 400x400 gradient:red-blue input.png
替换任意一个红色值<100的像素(假设max-value是255的8bit量程),可以表示为..
convert input.png -fx 'r < (100/255) ? #FFFFFF : u' output.png
更新
FX 功能强大,但速度较慢。它还会画出粗糙的边缘。另一种方法是分离红色通道,将其转换为遮罩,然后在其他通道上进行合成。这可以通过 -evaluate-sequance MAX
完成,或者设置 Alpha 通道并在白色背景上合成。
创建示例输入图像。
convert -size 400x400 xc:white \
-sparse-color shepards '0 0 red 400 0 blue 400 400 green 0 400 yellow ' \
input.png
convert -size 400x400 xc:white \
\( input.png \
\( +clone -separate -delete 1,2 \
-negate -level 39% -negate \
\) \
-compose CopyOpacity -composite \
\) -compose Atop -composite output.png
这与 emcconville 的优秀解决方案相似但略有不同。这是 Unix 语法。
#1 compute the 100 out of 255 threshold in percent
#2 read the input
#3 clone the input and make it completely white
#4 clone the input and separate the red channel, threshold and negate so that the white part represents values less than 100 out of 255
#5 use the threshold image as a mask in a composite to select between the original and the white images
#6 write the output
thresh=`convert xc: -format "%[fx:100*100/255]" info:`
convert image.png \
\( -clone 0 -fill white -colorize 100 \) \
\( -clone 0 -channel r -separate +channel -threshold $thresh% -negate \) \
-compose over -composite \
result.png
有没有一种方法可以根据使用像素的 R、G 或 B 值的算术表达式有选择地替换颜色?
示例:假设我有一个 RGB 图像 "foobar.png",我想将红色通道 <100 的所有像素更改为白色。
在伪代码中:
for (all pixels in image) { if (pixel.red < 100) then pixel = 0xffffff; }
有没有办法用 ImageMagick 解决这个问题?
您可以使用 FX
expressions.
假设我创建了一个测试图像...
convert -size 400x400 gradient:red-blue input.png
替换任意一个红色值<100的像素(假设max-value是255的8bit量程),可以表示为..
convert input.png -fx 'r < (100/255) ? #FFFFFF : u' output.png
更新
FX 功能强大,但速度较慢。它还会画出粗糙的边缘。另一种方法是分离红色通道,将其转换为遮罩,然后在其他通道上进行合成。这可以通过 -evaluate-sequance MAX
完成,或者设置 Alpha 通道并在白色背景上合成。
创建示例输入图像。
convert -size 400x400 xc:white \
-sparse-color shepards '0 0 red 400 0 blue 400 400 green 0 400 yellow ' \
input.png
convert -size 400x400 xc:white \
\( input.png \
\( +clone -separate -delete 1,2 \
-negate -level 39% -negate \
\) \
-compose CopyOpacity -composite \
\) -compose Atop -composite output.png
这与 emcconville 的优秀解决方案相似但略有不同。这是 Unix 语法。
#1 compute the 100 out of 255 threshold in percent
#2 read the input
#3 clone the input and make it completely white
#4 clone the input and separate the red channel, threshold and negate so that the white part represents values less than 100 out of 255
#5 use the threshold image as a mask in a composite to select between the original and the white images
#6 write the output
thresh=`convert xc: -format "%[fx:100*100/255]" info:`
convert image.png \
\( -clone 0 -fill white -colorize 100 \) \
\( -clone 0 -channel r -separate +channel -threshold $thresh% -negate \) \
-compose over -composite \
result.png