egor7
egor7

Reputation: 4950

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.

Upvotes: 85

Views: 98485

Answers (8)

egor7
egor7

Reputation: 4950

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>.

Upvotes: 140

mifth
mifth

Reputation: 292

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

Upvotes: 1

loved.by.Jesus
loved.by.Jesus

Reputation: 2534

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.

Upvotes: 2

user14003895
user14003895

Reputation: 19

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)

Upvotes: 1

fmw42
fmw42

Reputation: 53174

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

Upvotes: 17

Eduard Florinescu
Eduard Florinescu

Reputation: 17541

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 

Upvotes: 0

Paul Wenzel
Paul Wenzel

Reputation: 2016

To batch convert images in Fish shell:

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

Upvotes: 16

emcconville
emcconville

Reputation: 24439

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

Upvotes: 18

Related Questions