TechEarl

How to Get Image Dimensions From the Command Line

Get an image's width and height from the terminal: ImageMagick's identify, the built-in sips on macOS (no install), file for a quick guess, ffprobe for video, plus a batch loop over a whole folder.

Ishan Karunaratne⏱️ 7 min readUpdated
Share thisCopied
Get the width and height of an image from the command line with ImageMagick identify, macOS sips, file, and ffprobe for video, including a batch loop over a folder.

The fastest way to read an image's width and height from the terminal is ImageMagick's identify:

bash
magick identify -format "%w %h" photo.jpg

That prints 1920 1080 (width then height) and nothing else, which is exactly what you want when you are piping it into a script or pasting it into a <PostImage> width/height. The rest of this page covers the variants: a print-style format string, the built-in macOS option that needs nothing installed, a rough one-liner with file, the video equivalent with ffprobe, and a loop for a whole folder.

Which tool to reach for:

ToolInstallBest forScriptable
magick identifyImageMagick (separate install)Anything you script; widest format supportYes, with -format "%w %h"
sipsBuilt into macOSA one-off check on a Mac with nothing installedYes, but labelled output needs parsing
fileOn nearly every Unix systemA rough glance when nothing else is aroundNo, the string is free-form
ffprobeffmpeg (separate install)Video resolution, not still imagesYes, with -of csv=p=0

ImageMagick: identify

If you only need a quick human-readable look, run identify with no format string at all:

bash
magick identify photo.jpg

That gives you a line like photo.jpg JPEG 1920x1080 1920x1080+0+0 8-bit sRGB 412KB. The first 1920x1080 is the dimensions; the rest is geometry, depth, colorspace, and file size. Handy for eyeballing, noisy for scripting.

For scripting, pin the output down with -format. The %w and %h specifiers are width and height in pixels:

bash
# Just the numbers, space-separated
magick identify -format "%w %h" photo.jpg
# The classic WxH string
magick identify -format "%wx%h\n" photo.jpg

On ImageMagick 7 the command is magick identify. The bare identify (and convert) still work as legacy aliases, so if you are on an older box or a script you inherited, 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 handles far more than JPEG and PNG: it will read 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, since it shells out to Ghostscript to read PDFs.)

A note worth keeping: identify is cross-platform (Linux, macOS via Homebrew, Windows), but it is a separate install. On macOS that is brew install imagemagick; on Debian or Ubuntu, sudo apt install imagemagick. If you would rather not install anything on a Mac, the next option is built in.

macOS: sips (nothing to install)

sips (Scriptable Image Processing System) ships with macOS, so on any Mac you can read dimensions with zero setup:

bash
sips -g pixelWidth -g pixelHeight photo.png

It prints the path followed by labelled lines:

bash
/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 than identify but slightly more work to parse in a script, so if you are scripting on a Mac and already have ImageMagick installed, identify -format is cleaner. For a one-off check on a stock Mac, sips wins because there is nothing to install.

A quick guess with file

file is on virtually every Unix system and will tell you the dimensions as part of its type description:

bash
file photo.png

You 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 dimension string is part of free-form text whose exact wording varies by format and by file version, and some formats report no dimensions at all. Use it when ImageMagick is not installed and you just want a glance. For anything you parse, reach for identify or sips.

Video: ffprobe

An image tool will not read a video's resolution; for that, use ffprobe, which ships with ffmpeg:

bash
ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=p=0 clip.mp4

That prints 1920,1080. Breaking it down: -v error silences ffprobe's normal banner so only the answer comes back; -select_streams v:0 targets the first video stream (a file can carry several streams, and you do not want the audio stream's blank dimensions); -show_entries stream=width,height asks for just those two fields; and -of csv=p=0 formats them as bare comma-separated values with no key prefix. Swap the output to -of default=noprint_wrappers=1 if you would rather see width=1920 / height=1080 labelled.

Batch over a folder

To print dimensions for every image in a directory, loop and let identify do the work per file:

bash
for f in *.jpg *.png; do
  echo "$f: $(magick identify -format "%wx%h" "$f")"
done

Quote "$f" so filenames with spaces survive. If a glob matches nothing (say there are no .png files), bash passes the literal pattern through and identify errors on it; shopt -s nullglob before the loop makes an empty glob expand to nothing instead. On macOS without ImageMagick, the same idea works with sips, which already accepts multiple files in one call:

bash
sips -g pixelWidth -g pixelHeight *.png

FAQ

See also

Sources

Authoritative references this article was fact-checked against.

TagsImageMagickidentifysipsffprobeimage dimensionsCLImacOS

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.