Convert RGB to Grayscale in ImageMagick command-line

egor7 picture egor7 · Nov 10, 2012 · Viewed 58.3k times · Source

How do I convert a RGB image (3 channels) to a grayscale one, using the (r+g+b)/3 method? I look through an examples page: http://www.imagemagick.org/Usage/color_mods/#grayscale but the desired method:

convert test.png -fx '(r+g+b)/3' gray_fx_average.png

gave me a wrong result - the resulted image has still 3 channels.

You can check this by running a command: identify -format "%[colorspace] <== %f\n" *.png.

Answer

egor7 picture egor7 · Nov 21, 2012

convert <img_in> -set colorspace Gray -separate -average <img_out> gives the best result for any image for me.