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):
[code lang=”text”]convert flower.jpg -font courier -fill white -pointsize 20 -annotate +50+50 ‘Flower’ flower_annotate1.jpg[/code]
Produces:
Example (text with background at bottom):
[code lang=”text”]convert flower.jpg -fill white -box ‘#00770080’ -gravity South -pointsize 20 -annotate +0+5 ‘ Flower ‘ flower_annotate2.jpg[/code]
Produces:
Look at these examples to see more.
2. Cropping an image
Example:
[code lang=”text”]convert flower.jpg -crop 128×128+50+50 flower_crop.jpg[/code]
Produces:
Look at these examples or -crop for more information.
3. Rotate an image
Example:
[code lang=”text”]convert flower.jpg -rotate 45 flower_rotate45.jpg[/code]
Produces:
Look at these examples or -rotate for more information.
4. Image montage
Example:
[code lang=”text”]montage flower.jpg flower_unsharp.jpg -geometry ‘300×200+20+20’ flower_montage.jpg[/code]
Produces:
Look at these examples to see more.
5. Animation
Example:
[code lang=”text”]
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
[/code]
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]imagemagick, photo, linux[/tags]
Hmmm, I always used “mogrify” for resizing images.
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 :)
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.
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.
Pingback: 5 ImageMagick command line examples part 2 @ IONCANNON
why use the command mode….why not GIMP!!!
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
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.
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…
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
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.
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…
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/
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!