TechEarl

How to Convert an Image to Grayscale From the Command Line (ImageMagick)

Convert an image to grayscale from the command line with ImageMagick: the one-line magick command, the difference between perceptual Gray and linear gray, batching a folder with mogrify, sepia, and a built-in macOS alternative.

Ishan Karunaratne⏱️ 7 min readUpdated
Share thisCopied
Convert an image to grayscale from the command line with ImageMagick, batch a whole folder with mogrify, add a sepia tone, and use sips on macOS without installing anything.

The fastest way to convert an image to grayscale from the command line is one ImageMagick command:

bash
magick photo.jpg -colorspace Gray photo-gray.jpg

That reads photo.jpg, drops the color, and writes photo-gray.jpg. It works on JPEG, PNG, TIFF, WebP, and anything else ImageMagick can decode, and the output format follows the extension you give the second file. That is the whole job for most people. The rest of this page is the detail behind it: why Gray and not GRAY, how to batch a folder safely, sepia, and the built-in macOS option when you do not want to install anything.

Use magick, not convert

If you have seen older tutorials run convert photo.jpg -colorspace Gray out.jpg, that is ImageMagick 6 syntax. On ImageMagick 7 (the current line) the single entry point is magick, and the bare convert command is deprecated: it still runs as a compatibility shim but prints a deprecation warning, and it can collide with the Windows convert.exe (the disk-format utility) on PATH. Everywhere below uses magick. The other legacy tools are now subcommands: magick identify, magick mogrify, magick montage. Check your version with:

bash
magick -version

If magick is not found you are on IM6 or it is not installed: brew install imagemagick on macOS, sudo apt install imagemagick on Debian/Ubuntu, winget install ImageMagick.ImageMagick on Windows.

Perceptual gray vs linear gray

-colorspace Gray is the one you almost always want. It produces a perceptual (nonlinear, gamma-encoded) grayscale, the kind your eye expects, where a mid green looks brighter than a mid blue. The official command-line docs give its formula as Gray = 0.212656*R+0.715158*G+0.072186*B, the Rec.709 weighting applied to the gamma-encoded channels. That is exactly the same math as the -grayscale Rec709Luma operator (the primed R'G'B', nonlinear form), so the two are interchangeable.

There is a second, subtly different result you may run into:

bash
# Linear-light grayscale: weights computed in linear RGB, not gamma-encoded
magick photo.jpg -colorspace LinearGray photo-gray.jpg

-colorspace LinearGray computes the gray value in linear light before re-encoding. Note that it does not use the same coefficients as Gray: the docs give it as LinearGray = 0.298839*R+0.586811*G+0.114350*B, which are the Rec.601 weights, so the closest -grayscale operator equivalent is Rec601Luminance (linear), not Rec709. Computing in linear light is more physically correct for compositing math, but on a normal photo it usually comes out noticeably darker and flatter than -colorspace Gray, which surprises people. Casing is not a switch: -colorspace gray and -colorspace Gray are the same colorspace, and the meaningful distinction is the word, not the capitals: Gray (Rec.709, nonlinear) vs LinearGray (Rec.601, linear), and on the -grayscale operator Luma (nonlinear) vs Luminance (linear). For a photo you want to look at, stick with -colorspace Gray.

Batch a whole folder with mogrify

To grayscale every JPEG in a directory at once, use magick mogrify. The catch is the entire reason to read this section: mogrify overwrites the files in place. There is no separate output file. Point it at your originals and the originals are gone.

So copy first, then convert the copies:

bash
mkdir gray
cp *.jpg gray/
magick mogrify -colorspace Gray gray/*.jpg

If you want grayscale copies in a different format and to keep the originals untouched, -format writes new files instead of overwriting (it changes the suffix, so the source stays put):

bash
# Writes gray *.png copies next to the *.jpg originals, originals untouched
magick mogrify -format png -colorspace Gray *.jpg

That is the safe batch pattern: -format when you want the originals kept, a copied directory when you genuinely want grayscale files in the same format.

Sepia and true black-and-white

Grayscale is not the only "make it old" effect. For a warm sepia tone, -sepia-tone takes a threshold as a percentage (the docs allow up to 99.9%); around 80% is a typical look:

bash
magick photo.jpg -sepia-tone 80% photo-sepia.jpg

And "grayscale" is not the same as true 1-bit black and white. Grayscale keeps the full range of grays; a hard black-and-white image has only two values. Threshold the grayscale to get that:

bash
magick photo.jpg -colorspace Gray -threshold 50% photo-bw.png

Lower the percentage to keep more black, raise it to keep more white. That is the right tool for line art or a scanned document, not for a photo you want to keep tonal detail in.

macOS: grayscale with sips, no install

Every Mac ships with sips (Scriptable Image Processing System), so if ImageMagick is not installed and you only need a quick grayscale, you do not have to install anything:

bash
sips -s format jpeg --matchTo /System/Library/ColorSync/Profiles/Generic\ Gray\ Profile.icc photo.jpg --out photo-gray.jpg

sips is more limited than ImageMagick (it converts grayscale by matching to the system Generic Gray ICC profile rather than offering a choice of luminance methods), but it is genuinely zero-install on macOS and fine for one-off jobs. For anything batch or precise, ImageMagick is the better tool.

See also

Sources

Authoritative references this article was fact-checked against.

TagsImageMagickgrayscalemagickmogrifysipsimage conversionCLIsepia

Found this useful? Pass it on.

Copied

Ishan Karunaratne

Tech Architect · Software Engineer · AI/DevOps

Tech architect and software engineer with 20+ years building software, Linux systems, and DevOps infrastructure, and lately working AI into the stack. Currently Chief Technology Officer at a healthcare tech startup, which is where most of these field notes come from.

Keep reading

Related posts

How to Convert a PSD to PNG From the Command Line

Convert a PSD to PNG from the command line with ImageMagick. The one trick that matters: design.psd[0] selects the flattened composite, so you get one PNG instead of a folder full of separate layers.