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.

Subscribe to RSS feed for this article!

41 Comments

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/).

~GreyCells

(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
http://blog.shevin.info/2007/05/how-to-add-watermark-to-300-photos-in.html

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:
http://tech.shantanugoel.com/2008/04/26/imagemagick-weaving-magic-with-your-pictures.html

(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
0.

(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)
 
fold this thread comment maigrir Rapidement  Friday, 25 July 2014 o godz. 4:06 pm #  Add karma Subtract karma  +0

Wonderful site you have here but I was curious if you knew of
any forums that cover the same topics talked about here?
I’d really love to be a part of community where I can get feed-back
from other experienced people that share the same interest.
If you have any suggestions, please let me know.
Thanks a lot!

(Comments wont nest below this level)
 
fold this thread Moukhtar Abliazov  Thursday, 31 July 2014 o godz. 10:48 pm #  Add karma Subtract karma  +0

Usually I do not learn post on blogs, however I would like to say that this write-up very forced me to take a look at and do
it! Your writing taste has been amazed me. Thanks, quite nice article.

(Comments wont nest below this level)
 
fold this thread avis voyance dany rennes  Friday, 1 August 2014 o godz. 2:48 pm #  Add karma Subtract karma  +0

I was wondering if you ever thought of changing the layout of
your blog? Its very well written; I love what youve got to say.
But maybe you could a little more in the way of content so
people could connect with it better. Youve got an awful lot of text for only having
1 or two images. Maybe you could space it out better?

(Comments wont nest below this level)
 
fold this thread buy nail  Friday, 1 August 2014 o godz. 6:05 pm #  Add karma Subtract karma  +0

Amazing! Its actually amazing paragraph, I have got much clear idea about from this paragraph.

(Comments wont nest below this level)
 
fold this thread homepage  Monday, 4 August 2014 o godz. 4:13 am #  Add karma Subtract karma  +0

An impressive share! I’ve just forwarded this onto a co-worker
who has been conducting a little resarch on this. And he in fact ordered me breakfast simply because I stumbled upon it for him…
lol. So alpow me too reword this…. Thank YOU for the meal!!
But yeah, thanx forr spending some time to talk
about this topic here on your web site.

(Comments wont nest below this level)
 
fold this thread best running shoes  Tuesday, 5 August 2014 o godz. 3:00 pm #  Add karma Subtract karma  +0

Wow, incredible blog layout! How long have you been blogging for?
you made blogging look easy. The overall look of your site is fantastic,
as well as the content!

(Comments wont nest below this level)
 
fold this thread www.youtube.com  Wednesday, 6 August 2014 o godz. 10:54 am #  Add karma Subtract karma  +0

Hi there to all, how is all, I think every one is getting more from this web page, and your views are fastidious in favor of new visitors.

(Comments wont nest below this level)
 
fold this thread Brigitte  Thursday, 7 August 2014 o godz. 12:22 pm #  Add karma Subtract karma  +0

Even celebrity names can be monetized through Amazon as you can include
your Amazon affiliate links to their work whether that be
a movie or television show on DVD, or some of their records.
However, once you choose to do this, make sure that you have
the right content and necessary applications to avoid wasting the time of the user.
When I get asked what is the quickest way to make money online I start to sigh.

(Comments wont nest below this level)
 
fold this thread http://oldschoolnewbodyfree2014.blogspot.com  Friday, 8 August 2014 o godz. 1:00 am #  Add karma Subtract karma  +0

If you are someone who sleeps on their back, you will want
to switch to your sides. Cue the rubber tipped poles from Exerstrider and
other suppliers of exercise walking poles. Also, all the origin or ingredients
that you’ll require for home made remedies can end up being easily available on grocery shop or any kind of nearby grocery store.

(Comments wont nest below this level)
 
fold this thread Télécharger Avis de mistral Gratuit  Saturday, 9 August 2014 o godz. 1:58 am #  Add karma Subtract karma  +0

I feel this is among the most significant information for me.
And i am happy reading your article. However wanna observation on few basic issues, The website style is perfect, the
articles is in reality nice : D. Good process, cheers

(Comments wont nest below this level)
 
fold this thread slidingwindowsalamedacounty.blog.cz  Friday, 15 August 2014 o godz. 5:23 am #  Add karma Subtract karma  +0

Thank you a bunch for sharing this with all people you
actually realize what you’re talking about! Bookmarked.
Please also visit my website =). We can have a link exchange
contract between us

(Comments wont nest below this level)
 
fold this thread boom beach hack tool  Sunday, 17 August 2014 o godz. 2:54 am #  Add karma Subtract karma  +0

Phoenix moved there from the Ice Cream shop and we followed, but it also had Asteroids as well as Pac Man Jr.
The internet, in the past few hours, has become littered with people speaking out about the episode.
Alice has compared her curvy body shape to that of this blonde bombshell who definitely marches to the
beat of her own drummer – Scarlett has covered Tom Waits songs and dated Sean Penn.

(Comments wont nest below this level)
 
fold this thread http://www.amazon.com/iPool-Above-Ground-Exercise-Swimming/dp/B00126QCOO  Sunday, 17 August 2014 o godz. 4:05 am #  Add karma Subtract karma  +0

Hi colleagues,how is the whole thing, and what you wish for to say about
this pijece of writing, in my view its really amazing in favor of me.

(Comments wont nest below this level)
 
fold this thread log cabin decor.lake cabin decor  Sunday, 17 August 2014 o godz. 6:08 pm #  Add karma Subtract karma  +0

Hi there to all, how is the whole thing,
I think every one is getting more from this web page,
and your views are pleasant designed for new visitors.

(Comments wont nest below this level)
 
fold this thread Alan Trider  Monday, 18 August 2014 o godz. 2:16 pm #  Add karma Subtract karma  +0

All assets if they are in real-property, sector or any another look at french Polynesian overall
economy needs the endorsement and the authorization using the government.
If you get true-property by way of dealer, you could possibly wind up confident that
one is inside of outstanding palms. Just like renting other designs of real estate home, the renter
can protected the place less than fasten and crucial.

(Comments wont nest below this level)
 
fold this thread comet Busters!  Tuesday, 19 August 2014 o godz. 12:22 am #  Add karma Subtract karma  +0

I like the helpful information you provide in your
articles. I’ll bookmark your blog and check again here regularly.

I’m quite sure I’ll learn a lot of new stuff right here!
Best of luck for the next!

(Comments wont nest below this level)
 
fold this thread Chasity  Sunday, 24 August 2014 o godz. 2:59 pm #  Add karma Subtract karma  +0

First of all I would like to say wonderful blog! I had a quick question which I’d like
to ask if you don’t mind. I was interested to know how you center
yourself and clear your head prior to writing.
I’ve had trouble clearing my thoughts in getting my thoughts out there.
I do enjoy writing however it just seems like the first 10 to
15 minutes are lost just trying to figure out how to begin.
Any recommendations or hints? Thanks!

(Comments wont nest below this level)
 
fold this thread Alycia  Sunday, 24 August 2014 o godz. 9:28 pm #  Add karma Subtract karma  +0

Thank you for any other informative website. The place else may just I get that kind of info
written in such an ideal approach? I’ve a mission that I am simply now operating on, and I’ve been at the glance out for such information.

(Comments wont nest below this level)
 
fold this thread wellnessdevelop.com  Monday, 25 August 2014 o godz. 2:22 pm #  Add karma Subtract karma  +0

Hi there mates, fastidious article and good urging commented at
this place, I am really enjoying by these.

(Comments wont nest below this level)
 
fold this thread male performance enhancers  Sunday, 7 September 2014 o godz. 9:33 am #  Add karma Subtract karma  +0

It’s actually very complicated in this active life to listen news
on Television, therefore I simply use web for that purpose, and take the most up-to-date news.

(Comments wont nest below this level)
 
fold this thread aimbot mw3 ps3  Tuesday, 9 September 2014 o godz. 5:19 pm #  Add karma Subtract karma  +0

I’m really enjoying the design and layout of your blog. It’s
a very easy on the eyes which makes it much more pleasant
for me to come here and visit more often. Did you hire out a developer to create your theme?

Fantastic work!

(Comments wont nest below this level)
 
fold this thread LiveJasmin Credits Hack  Thursday, 11 September 2014 o godz. 11:20 pm #  Add karma Subtract karma  +0

Hi there! Ӏ simply want to offer you а huge thumƅs up for your
great info yoս’ve got right here on this post. I’ll be coming back to your site for
more soon.

(Comments wont nest below this level)
 
fold this thread http://rattandiningsetsbyportroyal.pen.io/  Sunday, 14 September 2014 o godz. 11:41 am #  Add karma Subtract karma  +0

Fantastic page, really been looking forever and a day for ideas on the perfect rattan furniture for our home and in our patio.
The site sincerely helpedgreat blog some great info here

(Comments wont nest below this level)
 
fold this thread onip.it  Monday, 15 September 2014 o godz. 7:34 pm #  Add karma Subtract karma  +0

Hey there! Quick question that’s completely off topic.

Do you knolw how too make your siite mobile friendly?
My web site looks weird when viewing from my iphone4.
I’m trying to find a theme or plugin that might be able too resolve this issue.
If you have any recommendations, please share.
Thank you!

(Comments wont nest below this level)
 
fold this thread avocats sur mesure en ligne  Wednesday, 17 September 2014 o godz. 1:07 pm #  Add karma Subtract karma  +0

Hi! This is my first visit to your blog!
We are a group of volunteers and starting a new initiative in a
community in the same niche. Your blog provided us useful information to work on. You have
done a outstanding job!

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

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