如何使用(r + g + b)/ 3方法将RGB图像(3通道)转换为灰度图像?我翻阅示例页面:http://www.imagemagick.org/Usage/color_mods/#grayscale但所需的方法:
convert test.png -fx '(r+g+b)/3' gray_fx_average.png
给我一个错误的结果-结果图像仍然具有3个通道。
您可以通过运行命令:identify -format "%[colorspace] <== %f\n" *.png
进行检查。
convert <img_in> -set colorspace Gray -separate -average <img_out>
为我的任何图像提供最佳效果。
使用(r+g+b)/3
方法将应用灰度效果,但是图像将保留在sRGB中(这是此方法的预期行为)。您需要指定所需的colorspace以及-fx
命令。
convert test.png -fx '(r+g+b)/3' -colorspace Gray gray_fx_average.png
使用identify -format "%[colorspace] <== %f\n" gray_fx_average.png
验证
Gray <== gray_fx_average.png
要在Fish shell中批量转换图像:
for file in *.jpg; convert -colorspace Gray $file $file; end;
我将其用于灰度图像(从PNG转换为:):
ls ./*.png | xargs -L1 -I {} convert {} -strip -interlace JPEG -sampling-factor 4:2:0 -gaussian-blur 0.05 -colorspace Gray -quality 20 {}.jpg
我将其用于扫描的黑白页面,使它们成为灰度图像(多余的参数清除了前一页的阴影):
ls ./*.png | xargs -L1 -I {} convert {} -strip -interlace JPEG -sampling-factor 4:2:0 -gaussian-blur 0.05 -colorspace Gray -quality 20 -density 300 -fill white -fuzz 40% +opaque "#000000" -density 300 {}.jpg