The ultimate guide to image manipulation with ImageMagick

Laptop with code
Related Content

ImageMagick is a versatile and widely used command-line utility for converting, resizing images, and manipulating images from one format to another. It supports over 200 image formats, including JPEG, JPG, PNG, GIF, PDF, BMP, SVG, and more. ImageMagick is a command-line based for manipulating images used for various purposes such as rotating an image, transforming images, crop images, adding captions, modifying picture colors, adding special effects to photos, and drawing lines, polygons, and other shapes on them. This article covers the most widely used ImageMagick image manipulation commands.

Installing ImageMagick

Run the following command to install the ImageMagick package in your command line to start converting images into multiple various formats:

sudo apt -y install imagemagick
1. The ultimate guide to image manipulation with ImageMagick - apt-get install imagemagick

Using ImageMagick

How To Resize Image in Microsoft Paint

How To Resize Image in Microsoft Pa...
How To Resize Image in Microsoft Paint

Convert is a command-line utility that is a part of the “ImageMagick” package.

Once the ImageMagick packages have been installed, ensure the convert utility also has been installed:

convert --version
2. The ultimate guide to image manipulation with ImageMagick - convert version

Listing image formats

ImageMagick can assist with converting image format from one format to another, and it is one of the most valuable tasks.

To list all supported formats, use the following command:

convert -list format | more
3. The ultimate guide to image manipulation with ImageMagick - convert list-formats

In addition to the most commonly used formats like BMP (bitmapped image in Microsoft Windows), Graphics Interchange Format (GIF), JPG (JPEG image format), and PNG (Progressive Network Graphics), ImageMagick supports a wide range of additional file types, including all major video formats (MKV, MP4, AVI, MOV). Also, you can find useful information about an image, such as its file type, size, and dimensions, using the identify command, as it will provide details additionally.

It can also convert EPSF (Encapsulated Postscript) and PDF (Portable Document Format), which is helpful in some situations.

Converting image format with the convert command

Converting an image from one format to another format is straightforward.

Specify the format in which you want to convert your source image or input image you desire, then use the convert command with its output filename:  

convert WindowsImages-016.png Output_Image.jpeg

4. The ultimate guide to image manipulation with ImageMagick - convert png to jpg

You can use the convert command to convert all *.png images into the resulting image with a JPEG format using the mogrify command, without harming your original file, which accepts an input file that will be modified in place:

mogrify -format jpeg *.png

Now, let’s try it with the following example of a folder containing multiple .jpeg images, and see what happens:

5. The ultimate guide to image manipulation with ImageMagick - morgify all files from png to jpg

Resizing image

Resize option will help you to resize your source image or rapidly make a thumbnail, for example.

Let’s say I want to resize my original image with 25% dimensions, it will generate  a new image with 25% of size and aspect ratio:

convert -resize 25% '1. Loops in Python - Flow diagram - for loop.png' '1. Loops in Python - Flow diagram - for loop.png'
6. The ultimate guide to image manipulation with ImageMagick - convert resize 25 percent

Alternatively, you can also specify the dimensions instead of percentage to resize your source image:

convert -resize 1024x768 '1. Loops in Python - Flow diagram - for loop.jpg' '1. Loops in Python - Flow diagram - for loop - Resized.jpg'
7. The ultimate guide to image manipulation with ImageMagick - convert resize by dimensions

The command above resized the image to 1024×768 pixels and saved as 1. Loops in Python - Flow diagram - for loop - Resized.jpg file. Resizing images from the command-line is just one example and is really just the tip of the iceberg.

Crop images

If you need to crop an image and remove somewhat from the top or bottom edge and sides of your source image, you can do a percentage crop to it by executing the command shown below:

convert 'Conditionals in Python.png' -crop 640x360+320+180 'Conditionals in Python - Cropped.png'
8. The ultimate guide to image manipulation with ImageMagick - convert crop

Here, is an example of the cropped image Conditionals in Python - Cropped.png we specified target output dimensions of the example image with 640×480 pixels to change its height and width and the crop offset +320+180 to identify where the cropping should begin in your input image.

Here’s a graphical explanations of crop operation:

8. The ultimate guide to image manipulation with ImageMagick - convert crop explained

Rotating image

You can rotate the image with the clock and anti-clockwise.

Use the following command to rotate Conditionals in Python - Cropped.png, an example image clockwise to 90 degrees and save it as Conditionals in Python - Rotated.png file as the output filename:

convert 'Conditionals in Python - Cropped.png' -rotate 90 'Conditionals in Python - Rotated.png'
9. The ultimate guide to image manipulation with ImageMagick - convert rotate

Adding captions to the image

You can add the caption to the image and set the text’s color, font size, and style options without reducing the quality of your original images.

Use the -draw option to specify caption in single or double quotes:

convert 'Terminal.png' -pointsize 64 -font helvetica -fill yellow -draw 'text 512,512 "Welcome to ImageMagick"' 'Terminal with text.png'
10. The ultimate guide to image manipulation with ImageMagick - convert draw

Combining multiple commands

Note that you can execute multiple operations using a single command on multiple images by specifying the image’s orientation, pixels, and size instead of doing all those actions separately when needed:

convert 'Terminal.png' -rotate 90 -crop 1000x600 -resize 25% 'Terminal transformed.png'
11. The ultimate guide to image manipulation with ImageMagick - convert multiple operations

Batch image processing

One of the most common operations is to process all the images from specific directory.

Let’s take a look at an example of how we can apply resize operation in a bulk to all images.

Create a directory, for example, and let it be named “resized”.

mkdir resized

Now, use can execute the following command to resize all .jpg files or your input file in your current directory, resizing images to 25% of their original size to change its virtual canvas information and put result files into the resized subdirectory:

find . -name '*.jpg' -print0 | while read -d $'\0' file; do; do convert "$file" -resize 25% resized/"$file"; done
12. The ultimate guide to image manipulation with ImageMagick - convert multiple images using find

Summary

ImageMagick is a powerful command-line utility for converting, resizing images, and manipulating images from one format to another supporting a wide range of formats. It is a cross-platform platform that works on macOS, Linux, and Windows. ImageMagick can help you with various tasks, including converting images from one format to another (e.g.: png, jpeg, pdf, gif, Mp4, and etc), resize the image, blur image, adding colors, and insert a caption to an image using ImageMagick. In addition, rotating an image to the clock or anticlockwise, crop images, add effects, and many more features are available. In this guide, we illustrated how images manipulate with Imagemagick to convert from one format to another.

LIKE THIS ARTICLE?
Facebook
Twitter
LinkedIn
Pinterest
WANT TO BE AN AUTHOR OF ANOTHER POST?

We’re looking for skilled technical authors for our blog!

Table of Contents