74

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.

8 Answers 8

129

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

5
  • 4
    Thanks egor7 :) egor7 command works good even with imagemagick 6.5.7 that has a problem of returning very dark images when using just convert <infile> -colorspace Gray <outfile>. I'm using rails+paperclip on heroku (imagemagick 6.5.8) and was getting very dark images with lots of diferent variants of convert command. This one worked great ;) Thanks again.
    – Rui Castro
    Oct 19, 2013 at 23:21
  • 12
    At least in newer versions of ImageMagick, it seems ... -set colorspace ... should be -colorspace. Feb 7, 2016 at 22:34
  • 1
    I needed to pump the brightness even with this. convert -brightness-contrast 5x0 <infile> -set colorspace Gray -separate -average <outfile> gave me virtually the same brightness. Sep 24, 2018 at 20:38
  • 13
    I'd recommend against this solution, even though it's the most voted one. I have implemented it in my code and it caused multiple weird results to me, the latest time a document which originally contained only black and magenta text on white background was made a "2-bit grayscale" image (?) with the magenta being transformed to white - hence illigible. The best solution I found was simply convert source.jpg -colorspace Gray destination.jpg (see here: stackoverflow.com/questions/7708368/…) both in terms of result and time. Feb 19, 2019 at 17:09
  • I get this shitty error: convert: profile 'icc': 'RGB ': RGB color space not permitted on grayscale PNG pos-gray.png' @ warning/png.c/MagickPNGWarningHandler/1750.` Jun 21, 2022 at 19:44
18

Using the (r+g+b)/3 method will apply the effects of grayscale, but the image will remain in sRGB (which is the expected behavior for this method). You'll need to specify the desired colorspace along with the -fx command.

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

Verify with identify -format "%[colorspace] <== %f\n" gray_fx_average.png

Gray <== gray_fx_average.png
16

To batch convert images in Fish shell:

for file in *.jpg; convert -colorspace Gray $file $file; end;

4
  • 10
    mogrify is a more compact command for converting all files of one type
    – jsta
    Jul 19, 2018 at 18:48
  • 8
    Nothing specific to Fish shell here. Feb 18, 2019 at 22:47
  • 7
    or in Bash shell: for file in *.jpg;do convert -colorspace Gray $file $file; done
    – Kyeno
    Sep 18, 2019 at 23:46
  • Using parallel significantly reduces conversion time and improves readability: parallel convert -colorspace Gray {} {} ::: *.jpg
    – Sergey
    Feb 9, 2021 at 13:00
12

A few ways to that in Imagemagick command line are:

convert test.png -grayscale average gray_average.png

or

convert test.png -colorspace OHTA -channel r -separate +channel gray_average.png

or

convert test.png -intensity average -colorspace gray gray_average.png

or

convert test.png -colorspace HSI -channel blue -separate +channel gray_average.png


See

https://imagemagick.org/script/command-line-options.php#grayscale https://imagemagick.org/script/command-line-options.php#intensity https://imagemagick.org/script/command-line-options.php#colorspace

1

Seems like you are taking the red channel to do that, on convert test.png -colorspace OHTA -channel r -separate +channel gray_average.png i prefer the green channel (i heard that way works on tv sice ancient days, maybe the best)

1
  • I tried the green and blue channel, i.e., -channel G and -channel B, and got nothing but a grey picture. For me only -channel R worked. Aug 20, 2020 at 8:22
1

I use convert mostly to convert colour pictures of documents into grey-scale pdf documents in order to perform OCR. My best results are using Rec709Luminance. So I recommend

convert colourpicture.png -grayscale Rec709Luminance greyscalepicture.png

Short command, nice outputs.

5
  • Produces too dark grays. May 13, 2021 at 0:39
  • @SzieberthAdam I guess this is a matter of taste. ;) May 13, 2021 at 17:44
  • Maybe. But compare the result of this to other grayscale conversions.: github.com/SzieberthAdam/cvd-palette/blob/master/haldclut/gray/… You can compare the hald cluts as well in the parent directory. Usually the aim is to get grays of same luminance or perceived lightness. See how gimp and photoshop (psN) performs in this sense. Best are the ones with most lines of hard-to-read texts. May 22, 2021 at 18:22
  • @SzieberthAdam Thanks for the detailed explanation and, especially, for the test picture. It's a pleasure to discuss based on facts! I still have to say that if the "aim is to get grays of same luminance or perceived lightness", then I fully agree with you: the method I provide in my answer performs poorly. However, the question did not specified anything about the aim of the grayscale conversion—you just argued one possible aim—and, consequently, for certain tasks my answer can be even optimal (!). Just as an example, for grayscaling text documents or books (as I already mention). May 31, 2021 at 13:00
  • 1
    Agreed. For different aims, different conversion works best. You mentioned you usually do grayscale conversion for OCR and I trust your judgement in conversion for that target. I just wanted to show that for other uses like photo editing etc. it might be worth to do the conversion with GIMP 2.10 or photoshop (not the newest version) or in command line, using imagemagick and one of the hald cluts. May 31, 2021 at 14:26
0

I use this with good result for gray-scale images (I convert from PNG):

ls ./*.png | xargs -L1 -I {} convert {} -strip -interlace JPEG -sampling-factor 4:2:0 -gaussian-blur 0.05 -colorspace Gray -quality 20  {}.jpg

I use this for scanned B&W pages get them to gray-scale images (the extra arguments cleans shadows from previous pages):

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 
0

I had an issue to convert an sRGB colorspace to a Gray colorspace. I had to delete Alpha channel manually before a conversion. In other case, the image will stay sRGB.

convert image_original.tga -alpha off -set colorspace Gray image_converted.tga

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.