Command-line Basics: Resizing Images with ImageMagick
source link: https://www.digitalocean.com/community/tutorials/workflow-resizing-images-with-imagemagick
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
// Tutorial //
Command-line Basics: Resizing Images with ImageMagick
Introduction
If you’ve ever done programmatic image manipulation, you have probably encountered the ImageMagick library or its major fork, GraphicsMagick. Essentially, ImageMagick is the most commonly-used program for resizing, converting, or otherwise manipulating images on the command line. It has libraries for integration into almost all popular programming languages , and you can use it directly with its included commands, mogrify
and convert
.
Prerequisites
Before you begin this guide, you should have a regular, non-root user with sudo privileges configured on your server. You can learn how to configure a regular user account by following our Initial server setup guide for Ubuntu 22.04.
If you would prefer to follow this tutorial using a terminal in your browser, click the Launch an Interactive Terminal! button to get started. You’ll be able to experiment with and run all the commands directly in your browser.
Getting started
The ImageMagick library is very popular, but doesn’t usually come installed by default. However, it is widely available in package managers for all platforms. On Ubuntu 22.04, you can install it with apt
.
First, update your package sources:
sudo apt update
Then, install imagemagick
:
sudo apt install imagemagick
If you don’t already have a sample image handy to work with, you can download the header image from this tutorial using curl
, and save it as image.jpg
:
curl https://images.prismic.io/digitalocean/0b619d51-a723-4748-997f-39ed5697a540_intro-to-cloud.jpg?auto=compress,format --output image.jpg
Resize to specific dimensions and keep aspect ratio
To resize an image to specific dimensions, use the convert
command with an input file, the -resize
parameter, your preferred dimensions, and an output filename:
convert original.png -resize 100x100 new.png
This won’t actually resize the image to the exact dimensions specified. What it will do is resize the image to fit within those dimensions.
You can also append ^
to the dimensions to tell ImageMagick that you’d like to resize the image to fill the dimensions, potentially overlapping on one side.
One of the two dimensions (either width or height) will be scaled exactly, while the other will be scaled proportionately and may overlap:
convert original.png -resize 100x100^ new.png
Resize to specific dimensions and keep the aspect ratio
Sometimes you’ll need to not only resize an image, but also crop it so there’s nothing overlapping. A good use case for this is user avatars. You should avoid stretching or squashing a user’s profile picture, so cropping it to a square is an acceptable solution:
convert original.png -resize 100x100^ -gravity center -extent 100x100 new.png
Note: In order to resize an image to arbitrary dimensions while ignoring the aspect ratio, you can append !
to the dimensions, like 100x100!
. Most times, you will not want to do this, as it will stretch the image.
If needed, you can change the file extension of your output file, for example from .png
to .jpg
, and ImageMagick will convert your image to that format. You can use ImageMagick this way even without resizing or otherwise modifying an image. Refer to the ImageMagick documentation for a more complete list of options.
Resizing files in place
Thus far, we’ve been converting a file and saving it to a brand new file. While being a safer option, as it’s non-destructive to your original file, this is not always an ideal workflow.
If you need to edit a file in place, you can swap the convert
command with mogrify
. The mogrify
command accepts an input file that will be modified in place.
Note: You are highly encouraged to make back ups of any images you are modifying in case you aren’t happy with the results.
Here’s the “mogrify
ed” versions of the previous commands:
mogrify -resize 100x100 original.png
Resizing multiple files
mogrify
can also operate on an entire directory of files at once, unlike convert
. In general, both ImageMagick commands use similar syntax, but convert
only works with a single specified input and output.
To edit an entire directory of images and resize them all in the same way, you can pass mogrify
a *
wildcard:
mogrify -resize 100x100! ./some/path/*.png
mogrify -resize 100x100^ -gravity center -extent 100x100 *.png
Conclusion
Resizing images from the command-line is really just the tip of the iceberg. ImageMagick supports many additional options that allow you to optimize images, resample their colors, and convert them to other formats.
Next, you may want to learn how to Serve Modern Image Formats Using imgproxy with Docker.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK