1. Overview

In this tutorial, we’ll use several tools to quickly find out and display the dimensions of an image file from the Linux command line.

2. The file Command

The file command is available on the majority of Linux distributions. Behind the scenes, it reads the metadata from the headers of the image files, thus providing a performance advantage. We can use the file command followed by image paths to find out the dimensions of images:

$ file myimage.jpeg
myimage.jpeg: JPEG image data, JFIF standard 1.01, resolution (DPI), density 300x300, segment length 16,
Exif Standard: [TIFF image data, little-endian, direntries=7, orientation=upper-left,xresolution=98,
yresolution=106, resolutionunit=2, software=GIMP 2.10.24, datetime=2021:07:07 21:18:52],
progressive, precision 8, 768x768, components 3

As we can see, it prints the metadata for myimage.jpeg. If we want to grab merely the dimensions of the image, we can grep the output:

$ file myimage.jpeg | grep -Eo "[[:digit:]]+ *x *[[:digit:]]+"
  • The -E flag will enable extended regular expressions
  • The -o option will print only the text that’s matched

However, we should know that the file command will only print dimensions for JPEG, PNG, GIF, WEBP, PGM, and PPM images.

3. The identify Command

The identify command comes with the ImageMagick suite. It identifies a lot of information for numerous image types. Like the file command, it also reads from the image header.

3.1. Installation

On most Linux distributions, ImageMagick isn’t installed out of the box. However, it should be available on the official package repositories under the name imagemagick. If our Linux distro is Debian-based or Fedora, we can use yum or apt.

Once installed, we can verify it using the command:

$ identify --version | grep Version
Version: ImageMagick 7.1.0-2 Q16 x86_64 2021-06-25 https://imagemagick.org

3.2. Usage

Using the identify tool is a piece of cake. Type in the identify command followed by the image path:

$ identify myimage.jpeg
myimage.jpeg JPEG 768x768 768x768+0+0 8-bit sRGB 86290B 0.010u 0:00.001

Apart from JPEG, we can enlist the other image types that identify supports out of the box:

$ identify --version
Version: ImageMagick 7.1.0-2 Q16 x86_64 2021-06-25 https://imagemagick.org
Copyright: (C) 1999-2021 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules OpenMP(4.5)
Delegates (built-in): bzlib cairo djvu fontconfig freetype heic jbig jng jp2 jpeg lcms
lqr ltdl lzma openexr pangocairo png raqm raw rsvg tiff webp wmf x xml zip zlib

As we can see in the “Delegates” field, the tool supports a wide range of image and document formats as well.

4. The exiv2 Manager

The exiv2 tool is used to manage image metadata. It’s meant specifically for photography users, but it’s useful in other scenarios as well.

4.1. Installation

It’s not pre-installed on many distributions, so we’ll need to install the exiv2 package from the official repository using a package manager such as yum or apt.

Once exiv2 is installed, we can verify it:

$ exiv2 --version
exiv2 0.27.4

4.2. Usage

We can use exiv2 by typing the command followed by our image path:

$ exiv2 myimage.jpeg
File name : myimage.jpeg
File size : 86290 Bytes
MIME type : image/jpeg
Image size : 768 x 768
Thumbnail : image/jpeg, 7452 Bytes

Upon running the command, it will grab all the available metadata from the image. If we want to grab only the image dimensions, we can grep its output:

$ exiv2 myimage.jpeg | grep -i "image size"
Image size : 768 x 768

The -i flag disables the case-sensitive mode. Additionally, exiv2 doesn’t work with PPM or PGM formats.

5. The mediainfo Utility

MediaInfo is a free program that is used to check for metadata about media files. It supports video, audio, and image files as well.

5.1. Installation

As before, install the package mediainfo from the official package repository using yum or apt. Alternatively, we can also use Snap to install the package:

$ snap install mediainfo

We can then verify the installation:

$ mediainfo --version
MediaInfo Command line,
MediaInfoLib - v21.03

5.2. Usage

Just like the other tools, we simply type the mediainfo command followed by image paths:

$ mediainfo myimage.jpeg
Complete name : myimage.jpeg
Format : JPEG
File size : 84.3 KiB
Format : JPEG
Width : 768 pixels
Height : 768 pixels
Color space : YUV
Bit depth : 8 bits
Compression mode : Lossy

To narrow down the output, we can grep the *Width* and *Height* fields:

$ mediainfo avatar.webp | grep -iE "width|height"
Width : 768 pixels
Height : 768 pixels

We should note that mediainfo doesn’t work with WEBP, PPM, or PGM formats.

6. Conclusion

In this tutorial, we took a look at using different tools to quickly find out the dimensions of images from the command line.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.