Fri 14 Jul 2006
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):
Produces:

Example (text with background at bottom):
Produces:

Look at these examples to see more.
2. Cropping an image
Example:
Produces:

Look at these examples or -crop for more information.
3. Rotate an image
Example:
Produces:

Look at these examples or -rotate for more information.
4. Image montage
Example:
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 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 or these examples for more information.
Summary
That is it for now. Part 2 should be up soon.
Tags: imagemagick, photo, linux


















July 15th, 2006 at 12:45 pm
Hmmm, I always used "mogrify" for resizing images.
July 17th, 2006 at 4:02 am
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
July 19th, 2006 at 3:11 am
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.
July 19th, 2006 at 9:15 am
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.
July 22nd, 2006 at 8:44 am
[...] 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. [...]
September 25th, 2006 at 7:15 am
why use the command mode….why not GIMP!!!
September 27th, 2006 at 6:45 am
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
September 27th, 2006 at 8:32 am
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.
June 17th, 2007 at 10:02 am
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…
June 22nd, 2007 at 8:51 pm
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
September 16th, 2007 at 7:51 pm
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.
December 29th, 2007 at 9:41 am
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…