Advanced image editing from the command line with ImageMagick

1774

Author: Shashank Sharma

ImageMagick (IM) is a command-line graphics creation and editing application. In a previous article we used it to add text and frames to images, and for other basic image manipulation. In this article we’ll use the ImageMagick suite of commands to create a multi-image mosaic, draw some basic shapes, and create 3D logos.

Superimposing images

Pasting one image over another is known as superimposing. Using this technique we can turn the hard-corners of an image into rounded borders — or any other shape, for that matter — by simply superimposing pre-fabricated transparent curved corners onto the image. To place them at the appropriate position we use the composite command with the -gravity switch specifying the position:

$ composite -gravity SouthEast curve_se.png image.png curved-se.png

This command superimposes the curved southeast corner onto the southeast corner of the original image. We can then use the resulting image as the source image for the next command, to apply the next corner, and so on. To curve the rest of the corners of the image:


$ composite -gravity NorthEast curve_ne.png curved-se.png curved-ne.png
$ composite -gravity NorthWest curve_nw.png curved-ne.png curved-nw.png
$ composite -gravity SouthWest curve_sw.png curved-nw.png curved-sw-final.png

The -gravity switch offers some more positions that can be used to place images.

Creating mosaics

We can create a mosaic by superimposing many images onto a surface at various positions. Instead of the -gravity switch with its rigid positions, we use the -geometry switch to specify locations on the image.

First we create a canvas to work on and colour it with the xc switch:

$ convert -size 200x200 xc:wheat multiple.png

Next, we place various images on the canvas, specifying the horizontal and vertical coordinates with the -geometry switch.


$ composite -geometry +15+30 1.gif multiple.png multiple.png
$ composite -geometry +19+20 2.gif multiple.png multiple.png
$ composite -geometry +12+28 3.gif multiple.png multiple.png

Since IM reads in all the input images before opening the output image, it can work on the same input image again and again without creating new images.

If an image exceeds the canvas dimensions it is cropped. To overcome this problem, use the -mosaic switch, which automatically creates a canvas large enough to hold all the images. The color of the canvas in this case is supplied by the -background switch.


$ convert -page +15+30 1.png -page +49+60 2.png
> -page +52+58 3.png
> -background wheat -mosaic mosaic.png

You can find more tricks on creating multi-image mosaics on the Web.

Next: Creating a logo

IM lets you draw shapes such as a rectangle, circle, ellipse, and polygon from the command line using convert with the -draw switch. Use the -fill switch to assign the color to be filled inside the shapes and the -stroke switch to define the boundary color.

To create a rectangle, you need to specify the upper-left and the lower right coordinates:


convert -size 120x60 xc:wheat -fill white
> -stroke black -draw "rectangle 25,10 95,50" rect.gif

You can also create a round-rectangle with curved edges. In this case, along with the upper left and the lower right coordinates you need to mention the width (20) and the height of the corners (12).


convert -size 120x60 xc:wheat -fill blue
> -stroke black -draw "roundrectangle 25,10 95,50 20,12" rrect.gif

An arc requires start and end points and the degrees of rotation (65,250):


convert -size 120x60 xc:blue -fill white -stroke black -draw "arc 90,4 40,50 65,250" arc_partial.png

To draw an ellipse centered at a given point (45,18):


convert -size 120x60 xc:magenta -fill white -stroke black -draw "ellipse 60,30 45,18 0,360" ellipse.png

A polygon requires at least three coordinates to define its boundaries:


convert -size 100x60 xc:skyblue -fill white -stroke black
> -draw "polygon 40,30 20,70 90,40 110,70" polygon.gif

Making 3D logos

Now that you know some of the building blocks, designing logos is easy. Make any shape, add some colors, write a word or two, and you have a logo. ImageMagick can even add shadows and lights to make your logo 3D.

The first step is to make a shape for your logo. Here we have a rectangle overlapping two circles to form a capsule-like shape:


convert -size 200x120 xc:black -fill white -draw 'circle 65,60 28,60'
> -draw 'circle 135,60 172,60' -draw "rectangle 65,23 135,97"
> -gaussian 1x1 +matte basic.png

The +matte switch tells IM to ignore the matte channel of the image, which is used to determine the transparency of pixels. I’ll explain the -gaussian switch in a moment.

We next add some color and text, which gives us a 2D logo.


convert basic.png -fill blue -draw 'color 0,0 reset'
> basic.png +matte -compose CopyOpacity -composite
> -font Helvetica -pointsize 20 -fill white
> -gravity center -annotate 0x0 "Blue Pill" 2D.png

In -draw 'color 0,0 reset', color is used to change the color of a pixel. There are different methods of changing the color, such as point, replace, floodfill, and reset. reset recolors all the pixels beginning with the specified coordinates.

The CopyOpacity option with the -compose switch copies the transparency channel of the source image onto the destination image. We add text to the image using the -annotate switch, along with -gravity to specify its position.

Now let’s add some 3D effects.

Blurring

Blurring is an integral part of making 3D images. If you use the shade effect without blurring the image first, you end up with heavily beveled images. By blurring the basic image first, you smoothe the beveled edges created by shade. Using blur repeatedly makes the image appear more rounded.


convert basic.png -blur 0x4 -blur 0x4 -blur 0x4 -blur 0x4
> +matte blurred.png

The -blur and the -gaussian switches have the same arguments: [radius]x[sigma]. The sigma value describes how much you want the image to spread (appear rounded) and the radius defines the area of operation for spreading pixels.

Shading

Next we want some lighting effects, to make it look as if the image is placed under a light. We do this twice to the blurred image, from the top and bottom, to define the dark and the bright areas.

convert blurred.png -shade 90x0 -normalize light.png
convert blurred.png -shade 90x180 -normalize dark.png

The -normalize switch is often used with -shade switch to enhance the contrast.

We now need to overlay these two images to form our shaded image. This next command uses new features of IM version 6, and won’t work with older versions.


convert ( light.png ( +clone -fx 'rand()' -threshold -1 )
> +swap +matte -compose CopyOpacity -composite )
> ( dark.png ( +clone -threshold 100% )
> +swap +matte -compose CopyOpacity -composite )
> -compose Over -composite high_lighting.png

IM permits you to work on images in isolation and introduce the results back into the command. This capability, called an image stack, is useful when you want certain operations to work only on some images in a sequence. An image stack is marked with parentheses preceded with backslashes to indicate that they are escaped, since Unix considers parentheses as special characters. The -threshold switch defines the opacity. The +swap switch swaps the last two images in the current image sequence.

We now have all the ingredients of a 3D logo. To add these to our 2D logo, we need to overlay them using the -compose switch, which is available from version 6 onwards:

convert 2D.png high_lighting.png -compose ATop -composite 3D.png

The ATop option ensures that only the parts that fall “above” the destination image get overlaid.

Now let’s put some finishing touches to it, like adding a background.


convert 3D.png ( +clone -fx 'rand()' +matte -shade 120x45
> -fill gray -fill beige -tint 120 )
> -insert 0 -flatten bg_3D.png

+clone is used to make a copy of the last image in the sequence; if you don’t use this switch, your final image would only show the background created with rand(). -insert takes the last image in the current image sequence and places it at the given index. In this case, it places our 3D logo into the final image.

Voilà! Your 3D logo is ready.

Conclusion

Playing with multiple images and 3D effects like lighting is no simple task even for a graphical image editor. While ImageMagick might take some getting used to, the mere fact that you can perform these operations from the command line speaks highly of this small package.