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,
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.
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
First, update your package sources:
- sudo apt update
- 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
- curl https://images.prismic.io/digitalocean/0b619d51-a723-4748-997f-39ed5697a540_intro-to-cloud.jpg?auto=compress,format --output image.jpg
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
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
.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.
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 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 “
mogrifyed” versions of the previous commands:
- mogrify -resize 100x100 original.png
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 -resize 100x100! ./some/path/*.png
- mogrify -resize 100x100^ -gravity center -extent 100x100 *.png
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.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.