Tutorial

Command-line Basics: Resizing Images with ImageMagick

Updated on June 1, 2022
    Default avatar

    By joshtronic

    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.

    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:

    1. sudo apt update

    Then, install imagemagick:

    1. 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:

    1. 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:

    1. 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:

    1. 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:

    1. 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 “mogrifyed” versions of the previous commands:

    1. 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:

    1. mogrify -resize 100x100! ./some/path/*.png
    2. 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.

    Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

    Learn more about us


    About the authors
    Default avatar
    joshtronic

    author

    Still looking for an answer?

    Ask a questionSearch for more help

    Was this helpful?
     
    1 Comments
    

    This textbox defaults to using Markdown to format your answer.

    You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

    The exclamation mark is liable to be replaced or throw an error since it is a special character in bash. According to the imagemagick documentation, you should escape the exclamation mark, as in 100x100!. I’ve found that you can also add single quotes such as ‘100x100!’ which looks nicer.

    Try DigitalOcean for free

    Click below to sign up and get $200 of credit to try our products over 60 days!

    Sign up

    Join the Tech Talk
    Success! Thank you! Please check your email for further details.

    Please complete your information!

    Get our biweekly newsletter

    Sign up for Infrastructure as a Newsletter.

    Hollie's Hub for Good

    Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

    Become a contributor

    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

    Welcome to the developer cloud

    DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

    Learn more
    DigitalOcean Cloud Control Panel