Unlock the Power of ImageMagick: A Beginner's Guide to Image Processing

ImageMagick is a free and open-source command-line tool that allows users to perform various image processing tasks, such as image conversion, resizing, cropping, rotating, adding effects, and more. It supports a wide range of image formats and can be used on Linux, Windows, and macOS. In this tutorial, we'll cover some of the most common commands and options in ImageMagick.

Installation

Before we get started, you'll need to have ImageMagick installed on your system. You can download it from the official website, or use your package manager if you're on Linux. To check if it's installed, open your terminal and run:

$ convert --version

If you see a version number, then you're good to go! Otherwise, you'll need to install ImageMagick first.

Basic Commands

Let's start with some basic commands that you'll use frequently:

Convert an image format

To convert an image from one format to another, use the convert command followed by the input and output file names. For example, to convert a JPEG image to PNG, run:

$ convert input.jpg output.png

You can also convert multiple files at once by specifying a wildcard character:

$ convert *.jpg output/%03d.png

This will convert all JPEG files in the current directory to PNG and save them in the output directory with a three-digit sequence number as the filename.

Resize an image

To resize an image, use the convert command with the -resize option followed by the new dimensions. For example, to resize an image to 800x600 pixels, run:

$ convert input.jpg -resize 800x600 output.jpg

You can also specify the new dimensions as a percentage:

$ convert input.jpg -resize 50% output.jpg

This will resize the image to 50% of its original size.

Crop an image

To crop an image, use the convert command with the -crop option followed by the new dimensions. For example, to crop an image to 400x400 pixels starting from the top left corner, run:

$ convert input.jpg -crop 400x400+0+0 output.jpg

The 400x400 part specifies the new dimensions, and 0+0 specifies the starting point of the crop. You can also specify the starting point as a percentage of the image size:

$ convert input.jpg -crop 50%x50%+0+0 output.jpg

This will crop the image to 50% of its original size, starting from the top left corner of the image.

Rotate an image

To rotate an image, use the convert command with the -rotate option followed by the angle. For example, to rotate an image 90 degrees clockwise, run:

$ convert input.jpg -rotate 90 output.jpg

You can also specify the rotation angle as a negative value to rotate counterclockwise:

$ convert input.jpg -rotate -45 output.jpg

Add a border to an image

To add a border to an image, use the convert command with the -border option followed by the border width and color. To add a black border with a width of 10 pixels, run:

$ convert input.jpg -bordercolor black -border 10x10 output.jpg

Note that the bordercolor option must precede the border option.

Adding a blur effect

To add a blur effect to an image, use the convert command with the -blur option followed by radius and sigma values. The sigma value determines the amount of blurring. To add a Gaussian blur with radius and sigma values of 5, run:

$ convert input.jpg -blur 5x5 output.jpg

Adding a sharpen effect

To add a sharpen effect to an image, use the convert command with the -sharpen option followed by the radius and sigma. To add a sharpen effect with a radius of 0 and a sigma of 1, run:

$ convert input.jpg -sharpen 0x1 output.jpg

You can adjust the radius and sigma values to create different levels of sharpening.

Adding a sepia effect

To add a sepia effect to an image, use the convert command with the -sepia-tone option followed by the threshold as a percentage of the intensity (0 - 99.9%). To add a sepia effect with a threshold of 80%, run:

$ convert input.jpg -sepia-tone 80% output.jpg

Adding a grayscale effect

To add a grayscale effect to an image, use the convert command with the -colorspace option followed by the colorspace name. For example, to convert an image to grayscale, run:

$ convert input.jpg -colorspace Gray output.jpg

You can also use the brightness-contrast option to adjust the brightness and contrast of the image:

$ convert input.jpg -brightness-contrast -20x10 output.jpg

This will decrease the brightness by 20% and increase the contrast by 10%.

Adding a watermark

To add a watermark image to an image, use the composite command with the -dissolve option followed by the opacity and the watermark image. For example, to add a watermark image with an opacity of 50% to the bottom right corner of an image, run:

$ composite -dissolve 50% -gravity southeast watermark.png input.jpg output.jpg

You can adjust the opacity and gravity values to position the watermark wherever you like. There are many more effects you can add to your images. Experiment with different options and see what kind of creative effects you can come up with!

Conclusion

These are just some basic commands to get started with ImageMagick. There are many more advanced features and options available, so be sure to check out the ImageMagick documentation for more information.


This post was written by Ramiro Gómez (@yaph) and published on . Subscribe to the Geeksta RSS feed to be informed about new posts.

Tags: command line image processing tutorial

Disclosure: External links on this website may contain affiliate IDs, which means that I earn a commission if you make a purchase using these links. This allows me to offer hopefully valuable content for free while keeping this website sustainable. For more information, please see the disclosure section on the about page.


Share post: Facebook LinkedIn Reddit Twitter

Merchandise