Enchanting Pictures with ImageMagick

[ Wednesday, 9 May 2007, TLu ]


Since digital still cameras hit mainstream, we’ve all become flooded with hundreds of photos. Most of them require some sort of post-processing before they can be published online. Even a simple task such as scaling a holiday picture and applying a signature can be a tedious task, often taking up hours, especially when one has hundreds of photos. Now try to do even more, for example normalize the colors, brightness and contrast… and you can find yourself going on another holiday before publishing pictures from the previous one.

Author: Tomasz Łuczak

Luckily there are some free magicians available, who can answer our call for help and automate all above mentioned processes causing them take minutes instead of days. In this extra time, you can do something more pleasant, such as… more pictures.

Having said this, I’d like to proudly present ImageMagick – a set of tools for creating and processing bitmap images. This article explains how ImageMagick can help you perform many image-manipulation tasks easily and quickly using… the console. Yes, the same black-box hackers use to do nasty stuff :)

What can one do using ImageMagick?

Just like every respectable magician, ImageMagick can

  • convert formats into other formats,
  • transform (change size, crop, rotate),
  • write and draw shapes on the picture,
  • decorate (for example framing),
  • add special effects (blur, sharpening)
  • or finally edit and combine pictures.

The program can also take screenshots and, of course display images.

Transformations

We can use two programs from the ImageMagick package to transform stuff – convert and mogrify. They are almost identical from the user’s point of view, but convert saves the result image into another file, while mogrify does all the modifications on the original, overwriting it.

The most popular task is scaling pictures, making those millions of pixels into hundreds, a size accepted widely on the web.

You can do this the following way:

convert picture_file.tiff -resize 40% \\
picture_file_smaller.jpg
convert picture_file.jpg -resize 640x480 \\
picture_file_resized.jpg

As you can see, the -resize option accepts relative values (as shown on the first example) and absolute values as well. When you use the latter method, the image will be resized with proportions kept.

Notice, that in the first example, besides shrinking the image to only 40%, we converted it to another format simply by specifying a proper extension.

We can also change sizes with other options:

  • -geometry X – preferred image size, or
  • -geometry XxY {+ -} dX {+ -} dY – preferred image size XxY, shifted relatively to (0, 0) by +/- dX and +/- dY units, such as in the example below:
mogrify -geometry 800 some_photo.jpg

We can also rotate images, like on the following example:

mogrify -rotate 90 picture.jpg

The argument for -rotate is of course the number of degrees we wish to rotate, clockwise.

Flips can also be performed, just as in every fair image-manipulation program. We can flip horizontally (flop) and vertically (flip). Just like this:

convert some_image.jpg -flop some_image_flipped_horizontally.jpg

Cropping images can be also performed, without the need to use a full-fledged graphics program. We do this like so:

convert photo.jpg -crop 100x100+10+10 cropped_photo.jpg

The first two arguments are dimensions of the cropping frame, and another two are offsets from the top-left corner of the image. Offset values can also be negative.

Decorating things

A pretty picture usually needs a proper frame. We can make one issuing the following commands:

convert photo.jpg -bordercolor white -border 10x20 framed_photo.jpg
mogrify -bordercolor „#ddddff” -border 10 framed_photo.jpg

The frame is added to the picture, and if it has the same width on every side, we only need to specify one border size – as shown in the second example. We can also specify different sizes for different sides, like in the first example.

Colors can be specified in a few ways – by name, by RGB value, or CMYK value — to name but a few.

Special effects

Another typical task which is often performed on pictures taken straight out of the camera, is normalizing the colors. It’s a simple task thanks to ImageMagick. The unnatural, pale skin colors instantly become more vivid :)

convert photo.jpg -normalize prettier_photo.jpg

Sometimes also contrast adjustment is needed. ImageMagick also supports this. All you need to do is use the -contrast option:

convert photo.jpg -contrast photo_with_reduced_contrast.jpg
convert photo.jpg +contrast photo_with_increased_contrast.jpg
convert photo.jpg +contrast +contrast photo_with_high_contrast.jpg

Obviously, the many times you add the +contrast switch, the more contrast you get on your photo. If you want to make some imperfections less visible, you can use blur:

convert photo.jpg -blur 3x5 blurred_photo.jpg
convert photo.jpg -gaussian 2x4 blurred_photo.jpg
convert photo.jpg -gaussian 3 blurred_photo.jpg

Both options take two parameters: radius and blurring amount. If we don’t specify numbers, ImageMagick will attempt to guess the best value. The ways in which the program calculates the necessary radius and amount are specified in the official documentation.

Changing sharpness of a photo can be done such as this:

convert photo.jpg -sharpen 2x4 sharpened_photo.jpg

The arguments taken by the -sharpen option are radius and standard sigma deviation.

When a picture is too “noisy”, such as from using a high ISO, we can also use ImageMagick to deal with the problem:

convert photo.jpg -noise 2 less_noisy_photo.jpg

The +noise parameters define the radius. We can also apply noise, to make a photo look more “dramatic”:

convert photo.jpg -noise Gaussian noisy_photo.jpg

When using a parameter for the +noise option, like ‘Gaussian’ here, we can specify additional noise methods: uniform, multiplicative, impulse, laplacian, poisson.

To invert an image, in order to create a negative, we can invoke the following:

convert negative.jpg -negate positive.jpg

Drawing and writing

We can also draw on pictures or write notes. It’s not that complicated, but let’s just stick to writing for now. Let’s say we want the author’s signature on the photo.

convert photo.jpg -draw „text 50,50 it is me!” signed_photo.jpg
convert photo.jpg -gravity Southeast -draw „text 50,50 it is me!” \\
 signed_photo.jpg
convert photo.jpg -fill white -font -*-helvetica-*-r-*-*-18-*-*-*-*-*-*-2 //
-gravity Southeast -draw 'text 50,50 „it is me”!' signed_photo.jpg

Parameters for the -draw option should be passed in double quotes or single quotes; the first element is the keyword which specifies what we’re going to draw (such as text), coordinates for the object and the text itself.

The -gravity option can take options which mean directions, like North, East, etc. and they specify where the object will be placed.

The -fill parameter defines the font color. This also applies when we want to draw shapes.

The font itself can also be altered, here it’s helvetica.

As shown on the last example, many tasks can be combined into one ImageMagick invocation. Keep in mind though, that it’s important in what order they are applied!

Not just the console

We can take screenshots, by invoking the following commands:

import screenshot.png
import -frame screenshot.png

In the second example the captured window will have a decoration, added by the window manager. When launching ‘import’, the cursor will become a cross, and you can draw a frame on the screen. Everything in that frame will be saved. Apart from modifying the pictures in console mode, we can also view them and use the mouse to modify them. The ‘display’ command is useful to do so.

Automating tasks

If you really want to save time during those long winter nights, I give you my version of script which can perform the following modifications (yes, it overwrites the files, so be careful!) on all jpg files found in current directory: normalize colors, resize so that the largest side has 600 pixels at best, add a yellow frame 5 pixels wide, and add the author’s name in white helvetica on the bottom-right corner.

#!/bin/sh
for i in *jpg
do
echo $i
mogrify -normalize \\
-geometry 600 \\
-bordercolor yellow -border 5 \\
-fill white \\
-font \\
-*-helvetica-*-r-*-*-18-*-*-*-*-*-*-2 \\
-gravity SouthEast \\
-draw 'text 20,20 "photographer: me"'\\
$i
done

Now save the script a text file (e.g. automate.sh), grant the execure permission and finally run the script in a selected folder:

cd folder_with_saved_script
chmod u+x automate.sh
cd folder_with_images
./automate.sh

Of course you should modify the script according to your needs, this is an example only.

Conclusion

The possibilities which have been described in this article are just a fraction of what can actually be done with ImageMagick. I’d like to encourage you to get to know these tools, as they are included in almost every Linux distribution. ImageMagick is not only a powerful set of tools; it also has interfaces to many programming languages, such as Perl, Python, Tcl/Tk, Ruby, PHP, java, C and C++, which allows to use its potential in software development. Pretty good documentation is available on the project’s homepage: www.imagemagick.org

Creative Commons License
This work is licensed under a Creative Commons License.

This article has been first published in Dragonia Magazine, a Polish online magazine about Free and Open-Source Software. You can download the latest Dragonia issue (first one in English from our mirror). The article has been slightly modified compared with the original version by the PolishLinux team.

About the Author

Tomasz Łuczak

TeaM-TL developer, Dragonia Magazine's editor, TeX and Tcl/Tk enthusiast. Lives in Katowice, Poland.

New AdTaily ads!

Are you a film buff?

film buffs community, movie recommendations and reviews

RSS: Comments

You can follow the comments to this article through a special channel RSS 2.0 .

Related articles: Graphics

 more »

PolishLinux Top Content


Featured links

Once you get aware of the internet marketing techniques, it becomes easier for you to survive and compete the marketing, for this you need to know few techniques like ppc advertising and ppc management.

Become our fan on Facebook!

PolishLinux.org on Facebook

Follow PolishLinux on Twitter!

Follow polishlinux on Twitter

Google Ads