5 ImageMagick command line examples – part 1

If you have ever wanted to manipulate images under linux you probably have used Gimp. This isn't your only option and if you want to do things from the command line a better option is to use ImageMagick's convert utility.

I've put together 5 simple command line examples that I have found useful. This is just a sample of what you can do with convert. To see more examples and get more explanation of options see: ImageMagick v6 Examples.

I started with the following image as a base for all the examples that follow:

1. Text annotations

Example (simple text in static location):

convert flower.jpg -font courier -fill white -pointsize 20 -annotate +50+50 'Flower' flower_annotate1.jpg

Produces:

Example (text with background at bottom):

convert flower.jpg -fill white -box '#00770080' -gravity South -pointsize 20 -annotate +0+5 '   Flower  ' flower_annotate2.jpg

Produces:

Look at these examples to see more.

2. Cropping an image

Example:

convert flower.jpg -crop 128×128+50+50 flower_crop.jpg

Produces:

Look at these examples or -crop for more information.

3. Rotate an image

Example:

convert flower.jpg -rotate 45 flower_rotate45.jpg

Produces:

Look at these examples or -rotate for more information.

4. Image montage

Example:

montage flower.jpg flower_unsharp.jpg -geometry '300×200+20+20' flower_montage.jpg

Produces:

Look at these examples to see more.

5. Animation

Example:

convert flower.jpg -resize 100×100 -font courier -fill white -pointsize 20 -annotate +50+50 'Frame 1' flower_frame1.gif
convert flower.jpg -resize 100×100 -font courier -fill white -pointsize 20 -annotate +50+50 'Frame 2' flower_frame2.gif
convert flower.jpg -resize 100×100 -font courier -fill white -pointsize 20 -annotate +50+50 'Frame 3' flower_frame3.gif
convert flower.jpg -resize 100×100 -font courier -fill white -pointsize 20 -annotate +50+50 'Frame 4' flower_frame4.gif
convert -delay 100 -size 100×100 \
   -page +0+0 flower_frame1.gif \
   -page +0+0 flower_frame2.gif \
   -page +0+0 flower_frame3.gif \
   -page +0+0 flower_frame4.gif \
   -loop 0 flower_animation.gif

Produces:

Look at these examples, these examples, or these examples for more information.

Summary

That is it for now. Part 2 should be up soon.

Tags: , ,

This entry was posted in linux, utilities. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

12 Comments

  1. Posted July 15, 2006 at 12:45 pm | Permalink

    Hmmm, I always used "mogrify" for resizing images.

  2. Posted July 17, 2006 at 4:02 am | Permalink

    Interesting tutorial and nice examples! Looking forward to part 2…

    @Tony: If i am not mistaken, mogrify is indeed a part of the imagemagick suite :)

  3. Posted July 19, 2006 at 3:11 am | Permalink

    How can I optimize a jpg file? keep the aspect ratio i.e. 800×600 but reduce the file size only.
    Microsoft has a image resize utility for this, but I would like to use Linux and the CLI if possible.

  4. Posted July 19, 2006 at 9:15 am | Permalink

    You can use mogrify http://www.imagemagick.org/script/mogrify.php to resize and then use the -quality flag to change to reduce the size. I'll have the 2nd set of examples up soon and go into it more.

  5. Ankit
    Posted September 25, 2006 at 7:15 am | Permalink

    why use the command mode….why not GIMP!!!

  6. dhruv
    Posted September 27, 2006 at 6:45 am | Permalink

    why use the gimp at all in the first place?? we got better options like photoshop and photomagik at our disposal… moreover using linux has proven to be a pain this far

  7. Posted September 27, 2006 at 8:32 am | Permalink

    If you are comfortable putting gimp or photoshop in a script then I would think that would be fine. A lot of people need to be able to script or otherwise call image manipulation programs to do their hard work.

  8. Eric
    Posted June 17, 2007 at 10:02 am | Permalink

    If you need to do LOTS of image conversions you need a script. For example, right I have 1705 GIF screenshots (pages of a book) which I want to optimize and convert to JPEG so I can then put it a single DJVU file (like PDF but more efficient). Try converting 1705 files without a script…

  9. wander
    Posted June 22, 2007 at 8:51 pm | Permalink

    if I have 2 images of the same size and I need to sum each pixel like this.

    image 1 pixel i r=50 g=30 b=100
    image 2 pixel i r=150 g=90 b=10

    how I can get a result image like this:
    image pixel i r=200 g=120 b=110

  10. wikifun
    Posted September 16, 2007 at 7:51 pm | Permalink

    hi. Thanks for this. I was looking for something like that. However when I tried some of the scripts linux came back with an error. For example, the first two commands worked. But it did not accept 128×128+50+50 Everytime I wrote a command that had that or anything more than the 50+50 it gave me an error. Any idea what the reason for this is?
    Thanks.

  11. Armin Theissen
    Posted December 29, 2007 at 9:41 am | Permalink

    Hi – I wanted to rotate an image by 90 degrees from the
    command line, but 'convert -rotate' will decrease the
    image quality a bit. Any alternative?
    The reason for the quality loss is probably due to some
    real calculations (sines, cosines) in a transform matrix, but
    for 90/180/270 deg rotations you would just need to
    swap pixels…

  12. Posted January 9, 2009 at 11:00 pm | Permalink

    The 'animation' link is wrong, and has been for a long time. Animation in IM examples is now in three sections (there is a lot of info and techniques). I suggest you visit the primary source site for more detail, and techniques about any of these examples http://www.imagemagick.org/Usage/

One Trackback

  1. [...] I've put together another 5 ImageMagick command line examples as a followup to part 1. These examples are a little more advanced and include some extra information on techniques. [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>