The ultimate guide to image manipulation with ImageMagick

Andrei Maksimov

Andrei Maksimov

0
(0)

ImageMagick is a versatile and widely used command-line utility for converting images from one format to another. It supports over 200 image formats, including JPEG, JPG PNG, GIF, PDF, BMP, SVG, and more. ImageMagick can be 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 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

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).

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

Converting image format

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

Specify the format in which you want to convert your image:  

convert WindowsImages-016.png Output_Image.jpeg

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

You can convert all *.png images into JPEG format using mogrify:

mogrify -format jpeg *.png

Now, let’s try it with 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 image or rapidly make a thumbnail, for example.

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

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

Crop image

If you need to remove somewhat from the top or sides of your image, you can crop 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, for the cropped image Conditionals in Python - Cropped.png we specified target dimensions of 640×480 pixels and the offset +320+180 to identify where the cropping should begin.

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 image clockwise to 90 degrees and save it as Conditionals in Python - Rotated.png file:

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.

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

You can execute multiple operations using a single command 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, how we can apply resize operation in a bulk to all images.

Create a directory named “resized”.

mkdir resized

Now, use can execute the following command to resize all .jpg files in your current directory to 25% of their original size 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 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. 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.

How useful was this post?

Click on a star to rate it!

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Top rated Udemy Courses to improve you career

Subscribe to our updates

Like this article?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on linkedin
Share on Linkdin
Share on pinterest
Share on Pinterest

Want to be an author of another post?

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

Leave a comment

If you’d like to ask a question about the code or piece of configuration, feel free to use https://codeshare.io/ or a similar tool as Facebook comments are breaking code formatting.