63

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.

gerrit
  • 17,590
  • 12
  • 72
  • 135
egor7
  • 4,097
  • 5
  • 28
  • 52

7 Answers7

111

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

egor7
  • 4,097
  • 5
  • 28
  • 52
  • 2
    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 -colorspace Gray `. 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 '13 at 23:21
  • 11
    At least in newer versions of ImageMagick, it seems `... -set colorspace ...` should be `-colorspace`. – Kenny Evitt Feb 07 '16 at 22:34
  • 1
    I needed to pump the brightness even with this. `convert -brightness-contrast 5x0 -set colorspace Gray -separate -average ` gave me virtually the same brightness. – Jabberwock Sep 24 '18 at 20:38
  • 9
    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: https://stackoverflow.com/questions/7708368/how-can-i-convert-an-image-to-grayscale-via-the-command-line) both in terms of result and time. – Marco Spinaci Feb 19 '19 at 17:09
16

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
emcconville
  • 20,969
  • 4
  • 43
  • 60
12

To batch convert images in Fish shell:

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

Paul Wenzel
  • 1,537
  • 1
  • 13
  • 14
8

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

fmw42
  • 28,780
  • 5
  • 37
  • 49
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)

  • 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. – loved.by.Jesus Aug 20 '20 at 08:22
0

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.

loved.by.Jesus
  • 1,365
  • 20
  • 26
  • Produces too dark grays. – SzieberthAdam May 13 '21 at 00:39
  • @SzieberthAdam I guess this is a matter of taste. ;) – loved.by.Jesus May 13 '21 at 17:44
  • Maybe. But compare the result of this to other grayscale conversions.: https://github.com/SzieberthAdam/cvd-palette/blob/master/haldclut/gray/compare.png 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. – SzieberthAdam May 22 '21 at 18:22
-1

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 
Eduard Florinescu
  • 13,721
  • 26
  • 101
  • 164