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 128x128+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 '300x200+20+20' flower_montage.jpg

Produces:

Look at these examples to see more.

5. Animation

Example:

convert flower.jpg -resize 100x100 -font courier -fill white -pointsize 20 -annotate +50+50 'Frame 1' flower_frame1.gif
convert flower.jpg -resize 100x100 -font courier -fill white -pointsize 20 -annotate +50+50 'Frame 2' flower_frame2.gif
convert flower.jpg -resize 100x100 -font courier -fill white -pointsize 20 -annotate +50+50 'Frame 3' flower_frame3.gif
convert flower.jpg -resize 100x100 -font courier -fill white -pointsize 20 -annotate +50+50 'Frame 4' flower_frame4.gif
convert -delay 100 -size 100x100 \
   -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: , ,

14 thoughts on “5 ImageMagick command line examples – part 1

  1. Ganesh

    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 :)

  2. dajomu

    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.

  3. Pingback: 5 ImageMagick command line examples part 2 @ IONCANNON

  4. dhruv

    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

  5. carson Post author

    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.

  6. Eric

    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…

  7. wander

    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

  8. wikifun

    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.

  9. Armin Theissen

    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…

  10. Kathy

    I have an issue with a PDF being optimized. what I'm doing right now is
    exec("/usr/bin/convert uploads/$newName.pdf -resize 200×200 uploads/$newName.jpg"); it works for all PDF's that are not optimized. How can I have this work for them too!

Leave a Reply

Your email address will not be published. Required fields are marked *