Filter imagmagick:根据r/g/b条件有选择地填充像素?

Filter imagmagick:根据r/g/b条件有选择地填充像素?,filter,colors,imagemagick,rgb,pixel,Filter,Colors,Imagemagick,Rgb,Pixel,有没有一种方法可以基于使用像素的R、G或B值的算术表达式选择性地替换颜色 示例:假设我有一个RGB图像foobar.png,我想更改红色通道为的所有像素,您可以使用 假设我创建一个测试图像 convert -size 400x400 gradient:red-blue input.png 将任何像素替换为红色值

有没有一种方法可以基于使用像素的R、G或B值的算术表达式选择性地替换颜色

示例:假设我有一个RGB图像foobar.png,我想更改红色通道为的所有像素,您可以使用

假设我创建一个测试图像

convert -size 400x400 gradient:red-blue input.png
将任何像素替换为红色值<100,假设最大值为8位255,可表示为

convert input.png -fx 'r < (100/255) ? #FFFFFF : u' output.png
你可以用

假设我创建一个测试图像

convert -size 400x400 gradient:red-blue input.png
将任何像素替换为红色值<100,假设最大值为8位255,可表示为

convert input.png -fx 'r < (100/255) ? #FFFFFF : u' 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

这与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