The fastest way to convert an image to grayscale from the command line is one ImageMagick command, and this page also covers batching, sepia, macOS sips, and reading an image's dimensions:
magick photo.jpg -colorspace Gray photo-gray.jpgThat 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. It also covers the other quick ImageMagick question that comes up alongside it: how to get an image's width and height from the terminal, with identify, sips, file, or ffprobe for video.
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:
magick -versionIf 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:
# 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:
mkdir gray
cp *.jpg gray/
magick mogrify -colorspace Gray gray/*.jpgIf 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):
# Writes gray *.png copies next to the *.jpg originals, originals untouched
magick mogrify -format png -colorspace Gray *.jpgThat 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:
magick photo.jpg -sepia-tone 80% photo-sepia.jpgAnd "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:
magick photo.jpg -colorspace Gray -threshold 50% photo-bw.pngLower 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:
sips -s format jpeg --matchTo /System/Library/ColorSync/Profiles/Generic\ Gray\ Profile.icc photo.jpg --out photo-gray.jpgsips 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.
Get image dimensions
The fastest way to read an image's width and height from the terminal is ImageMagick's identify:
magick identify -format "%w %h" photo.jpgThat prints 1920 1080 (width then height) and nothing else, which is exactly what you want when piping into a script or filling in an image's width and height attributes. Which tool to reach for:
| Tool | Install | Best for | Scriptable |
|---|---|---|---|
magick identify | ImageMagick (separate install) | Anything you script; widest format support | Yes, with -format "%w %h" |
sips | Built into macOS | A one-off check on a Mac with nothing installed | Yes, but labelled output needs parsing |
file | On nearly every Unix system | A rough glance when nothing else is around | No, the string is free-form |
ffprobe | ffmpeg (separate install) | Video resolution, not still images | Yes, with -of csv=p=0 |
ImageMagick identify
For a quick human-readable look, run identify with no format string:
magick identify photo.jpgThat gives a line like photo.jpg JPEG 1920x1080 1920x1080+0+0 8-bit sRGB 412KB. The first 1920x1080 is the dimensions; the second, with offsets, is the page geometry, and the rest is depth, colorspace and file size. For scripting, pin the output down with -format, where %w and %h are width and height in pixels:
# Just the numbers, space-separated
magick identify -format "%w %h" photo.jpg
# The classic WxH string
magick identify -format "%wx%h\n" photo.jpgOn an ImageMagick 6 box, or in a script you inherited, the bare identify -format "%w %h" photo.jpg does the same thing. ImageMagick reads dimensions out of the header without decoding the whole image, so it is fast even on large files, and it reads the canvas size of a PSD, a TIFF, a WebP and so on. (PDF page sizes work too, but only when ImageMagick has its Ghostscript delegate installed.)
macOS: sips dimensions
sips reads dimensions with zero setup on any Mac:
sips -g pixelWidth -g pixelHeight photo.pngIt prints the path followed by labelled lines:
/Users/you/photo.png
pixelWidth: 1920
pixelHeight: 1080-g queries a single property; pass it twice for both. sips -g all photo.png dumps everything it knows (DPI, colorspace, format, profile). The labelled output is friendlier to read but more work to parse, so if ImageMagick is already installed, identify -format is cleaner for scripts.
A quick guess with file
file is on virtually every Unix system and includes dimensions in its type description:
file photo.pngYou get something like photo.png: PNG image data, 1920 x 1080, 8-bit/color RGB, non-interlaced. This is a convenience, not a contract: the wording varies by format and by file version, and some formats report no dimensions at all. Do not parse it.
Video resolution with ffprobe
An image tool will not read a video's resolution; use ffprobe, which ships with ffmpeg:
ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=p=0 clip.mp4That prints 1920,1080. -v error silences the banner, -select_streams v:0 targets the first video stream (not the audio stream's blank dimensions), -show_entries stream=width,height asks for just those fields, and -of csv=p=0 prints bare comma-separated values. Swap in -of default=noprint_wrappers=1 if you would rather see width=1920 / height=1080 labelled.
Dimensions for a whole folder
Loop and let identify do the work per file:
for f in *.jpg *.png; do
echo "$f: $(magick identify -format "%wx%h" "$f")"
doneQuote "$f" so filenames with spaces survive. If a glob matches nothing, bash passes the literal pattern through and identify errors on it; shopt -s nullglob before the loop makes an empty glob expand to nothing. On macOS without ImageMagick, sips already accepts multiple files in one call:
sips -g pixelWidth -g pixelHeight *.pngFAQ
magick photo.jpg -colorspace Gray photo-gray.jpg. On ImageMagick 7 the entry point is magick; the bare convert from older tutorials is the deprecated ImageMagick 6 form.
Gray computes a perceptual (nonlinear, gamma-encoded) grayscale using the Rec.709 weighting (0.212656R + 0.715158G + 0.072186B), which is what your eye expects and what you want for a photo. LinearGray computes the gray value in linear light and uses the Rec.601 weighting (0.298839R + 0.586811G + 0.114350B); it is more correct for compositing math but usually comes out darker and flatter on a normal photo.
Use magick mogrify -colorspace Gray *.jpg, but be careful: mogrify overwrites the originals in place. Copy the files into a new directory first, or use -format png so it writes new files and leaves the originals untouched.
Yes. Unless you change the suffix with -format, mogrify writes the result back over the source file. Always copy your originals to a working directory before running a batch grayscale, or pass -format to produce new files.
Use the built-in sips tool: match the image to the system Generic Gray ICC profile with --matchTo and write to a new file with --out. It is zero-install on macOS, though more limited than ImageMagick.
With ImageMagick installed, magick identify -format "%w %h" photo.jpg prints just the width and height. On a Mac with nothing installed, sips -g pixelWidth -g pixelHeight photo.jpg does the same job natively, and on any Unix system file image.png usually includes the dimensions in a free-form description.
The first (e.g. 1920x1080) is the image size; the second with the offsets (1920x1080+0+0) is the page geometry. They match for a normal flat image. Use -format "%wx%h" when you want only the size.
Use ffprobe: ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=p=0 clip.mp4 prints the video stream's width and height as 1920,1080. ImageMagick cannot read video resolution.
See also
- Convert a video to black and white (grayscale) with ffmpeg: the same effect for video, where
hue=s=0desaturates andformat=grayencodes true single-channel gray. - Inspect an animated GIF from the command line: frame count, first frame, and animated-or-not with the same
identifytool. - The ffmpeg command cheat sheet: the wider reference for converting, cropping, trimming, and compressing media from the CLI.
Sources
Authoritative references this article was fact-checked against.
- ImageMagick: color modifications (official usage docs)imagemagick.org
- ImageMagick: command-line options (official docs)imagemagick.org
- ImageMagick: porting to version 7 (official docs)imagemagick.org
- ImageMagick: mogrify command-line tool (official docs)imagemagick.org
- ImageMagick identify (official documentation)imagemagick.org
- ImageMagick format and print image properties (%w, %h escapes)imagemagick.org
- sips command reference for macOSss64.com
- ffprobe documentation (official)ffmpeg.org



![Convert SVG to PNG (and PSD to PNG) From the Command Line Convert SVG to PNG from the command line with rsvg-convert, Inkscape, or ImageMagick (size, density, transparency), plus PSD to PNG with the [0] composite.](https://images.techearl.com/convert-svg-to-png-command-line/convert-svg-to-png-command-line-1536.webp?v=2026-11-25T10%3A00%3A00-05%3A00)

