How to negate image using ImageMagick

The ImageMagick convert tool can be used to negate an image using the -negate and +negate options. -negate replaces every grayscale or RGB pixel with its inverted or complementary color. Use +negate when you want to restrict this operation to only work on grayscale pixels.


$ convert -negate foo.png inverted_foo.png

Tried with: ImageMagick 6.9.7 and Ubuntu 18.04


How to create GIF from images using ImageMagick

ImageMagick is truly a magical tool for images. It makes it super-easy to create an animated GIF file from a series of image files.

To create an animated GIF from 3 input images with a delay of 2 seconds between each frame and loop infinitely:

$ convert -delay 200 -loop 0 1.png 2.png 3.png out.gif

Tried with: ImageMagick and Kubuntu 16.04

ImageMagick display

ImageMagick display

The display program of ImageMagick is useful when you are at the shell and need to quickly view one or a few images. It can also be used to apply many of the image processing operations available in ImageMagick on an image and view it. This viewer is extremely light and great for checking some images quickly on a remote computer over SSH.

  • If you do not have ImageMagick, installing it is easy:
$ sudo apt install imagemagick
  • To view a specific image:
$ display foo.jpg
  • To view all the JPEG files in current directory:
$ display *.jpg
  • To display all image files in current directory:
$ display *
  • If you know that you will not be changing the images, but just viewing them, then open them in immutable mode:
$ display -immutable *.jpg
  • Press F1 to view help about the mouse operations and keyboard operations available in this program.

  • Some of the keyboard shortcuts I frequently use:

Space: Next image
Backspace: Previous image
Esc: Quit
/: Rotate by 90 degrees
<: Zoom out
>: Zoom in

Tried with: ImageMagick and Ubuntu 16.04

How to rotate image using ImageMagick

Rotating images from the shell is easy with ImageMagick tools.

To rotate 90 degrees clockwise and write output as new file:

$ convert -rotate "90" in.jpg out.jpg

To rotate 45 degrees counter-clockwise and overwrite the input file:

$ mogrify -rotate "-45" foo.jpg

Tried with: ImageMagick and Ubuntu 15.10

How to save image in different format

A common operation when working with images is to save them in a different format. Not surprisingly, ImageMagick has tools that make this very easy to perform at the shell.

The easiest way to save an image in a different format is to use convert:

$ convert foo.png bar.jpg

This saves an existing foo.png in PNG format to another file bar.jpg in the JPEG format.

Note that this command does not work if you want to convert a set of images. To do that, we use the mogrify command with its -format option.

To convert all PNG images in the current directory to JPEG format images keeping the same filename:

$ mogrify -format "jpg" *.png

Mogrify usually overwrites its source files. In this case however, the original PNG files are left untouched since the new files have a different file extension.

Tried with: ImageMagick 6.7.7 and Ubuntu 14.04

Identify: Another useful tool from ImageMagick

Identify is a useful tool from the ImageMagick basket to remember. Any time you are at the shell and want to quickly learn some information about any type of image file, try identify:

$ identify foo.png
foo.png PNG 203x463 203x463+0+0 8-bit DirectClass 9.31KB 0.000u 0:00.000

For more detailed information:

$ identify -verbose foo.png

This will literally give you all the information you can imagine about the image file.

To query for certain specific properties of an image file, see the other options that can be specified to Identify here.

Tried with: ImageMagick 8: and Ubuntu 14.04

Memory usage of image viewers

Resident memory used by image viewers
Resident memory used by image viewers
Virtual memory used by image viewers
Virtual memory used by image viewers

I use image viewer programs all day long to examine images captured from different sources. I was curious to see how much memory different viewers used. This might help in picking the right image viewer for a resource constrained system.

I have considered only simple image viewers, which allow you to view one or multiple images. Editing features are not really needed. The image viewers I considered were:

  • Eye of GNOME (eog): This is the default image viewer on Ubuntu.

  • Viewnior

  • Qiv

  • PQiv

  • GPicView

  • Xfe: A lean file manager that can also be used to view images.

  • Feh: A minimal image viewer.

  • ImageMagick Display: Can only display a single image in a X window.

I looked at resident memory (RES) that is the actual RAM used by the viewer. I also looked at virtual memory (VIRT), since the viewer might require the OS to load many libraries.

The results are shown above. It can be clearly seen that Feh does pretty well and is my choice for a minimal image viewer.

Tried with: Ubuntu 14.04

How to set background color in ImageMagick

PDF, SVG, PNG and many other image formats can have a transparent background. Sometimes, you might want to set the background color in image files. This can be done easily using the -background and -flatten options.

For example, to convert a PDF to JPG file and set the background color in the resulting file to white:

$ convert foo.pdf -background "#FFFFFF" -flatten foo.jpg

Tried with: ImageMagick and Ubuntu 14.04

How to crop image using ImageMagick

To crop an image using ImageMagick, you need to specify the X and Y coordinates of the top corner of the crop rectangle and the width and height of the crop rectangle. Use the mogrify command if you want the images to be replaced in-place or use the convert command otherwise to make a copy.

If you want the crop rectangle to start at top corner X: 50 Y: 100 and the crop rectangle to be of size W: 640 H:480, then use the command:

$ mogrify -crop 640x480+50+100 foo.png

To write the cropped image to a new file:

$ convert foo.png -crop 640x480+50+100 out.png

Tried with: ImageMagick and Ubuntu 14.04

How to join images using ImageMagick

If you want to tile images on a 2D grid, then check out how to use the tile command.

If you just want to join images side-by-side or one below another into a single row or column, then there is an easier command called append.

To join images horizontally side-by-side into a row:

$ convert *.png +append out.png

To join images vertically one below another into a column:

$ convert *.png -append out.png

Tried with: ImageMagick and Ubuntu 14.04