For those who work with image editing, ImageMagick is a must-have tool. This free source software suite provides a wide range of functions for manipulating and editing images in various formats.
ImageMagick has an interesting history dating back to 1987, when it was created by John Cristy. Since then, it has evolved to become one of the most popular and versatile tools for image processing.
If you’re new to ImageMagick or just want to improve your skills, this article presents 10 essential and practical commands to help you get started. From resizing an image or converting formats to modifying images in batch, these commands will give you a good idea of what ImageMagick can do.
Convert an image to another format
One of the most basic functions.
convert INPUT_IMAGE.jpg OUTPUT_IMAGE.png
convert
: This is the main ImageMagick command for converting or manipulating images.INPUT_IMAGE.jpg
: The file name of the input image to be modified in jpg format is specified.INPUT_IMAGE.jpg
: File name of the output image to be modified in png format.
Resize an image to a certain width
Another simple example.
convert INPUT_IMAGE.jpg -resize 1200x OUTPUT_IMAGE.jpg
INPUT_IMAGE.jpg
: The file name of the input image to be resized is specified.-resize 1200x
: This parameter sets the new size of the output image. In this case, the image width is set to 1200 pixels. The height will be automatically adjusted to maintain the original proportions of the image.OUTPUT_IMAGE.jpg
: Specifies the file name of the resulting output image after resizing the input image.
Enhancing the contrast and saturation of an image
Images on the web often suffer from low contrast and sometimes low saturation. To fix this:
convert INPUT_IMAGE.jpg -contrast -modulate 120 OUTPUT_IMAGE.jpg
INPUT_IMAGE.jpg
: Specify the file name of the input image to be modified.-contrast
: This parameter enhances the contrast of the input image. Contrast is defined as the perceived luminance difference between the lightest and darkest parts of an image. By increasing the contrast, the differences between the tones become more pronounced, which can make the image sharper and easier to see.-modulate 120
: This parameter modifies the saturation of the image. Saturation refers to the purity or intensity of a color. By increasing saturation, colors become more vivid and saturated. In this example, saturation is set to 120, which means it is increased by 20% from the original value.
Add logo to image
convert INPUT_IMAGE.jpg -quality 70 -strip \( LOGO.png -resize 300x -gravity SouthEast -geometry 15 15 \) -composite OUTPUT_IMAGE.jpg
INPUT_IMAGE.jpg
: The file name of the input image to be modified is specified.-quality 70
: This parameter sets the quality of the output image in a range from 0 to 100. In this example, the quality is set to 70.-strip
: This parameter removes any extra non-graphical metadata from the output image, such as EXIF data, to reduce the file size.( LOGO.png -resize 300x -gravity SouthEast -geometry 15 15 )
: This section of the command is enclosed in parentheses to indicate that it is a set of operations to perform on the logo image before combining it with the input image. The following changes are applied to the logo image:LOGO.png
: The file name of the logo image is specified.-resize 300x
: Resizes the logo image to a width of 300 pixels, maintaining the original aspect ratio.-gravity SouthEast
: Sets the gravity in the southeast of the logo image, which determines its position when superimposing it.-geometry 15 15
: Sets a distance of 15 pixels to the right and 15 pixels up from the bottom right corner (due to the gravity set above) to position the logo in the input image.
-composite
: This parameter tells ImageMagick to combine the input image and the previously processed logo image.OUTPUT_IMAGE.jpg
: The file name of the output image resulting from the operation is specified.
Download image from the Internet, make it smaller and optimize it.
convert https://blogandweb.com/INPUT_IMAGE.jpg -resize 1200x -quality 60 -strip OUTPUT_IMAGE.jpg
https://blogandweb.com/INPUT_IMAGE.jpg:
Specifies the URL of the input image to be modified.-resize 1200x
: This parameter resizes the input image to a maximum width of 1200 pixels, maintaining the original aspect ratio of the image.-quality 60
: This parameter sets the quality of the output image in a range from 0 to 100. In this example, the quality is set to 60.-strip
: This parameter removes any extra non-graphical metadata from the output image, such as EXIF data, to reduce the file size.OUTPUT_IMAGE.jpg
: The file name of the output image file resulting from the operation is specified.
Removing border in an image
convert INPUT_IMAGE_WITH_BORDERS.jpg -shave 10x10 OUTPUT_IMAGE.jpg
INPUT_IMAGE_WITH_BORDERS.jpg
: Specifies the file name of the input image file with borders to be removed.-shave 10x10
: This parameter removes a specified number of pixels from the borders of the input image. In this example, 10 pixels are removed from each horizontal and vertical side of the image.OUTPUT_IMAGE.jpg
: Specifies the file name of the resulting output image after applying the changes specified in the command.
Adding text to an image
convert INPUT_IMAGE.jpg -gravity South -pointsize 24 -annotate 0 'Creative Commons' OUTPUT_IMAGE.jpg
INPUT_IMAGE.jpg
: Specifies the filename of the input image file to be modified.-gravity South
: This parameter sets the gravity in the south of the image, which means that the text will be aligned at the bottom of the image.-pointsize 24
: This parameter sets thepoint size
of the text to 24.-annotate 0 'Creative Commons'
: This parameter adds an annotation (text) to the image. The number 0 indicates that the text should not have a frame. ‘Creative Commons’ is the text to be added to the image.OUTPUT_IMAGE.jpg
: The file name of the output image file resulting from the operation is specified.
Joining two or more images in an assembly
convert INPUT_IMAGE_1.jpg INPUT_IMAGE_2.jpg INPUT_IMAGE_3.jpg append OUTPUT_IMAGE.jpg
INPUT_IMAGE_1.jpg
,INPUT_IMAGE_2.jpg
,INPUT_IMAGE_3.jpg
: The file names of the input images to be used to create the montage are specified.append
: This parameter tells ImageMagick to append the input images side by side in a single output image.OUTPUT_IMAGE.jpg
: The file name of the output image file resulting from the montage is specified.
Modify images in batches
If you need to apply the same effect or change to a group of images you don’t need to do it one by one.
convert *.jpg -resize 800x600! OUTPUT_d.jpg
*.jpg
: This file pattern specifies that all JPG files in the current directory should be selected.-resize 800x600!
: This parameter sets the size of the output images to 800 pixels wide and 600 pixels high. The exclamation mark (!) after the dimensions indicates that the image should be resized keeping its original aspect ratio. If the exclamation mark is omitted, the image may be distorted if the proportions do not match the specified dimensions.OUTPUT_d.jpg
: This parameter specifies the file name of the output image. The symbol `d` is a placeholder that will be replaced by a three-digit number that will be incremented for each processed image. For example, the output images will be named OUTPUT_001.jpg, OUTPUT_002.jpg, etc.
In short, this command will resize all JPG images in the current directory to 800×600 pixels, maintaining their original proportions, and save the output images with an incremental name in the same directory.
Creating animated GIF files
convert -delay 20 -loop 0 INPUT_IMAGE_1.jpg INPUT_IMAGE_2.jpg INPUT_IMAGE_3.jpg OUTPUT_IMAGE.gif
INPUT_IMAGE_1.jpg
INPUT_IMAGE_2.jpg
INPUT_IMAGE_3.jpg
: The file names of the input images to be used to create the animated GIF are specified.-delay 20
: This parameter sets the amount of time (in hundredths of a second) that each image will be displayed in the animated GIF. In this example, each image will be displayed for 20 hundredths of a second.-loop 0
: This parameter indicates how many times the animated GIF will be repeated. A value of 0 indicates that it will repeat indefinitely.OUTPUT_IMAGE.gif
: The file name of the animated GIF resulting from the operation is specified.
Conclusion
ImageMagick is in my opinion an indispensable tool for image manipulation. Its power lies in its ability to edit one or several images in a simple and very fast way. Although the fact that it does not have a graphical user interface may scare more than one, it is not complicated to learn how to use it thanks to simple syntax and the large number of guides and examples online.
My personal experience with ImageMagick has been very positive. It has allowed me to leave Gimp alone for more complex edits, which has saved me a lot of time. In addition, its ability to manipulate multiple images at the same time has been very useful for both web projects and personal photo editing.