Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 655
egor7 Avatar asked Nov 09 '12 23:11

egor7


People also ask

Can convert images from RGB mode directly to grayscale?

I = rgb2gray( RGB ) converts the truecolor image RGB to the grayscale image I . The rgb2gray function converts RGB images to grayscale by eliminating the hue and saturation information while retaining the luminance. If you have Parallel Computing Toolbox™ installed, rgb2gray can perform this conversion on a GPU.


1 Answers

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

like image 157
egor7 Avatar answered Sep 22 '22 04:09

egor7