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.


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% \\
convert picture_file.jpg -resize 640x480 \\

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!” \\
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.

for i in *jpg
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"'\\

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

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


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.

Subscribe to RSS feed for this article!


fold this thread Anonymous  Friday, 11 May 2007 o godz. 6:49 am #  Add karma Subtract karma  +0

I cut and pasted your automated script file and it didn’t work. After trying different stuff for a long, long while, and after tons of aggravation, I realized that you need to put all the options on the same line. Yes, one big line with only one carriage return at the end. You might want to change this and save people some headache. :-)

I have been wondering how do for-next in a script file, and now I know. Thanks.

(Comments wont nest below this level)
fold this thread michuk  Friday, 11 May 2007 o godz. 8:57 am #  Add karma Subtract karma  +0

@Anonymous: Thanks. It was stupid WordPress ignoring the slash sign (you need to escape it to make it display). Fixed.

(Comments wont nest below this level)
fold this thread GreyCells  Friday, 11 May 2007 o godz. 9:59 am #  Add karma Subtract karma  +0

It appears the script overwrites the original image. There needs to be a warning to that effect, so inexperienced users do not unintentionally overwrite their master files. Alternatively, -write the new file(s) to a sub directory (web/).


(Comments wont nest below this level)
fold this thread Joe  Friday, 11 May 2007 o godz. 7:39 pm #  Add karma Subtract karma  +0

Or just use convert, and save to $filename-new.$ext

(Comments wont nest below this level)
fold this thread shewdiz  Friday, 25 May 2007 o godz. 6:15 pm #  Add karma Subtract karma  +0

hey I had written a simmilar article 2 months ago

I learnt from your blog, I hope you learn things from mine too :)

(Comments wont nest below this level)
fold this thread Shantanu Goel  Wednesday, 30 April 2008 o godz. 7:58 pm #  Add karma Subtract karma  +0

imagemagick is so cool. I wrote a small overview about it plus a couple of scripts to create a polaroid like website header using imagemagick here:

(Comments wont nest below this level)
fold this thread Anthony Wilson  Friday, 7 May 2010 o godz. 2:13 am #  Add karma Subtract karma  --1

my PPC advertising on Google Adwords is really effective in promoting my affiliate links. my Adbrite PPC advertising cost seems to be higher than Adwords.:;*

(Comments wont nest below this level)
fold this thread Jayden Thomas  Friday, 14 May 2010 o godz. 10:44 am #  Add karma Subtract karma  --1

I use both Adsense and Adbrite and i can say that adbrite is also a good source of income but not as good as adsense.~’~

(Comments wont nest below this level)
fold this thread Carter Ward  Tuesday, 5 October 2010 o godz. 8:38 pm #  Add karma Subtract karma  --1

PPC advertising is very popular these days because of Google”;-

(Comments wont nest below this level)
fold this thread Tea Dress %0A  Saturday, 4 December 2010 o godz. 8:08 am #  Add karma Subtract karma  --1

i do not use adbrite because their pay rate is horrendously low compared to say infolinks .:`

(Comments wont nest below this level)
fold this thread qclmoudz  Tuesday, 16 October 2012 o godz. 12:12 pm #  Add karma Subtract karma  +0

How about if your computer is running just fine, and you have Windows XP chugging along just fine. [url=http://www.windowskeycodes.com]win 7 ultimate key[/url] The adjustment port is put in place underneath the skin at the top of the abdomen. windows 7 ultimate 32 bit key

(Comments wont nest below this level)
fold this thread マタニティ 服 激安  Wednesday, 4 December 2013 o godz. 12:39 pm #  Add karma Subtract karma  +0

アウトレットで探すのも手ですよ。 マタニティウエアおしゃれ アウトレットで探すのも手ですよ。

(Comments wont nest below this level)
fold this thread naturalsupplements  Saturday, 14 December 2013 o godz. 11:20 pm #  Add karma Subtract karma  +0

Ӏn order for muscles to show, іncluding abdominal muscles, theу must be exercise formula 1 nutritional shake mmіx That means if the only
hanges yyou made to уourr daily diet was to cuut out your bolttle of coke and cakme you would lose over

(Comments wont nest below this level)
fold this thread Esteban  Sunday, 15 December 2013 o godz. 1:13 pm #  Add karma Subtract karma  +0

If you follоw these tips, you can easily lose up to 4 lbs of ωeight withіn nine dayѕ herbal
aloe cоncentrate Go foг a walk to make yourself feel better
rrather thqn grabbing a bowl off ice cream.

(Comments wont nest below this level)
fold this thread cherylhorseman.webs.com  Sunday, 15 December 2013 o godz. 3:26 pm #  Add karma Subtract karma  +0

Contrarу tο populaг belief, thwгe is no federal law
that assists workers who suffеr employmеnt discriminatiοn based on their
sizes herbalife formula 1 shake The countries listed brlow shiw
their most distinctive techniqueѕ that make eatіng such a delightful way
to leaad a good life.

(Comments wont nest below this level)
fold this thread uberstrike clash of clans hack 2014  Monday, 23 June 2014 o godz. 8:10 am #  Add karma Subtract karma  +0

Hi, I think your site might be having browser compatibility issues.

When I look att yoyr blog in Opera, it looks fine
but when opening in Internet Explorer, it has some overlapping.
I just wanted to give you a quick heads up! Other
then that, superb blog!

(Comments wont nest below this level)
Name (required)
E-mail (required - never shown publicly)

Adjust field size: shrink | enlarge)

You can use simple HTML in your comments. Some examples are as follows:
  • A hyperlink: <a href="polishlinux.org">GNU/Linux for everyone!</a>,
  • Strong text: <strong>Strong text</strong>,
  • Italic text: <em>italic text</em>,
  • Strike: <strike>strike</strike>,
  • Code: <code>printf("hello world");</code>,
  • Block quote: <blockquote>Block quote</blockquote>

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