How to Convert an Image to Grayscale via the Command Line

How can I convert an image to grayscale via the command line?

If you have imagemagick installed,

convert source.jpg -colorspace Gray destination.jpg (true grayscale only)
convert source.jpg -monochrome destination.jpg (true black and white)
convert source.jpg -separate destination.jpg (separate into gray channels)

If you don't care about losing the original file: mogrify -colorspace Gray file.

Convert RGB to Grayscale in ImageMagick command-line

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

Convert entire folder to grayscale using image magick?

No need for Notepad. Start a Command Prompt and change directory to the one containing your images. So if your images are in HOME/Images, type:

cd Images

then press Enter/Return. Then type:

magick mogrify -colorspace gray *.png *.jpg

then press Enter/Return and you're finished.

Convert a greyscale image to 1-bit black/white with definable threshold, maintaining transparency?

This works for me on IM 6.9.11.34 Q16 Mac OSX Sierra.

Sample Image

convert in.png -colorspace gray -channel rgb -threshold 50% +channel out.png

(In the above, you specify that the threshold should be applied only to the rgb channels and not the alpha via -channel rgb. After the threshold, I turn on all the channels again)

Sample Image

How to convert a PDF to grayscale from command line avoiding to be rasterized?


gs \
-sDEVICE=pdfwrite \
-sProcessColorModel=DeviceGray \
-sColorConversionStrategy=Gray \
-dOverrideICC \
-o out.pdf \
-f page-27.pdf

This command converts your file to grayscale (GS 9.10).

Convert entire folder to greyscale using image magick?

If you have lots of files to process, use mogrify:

magick mogrify -colorspace gray *.jpg

If you have tens of thousands of images and a multi-core CPU, you can get them all done in parallel with GNU Parallel:

parallel -X magick mogrify -colorspace gray ::: *.jpg

Convert RGB to Grayscale in ImageMagick (but not the entire image)

You can select the plum colour and change that to gray20 and then select the lime colour and change it to gray80 like this:

magick chart.png -fuzz 10%               \
-fill gray20 -opaque "rgb(68,1,84)" \
-fill gray80 -opaque "rgb(122,209,81)" result.png

Sample Image

Or, as a one-liner:

    magick chart.png -fuzz 10% -fill gray20 -opaque "rgb(68,1,84)" -fill gray80 -opaque "rgb(122,209,81)"  result.png

How can I convert an RGB image into grayscale in Python?

How about doing it with Pillow:

from PIL import Image
img = Image.open('image.png').convert('L')
img.save('greyscale.png')

If an alpha (transparency) channel is present in the input image and should be preserved, use mode LA:

img = Image.open('image.png').convert('LA')

Using matplotlib and the formula

Y' = 0.2989 R + 0.5870 G + 0.1140 B 

you could do:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

def rgb2gray(rgb):
return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140])

img = mpimg.imread('image.png')
gray = rgb2gray(img)
plt.imshow(gray, cmap=plt.get_cmap('gray'), vmin=0, vmax=1)
plt.show()

Convert JPG image from grayscale to RGB using imagemagick

The PNG colour types do not apply to JPEG images, so you can't use the same technique. Try forcing the colorspace to sRGB, and/or setting the type to TrueColour so you don't get a palettised image:

convert input.jpg -colorspace sRGB -type truecolor result.jpg


Related Topics



Leave a reply



Submit