Manipulating images with ImageMagick command


ImageMagick is an open source software suite for image manipulation. We can install it on our system using apt−get and then run commands through its command line interface (CLI).

We’ll take a quick peek at some of the most popular methods for manipulating images with ImageMagick.


Let’s start by downloading ImageMagick, which we can install using our package manager (e.g., apt). We can also download the binary directly or compile it from the sources.

After installing the software, let’s check whether we've successfully installed it by looking at its current status.

$ magick -version
Version: ImageMagick 7.0.8-13 Q16 x86_64 2018-10-21
Copyright: © 1999-2018 ImageMagick Studio LLC

After installing the extension successfully, you’ll be able to view the installed extension in the Chrome Web Store.

Converting an Image to Another Format

It’s essential to note that ImageMagick has a huge number of powerful tools and capabilities. However, our example will focus on manipulating images using the convert tool. We’ll tackle some basic, practical uses, but in reality, these are just the tip of the iceberg.

The Convert command has a staggering number of 237 different commands that allow you to perform a wide variety of tasks. Let’s start with the simplest −

$ convert flower_original.jpeg flower_original.png

We can clearly observe that this command changes a single file flower_original.jpg into flower_new.png without altering its original content.

If we look at the new image, we'll notice that it's identical except for its format and, of course, the file sizes.

$ magick identify flower_original.png
flower_original.png PNG 400x400 400x400+0+0 8-bit sRGB 163499B 0.000u 0:00.000

If we have a list of images, we can apply an image processing algorithm to each one of them.

$ convert *.jpg *.png

We can now inspect the many different formats that are available to us −

$ identify -list format
   Format Module Mode Description
      3FR    DNG     r--    Hasselblad CFV/H3D39II
      3G2    MPEG    r--    Media Container
      3GP    MPEG    r--    Media Container

It’s especially useful because we can even convert our JPEG images into PDF documents.

Resizing an Image

An additional common operation is to resize images. For instance, we could use the –resize to resize an existing JPEG file

convert flower_original.jpeg -resize 64x64 flower_64x64.jpeg

Here, we resize an original flower picture into a smaller square 64x64 pixel size.

We can also use the resize option to specify a percentage value for scaling an element. For example, if we want to scale our logo by 50%, we would type −

convert flower_original.jpeg -resize 50% half_flower.jpeg

We recommend using the default settings for most people, but if you want to get an exact pixel count, you can change the output resolution by clicking the "Resize" button at the bottom right corner of the dialog box.

To conclude this chapter, usually, the most common method for changing the width/height of an image is by shrinking it to fit the desired dimensions. However, using the resize options we've discussed, it is, of couse, perfectly feasible to increase our image.

Flipping and Rotating

Let’s now take a closer examination of how we can flip and/or turn our images so they can sometimes be useful.

convert flower_original.jpeg -flip flipped_flower.jpeg

It’ll be just as easy as using the −flop command, which will create an upside down version of the original file.

Let's now see how we can rotate images by applying the −rotate command.

convert flower_original.jpeg -rotate 60 rotate_clockwise_flower.jpeg

We need our image to shift 60 degrees clockwise.

If we provide a negative number such as -90, then the rotation will occur in a clockwise direction.


Let's take a look at how we can manipulate images by cropping them.

convert flower_original.jpeg -crop 180x170+50+50 cropped_flower.jpegCopy

Simply put, the −crop image operator will cut out the part of the image in the current sequence at the size and position we specify using the geometry argument.

In this example, we’re specifying the width and height of the geometry argument, which will give the size of the image that remains after cropping (180×170). Then the x and y in the offset (+50+50) gives us the location of the top left corner of the cropped image with respect to the original image.

Colour Effects

Now let’s take a look at how we can apply some simple but striking color effects to our flower image.

Starting from the left of the two images, in the first image, we’re explicitly setting the colourspace to Gray −

convert flower_original.jpeg -colorspace Gray greyscale_flower.jpeg

This will merely convert color to gray. In fact, we can list all the colour spaces available to us −

$ convert -list colorspace

On the other hand, in the second image, we’re using the −monochrome option to transform the image to true black and white.

We can also replace each pixel with its complementary color using the −negate option.

As a result, the red, green, and blue intensities of our image are negated. White becomes black, and yellow becomes blue, etc.

In our final image, we simulate a charcoal drawing −

convert flower_original.jpeg -charcoal 1.2 charcoal_flower.jpeg

This flag takes an additional associated factor. Using a small number usually results in a pretty accurate output.

Adding a Border

In our penultimate example, we’ll look at a neat feature using a couple of the CLI’s border options −

convert flower_original.jpeg -bordercolor yellow -border 5 flower_with_border.jpeg

In this example, we set the border color to yellow and the border size using the -border option.

As we can see, we’re left with a vibrant yellow border around our original flower image.

Creating an Animation

Let’s imagine we have a number of images spanning over a time period that we’d like to merge into an animated gif. Let’s say we have some images of the sun, which are captured roughly every 2 seconds.

Assuming our images are ordered sequentially in the correct time order, which they are −

$ ls -1

We can run a command to generate an animation from all the *.jpg image files in our directory −

convert -loop 0 *.jpg sol.gif

The output will be a video of the sun, which looks really cool.

We're using the loop option with a 0 for the iteration count. This means that the loop will run infinitely.

You can also use the − transition−duration [seconds] option to specify the length of time between each transition.

As a result, we could use different combinations of options, including resize to keep the final image size small.

convert -delay 20 -loop 0 -resize 50% *.jpg sol.gif


We've discussed some of the basic features provided by ImageMagick for image manipulation using the command line.

We first learned about some basic image editing features, and then we looked at how to resize, rotate, and crop them. Next, we learned how to apply some funky color effects, and finally we ended up creating an animated GIF.

We've only scratched the surface, so be sure to read the excellent documentation for more detailed instructions.

Updated on: 26-Dec-2022


Kickstart Your Career

Get certified by completing the course

Get Started