Use ImageMagick to work with images
ImageMagick is a handy multipurpose command-line tool for all your image needs. Available from the command line simply as magick, ImageMagick supports a variety of image types, including JPG photos and PNG graphics.
Identifying images
I often use ImageMagick on my webserver to resize images. For example, let’s say I want to share a photo of a little food delivery robot that I passed on the street, and put it on my website. I took this photo with the default resolution from my phone. But even though it’s cropped into a square image, I wonder if it’s still too big to share on a website. I can use the identify subcommand to tell me about the dimensions of my image:
$ magick identify PXL_20241016_175002510.jpg
PXL_20241016_175002510.jpg JPEG 2268x2268 2268x2268+0+0 8-bit sRGB 1.642MiB 0.000u 0:00.001
The output tells me the image is 2268×2268, and over 1.6MB in size. That’s much too large for a website.
Resizing images
Let’s use ImageMagick to change the size of my photo so that I can include it on my web page. ImageMagick is a full suite of tools. One of the most common features I use is the -resize subcommand.
To resize my photo to a more manageable 500-pixel width, type this:
$ magick PXL_20241016_175002510.jpg -resize 500x500 robot.jpg
The new image is only 120kB in size:
$ magick identify robot.jpg
robot.jpg JPEG 500x500 500x500+0+0 8-bit sRGB 119990B 0.000u 0:00.004
You can provide both width and height dimensions with the -resize option, as I’ve done here. If you provide only the width (like 500x) ImageMagick does the math for you and automatically retains the aspect ratio by resizing the output image with a proportional height.
Install ImageMagick on Linux
On Linux, you can install ImageMagick using your package manager. For instance, on Fedora or similar:
$ sudo dnf install imagemagick
On Debian and similar:
$ sudo apt install imagemagick
This article is adapted from Resize an image from the Linux terminal by Jim Hall, and is republished with permission from the author.