imagemagickgrayscale

Convert RGB to Grayscale in ImageMagick command-line


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.


Solution

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

    Update: As of 2025, the equivalent command in modern versions of ImageMagick is magick <img_in> -colorspace Gray -separate -evaluate-sequence Mean <img_out>. However, most of the flags can be omitted since they're the default behavior already, so the 2025 command can be simplified to just magick <img_in> -colorspace Gray <img_out>.