|
|
This article is available in: English Castellano Deutsch Francais Nederlands Portugues Russian Turkce |
by Katja Socher About the author: Katja is the German editor of LinuxFocus. She likes Tux, film & photography and the sea. Her homepage can be found here. Content: |
Abstract:
In this article we look at some spells the wizard of ImageMagick can do using a collection of graphic tools as his raw ingredients and the shell as his magic wand.
In the olden times wizards took their raw ingredients, mixed
them together in a big pot, swang their magic wand, murmured their
spells... and suddenly someone was turned into a frog. In our
modern times wizards are like everyone else in society highly
specialised and their spell books only contain a few useful spells
for very specific tasks. So ImageMagick is no spell book for
general use. In many aspects it can't compete with The Gimp or many
other graphic programs but it has some specialised features
that are very useful.
Its real strength lies in
the possibility to automate a lot of its processes when working
with it on the shell.
But before we are going to divulge some spells to you let's have a
quick look at the raw ingredients first:
or What is ImageMagick?
ImageMagick is a collection of graphic tools to work with images. The tools are display, import, animate, montage, convert, mogrify, identify and combine.convert -geometry 60x80 image.gif out.gifscales the image image.gif to a width of 60 and a height of 80 and writes the resulting image to out.gif.
#!/bin/sh for f in $* ;do convert -geometry 80x80 $f t_$f echo "<a href=\"$f\"><img src=\"t_$f\" width=\"80\" height=\"80\"></a>" done # end of script(Type this lines into any text editor of your choice (vi, emacs, nedit, kedit...) and save it under mksmallimage in your home directory. Then go to the bash shell and type
chmod 755 /home/katja/mksmallimage(use the name of your home directory instead of katja) Then you can use the script by typing /home/katja/mksmallimage xxx.jpg *.gif It converts all gif files plus the xxx.jpg file.)
display "vid:*.jpg"This will generate a visual image directory of all your jpg images in the current directory. Or:
display "vid:frog/*"will generate a visual image directory of all your images in your frog-directory.
<a href="file.gif"><img src="t_file.gif" width="60" height="80"></a>The original file here is file.gif and the thumbnail is t_file.gif.
for f in $* ;do convert -geometry 80x80 $f t_$f echo "<a href=\"$f\"><img src=\"t_$f\" width=\"80\" height=\"80\">" doneThe above script will loop over all images as specified on the command line, generate the thumbnails and write the html code to the screen. We can then copy and paste the html code into our webpage.
convert image.gif image.jpgConvert knows from the extension of the file name which format it has to use.
for f in $* ;do if echo "$f" | grep -i "jpg$" > /dev/null ; then gif=`echo "$f" | sed 's/jpg$/gif/i'` echo "converting $f to $gif ..." convert 80x80 $f $gif else echo echo "$f is not a jpg file, ignored" fi doneThere is also quite a large number of other formats that Image Magick knows as well.
combine -gravity SouthEast -compose Over img.jpg logo.gif stamp_img.jpgThe option "gravity SouthEast" puts the logo.gif in the lower right corner. "compose Over" says that we will replace the image by the logo in the places where they overlap.
identify image.jpg results in image.jpg 340x254 DirectClass 13939b JPEG 0.1u 0:01What can our wizard do with it? Well, to design good webpages that will display images while the page is still loading you should specify the exact geometry for all images. The html code would e.g look like this:
<img src="image.jpg" width="340" height="254" alt="[sample picture]">When our images have different sizes and we don't know the exact height and width of each of them we can use identify to help us. We write a shell script that will read the output of "identify" and then print this line. The geometry of the image is the second parameter in the output string of the identify program. To get this parameter we use the command awk:
identify image.jpg | awk '{print $2}' results in 340x254Now we have to split the geometry in width and height. This can be done with:
echo 340x254 | sed 's/[^0-9]/ /g' | awk '{print $1}'which would give the width. The height can be obtained with:
echo 340x254 | sed 's/[^0-9]/ /g' | awk '{print $2}'Don't focus too much on the exact shell commands. If you don't understand them completely yet then just accept them as given. There will be an article about shell programming in the next issue of LinuxFocus where we will explain all of its magic. The final shell script looks as follows:
file=$1 geometry=`identify $file | awk '{print $2}'` # geometry can be 563x144+0+0 or 75x98 # we need to get rid of the plus (+) and the x characters: width=`echo $geometry | sed 's/[^0-9]/ /g' | awk '{print $1}'` height=`echo $geometry | sed 's/[^0-9]/ /g' | awk '{print $2}'` echo "<img src=\"$file\" width=\"$width\" height=\"$height\">"To get the complete shell script we again add a help text and some error checking. Here is our final shell script, called imgsrcline :
While playing with ImageMagick I found that there is sometimes a discrepancy between the documentation and the actual functionality. Some features are also not very stable. However if you stick to the things that were demonstrated above then you will see that it is useful. The functions listed above do work. I used ImageMagick-4.2.9 , ImageMagick-5.2.9 and ImageMagick-5.3.0 and the things you learned here do work across all these versions.
I hope you got an idea now what you can do with ImageMagick and
will use the scripts or even start creating your own spells.
Have fun!
|
Webpages maintained by the LinuxFocus Editor team
© Katja Socher, FDL LinuxFocus.org Click here to report a fault or send a comment to LinuxFocus |
2001-08-03, generated by lfparser version 2.17