SEARCH

How-To Geek

How to Quickly Resize, Convert & Modify Images from the Linux Terminal

header imagemagick

ImageMagick is a suite of command-line utilities for modifying and working with images. ImageMagick can quickly perform operations on an image from a terminal, perform batch processing of many images, or be integrated into a bash script.

ImageMagick can perform a wide variety of operations. This guide will introduce you to ImageMagick’s syntax and basic operations and show you how to combine operations and perform batch processing of many images.

Installation

ImageMagick isn’t included in the default installations of Ubuntu and many other Linux distributions. To install it on Ubuntu, use the following command:

sudo apt-get install imagemagick

Converting Between Formats

The convert command takes an image, performs actions on it, and saves the image with the file name you specify. One of the most basic things you can do with it is converting images between formats. The following command takes a PNG file named “howtogeek.png” in the current directory and creates a JPEG image from it:

convert howtogeek.png howtogeek.jpg

convert formats

You can also specify a compression level for JPEG images:

convert howtogeek.png -quality 95 howtogeek.jpg

The number must be between 1 and 100. ImageMagick uses the quality level of the input image, if possible. If not, ImageMagick defaults to 92.

Resizing Images

The convert command can also quickly resize an image. The following command asks ImageMagick to resize an image to 200 pixels in width and 100 pixels in height:

convert example.png -resize 200×100 example.png

We’ve used the same file name here, so ImageMagick will overwrite the original file.

resize

ImageMagick will try to preserve the aspect ratio if you use this command. It will alter the image to fit within a 200×100 area, but the image may not be exactly 200×100. If you want to force the image to become a specific size – even if it messes up the aspect ratio – add an exclamation point to the dimensions:

convert example.png -resize 200×100! example.png

You can also specify a specific width or height and ImageMagick will resize the image to that width or height while preserving the aspect ratio. The following command will resize an image to a width of 200:

convert example.png -resize 200 example.png

The following command will resize an image to a height of 100:

convert example.png -resize x100 example.png

Rotating an Image

ImageMagick can quickly rotate an image. The following command takes an image named howtogeek.jpg, rotates it by 90 degrees and saves the rotated image as howtogeek-rotated.jpg:

convert howtogeek.jpg -rotate 90 howtogeek-rotated.jpg

If you specified the same file name, ImageMagick would save the rotated image over the original image file.

rotate

Applying Effects

ImageMagick can apply a variety of effects to an image. For example, the following command applies the “charcoal” effect to an image:

convert howtogeek.jpg -charcoal 2 howtogeek-charcoal.jpg

charcoal

The charcoal command applies an artistic “charcoal” style effect to an image – the 2 in the command lets you control the strength of the effect.

howtogeek-charcoal

The following command applies the “Implode” effect with a strength of 1:

convert howtogeek.jpg -implode 1 howtogeek-imploded.jpg

implode

The implode effect makes it appear as if there’s a black hole at the center of the image.

howtogeek-imploded

Combining Operations

All these operations can be combined. With a single command, you could resize an image, rotate it, apply an effect, and convert it to another format:

convert howtogeek.png -resize 400×400 -rotate 180 -charcoal 4 -quality 95 howtogeek.jpg

howtogeek-complex

This is just the start of what you can do with ImageMagick. There are many more operations you can combine.

Batch Processing

You can take advantage of Bash to quickly do batch processing of many images. For example, the following command would take all PNG files in the current directory, rotate them, and save a new copy of each with “-rotated” added to the beginning of each file name.

for file in *.png; do convert $file -rotate 90 rotated-$file; done

batch processing

You can easily modify this command to perform other actions. You can also integrate batch processing commands into a Bash shell script to automate image-processing operations.


Any article on ImageMagick will omit a lot of what you can do with it – there are just too many options and commands. If you’re interested in doing more with ImageMagick, check out the official documentation on the ImageMagick website for a much more in-depth look at ImageMagick.

Chris Hoffman is a technology writer and all-around computer geek. He's as at home using the Linux terminal as he is digging into the Windows registry. Connect with him on Google+.

  • Published 03/22/12

Comments (6)

  1. JDS

    Awesome terminal tricks.
    Command-line interface beats GUI.

  2. Michael

    Love command line for doing a lot of files at once. Thanks for the tip.

  3. Gian-Luigi Valle

    This converts any pictures in a folder to png’s ready for email. Great if you have a camera that makes huge image files:
    ~$ mogrify -resize 1020×1403 -format png *

    This converts pictures to pdf:
    ~$ convert 1.png 2.png all.pdf

  4. CR7

    Your Article is the best . thanks Chris …….

  5. sclewin

    Great article. I have been looking for something to quickly resize images and this will easily fit that need; thanks. :)

  6. Roland

    How to get the geometry of an image:
    identify filename|awk ‘{print $(NF-6)}’
    You have to index from the right because filenames can contain spaces.
    Pipe it further to separate X and Y:
    awk -Fx ‘{print $1,$2}’

Get Free Articles in Your Inbox!

Join 134,000 newsletter readers

Email:

Go check your email!