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.
Table of contents
Run the following command to install the ImageMagick package to start converting images into multiple various formats:
sudo apt -y install 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:
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
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
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:
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'
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'
The command above resized the image to 1024×768 pixels and saved as
1. Loops in Python - Flow diagram - for loop - Resized.jpg file.
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'
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:
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'
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'
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'
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”.
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
find . -name '*.jpg' -print0 | while read -d $'\0' file; do; do convert "$file" -resize 25% resized/"$file"; done
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.
We are sorry that this post was not useful for you!
Let us improve this post!
Tell us how we can improve this post?