2002 11 Graphic Scripting Save Time and Effort with Simple Scripting


KNOW HOW Graphic Scripting
Netpbm Tools and Shell Scripts
Animation on Demand
Editing images does not mean you interim formats. ppmtopgm converts
images to greyscales, pnmsmooth applies
will automatically need a mouse.
a soft focus effect and pgmnorm is used
for normalizing greyscales.
The filters included in the Netpbm
Source Material
package and similar tools can be used
Of course, raw material is required for
command-line based image processing.
in shell scripts to automate various
But your computer can also take care of
this task using the freeware raytracer,
steps. BY CHRISTIAN PERLE
Povray. The scene description file
glass.pov from Listing 1 causes the
program to trace an image with five glass
balls on a checkered background. You
can use the clock variable to move the
ixel based graphic formats have background and in turn produce a
been around for some years. simple animation.
PConsequently, there has been a Now let s feed this to Povray version
similar demand for suitable conversion 3.0 or 3.1. We want the program to
programs. If you want tackle this create a 320 by 240 pixel image using
problem Unix style, that is using individ- anti-aliasing, and creating output in
ual filter programs for the command line, step by step to the collection now known PPM format:
you will need to write exactly (n-1)*n as netpbm. Since 2000 the Netpbm
filters for n graphic formats. If you project has seen a return to more active povray +i glass.pov +w320 +h240U
decide to use an interim format instead, development and this is now hosted by +a0.1 +fp +v
you will only need n filters to convert the Sourceforge project.
various graphic formats into the interim Figure 1 shows the results as stored in
Bit, Grey or Pix?
format and another n to convert the the glass.ppm file.
interim format back to an original To be more exact, Netpbm does not offer
Filters in Chains
graphic format. a single interim format but three:
Jef Poskanzer started working on  Portable Bitmap (PBM),  Portable The following steps show how to use
pbmtools in 1989 with this in mind. Greymap (PGM) and  Portable Pixmap filter commands to modify an image.
Up until 1994 new formats and effect (PPM). The PBM format recognizes only The following command converts the
filters for the interim format were added occupied (black) and vacant (white) image to greyscale:
pixels and thus requires one bit per
pixel. The PGM format can store only ppmtopgm glass.ppm > glass.pgm
GLOSSARY
greyscales and will normally require
Sourceforge: A Web service for Open Source
eight bits per pixel (256 greyscales). The Just like the filters in the Netpbm
projects including developer forums, version
control, download areas, and various other PPM format requires 24 bits per pixel
resources at http://sourceforge.net/.
(eight bits each for the base colors red,
green, and blue), allowing 16.7 million
Povray: A freeware raytracing (3D graphics)
program that runs on various operating colors ( true color ).  Portable Anymap
systems  such as Linux.The Povray home- (PNM) refers to any interim format.
page is available at http://www.povray.org/;
tgatoppm, giftopnm, or g3topbm are
the subscription CD includes a tutorial in
examples of the filters used to convert
HTML format.
external formats to the interim format.
Anti-aliasing: Automatic smoothing of lines
ppmtogif, pnmtotiff, or pnmtops are
in high contrast images. Prevents an image
examples of formats for the opposite
from appearing  over-pixeled and simulates
direction. Additionally, there are some
a higher resolution.
formats that are applied only to the Figure 1: Glass balls as a test image
44 November 2002 www.linux-magazine.com
Graphic Scripting KNOW HOW
Listing 1: glass.pov
01 // Glass ball animation 20 40 pigment { rgbf<1, 0.7,
02 // (C) 11/2002 Christian Perle 21 // Five colored glass balls 1, 0.7> }
(POVaddict) / Linux Magazine 22 object { 41 }
03 23 GBall 42 object {
04 // Camera 24 translate <-1, -0.6, 0> 43 GBall
05 camera { 25 pigment { rgbf<1, 0.7, 44 translate <-1, 0.6, 0>
06 location <0, 0, -10> 0.7, 0.7> } 45 pigment { rgbf<0.7, 1,
07 direction <0, 0, 4> 26 } 1, 0.7> }
08 look_at <0, 0, 0> 27 object { 46 }
09 } 28 GBall 47
10 29 translate <0, 0, 0> 48 // Checkered pattern in
11 // Lighting 30 pigment { rgbf<.7, 1, background
12 light_source { <10, 10, .7, .7> } 49 plane {
-10> color rgb<1, 1, 1> } 31 } 50 <0, 0, -1>, -4
13 32 object { 51 pigment {
14 // Declaration of glass ball 33 GBall 52 checker color rgb<0.5,
15 #declare GBall = sphere { 34 translate <1, 0.6, 0> 0.5, 0.5>, rgb<1, 1, 1>
16 <0, 0, 0>, 0.5 35 pigment { rgbf<0.7, 0.7, 53 translate <-clock, -
17 scale <1, 1, 0.5> 1, 0.7> } clock, 0>
18 finish { phong 0.7 36 } 54 scale 0.4
reflection 0.1 refraction 37 object { 55 }
1 ior 1.33 } 38 GBall 56 finish { ambient 0.4 }
19 } 39 translate <1, -0.6, 0> 57 }
package, ppmtopgm sends the results to apply the oil effect to fields measuring 5 You can use the following command:
your standard output, which can be by 5 pixels.
redirected to an new file called glass.pgm To bring back some color to the image, povray +i glass.pov +w240 +h180U
using a greater than sign  > . Figure 2 we now add another filter to the chain. +fp +a0.1 +kfi00 +kff49 +kc
shows the results. pgmtoppm converts the greyscales to
In order to avoid creating additional blue and white, as is shown in figure 3. to have the raytracer produce an anima-
temporary files for the following filtering tion sequence with 50 images. The
Learning to Run
steps, we now make use of the fact that images are stored as glass00.ppm,
the Netpbm tools can read from Of course, there is nothing wrong with glass01.ppm, through glass49.ppm.
standard input. This allows us to link a calling individual filters in the Correspondingly, the options +kfi and
series of filters using pipes in the shell: command-line, but you will probably +kff refer to the numbers of the first and
need a shell script to make use of the last images. The option +kc shows that
ppmtopgm glass.ppm | pgmoil U power of most command-line tools. Shell this is a cyclical animation.
-n 5 | pgmtoppm Blue-White > U scripts allow you to send a whole The next task is to create an animated
oil.pgm collection of image files through the GIF image from the individual images
same chain of filters, or simply convert
The output from ppmtopgm is sent them to a different format.
GLOSSARY
directly to pgmoil. This filter adds an So now all we need is a whole bunch
Standard input, standard output: Many
effect to the image, making the contures of images to experiment on. Let s ask our
command-line programs allow you to omit
appear to melt just like in an oil painting. old friend Povray to help us out with that
the name of the input file. In this case the
The pgmoil option -n 5 tells the filter to animation feature we just talked about. program reads from standard input,which
will normally mean the keyboard. If you omit
the name of the output file, most programs
will use standard output, that is display the
results on your terminal
Pipe: The pipe character  | (representing a
stylized pipeline) connects the standard out-
put of a program to the standard output of
another program.This allows you to use
multiple programs in a single processing step.
Shell script: A file containing shell commands
that are processed automatically. Repetitive
tasks are often best accomplished using
automated shell scripts.
Figure 2: Everything turned grey all of a sudden Figure 3: Blue and white colouring with oil effect
www.linux-magazine.com November 2002 45
KNOW HOW Graphic Scripting
in the GIF to a maximum of 256. Finally,
Listing 2: mkgifanim Listing 4: mkoverlay
ppmtogif writes the GIF image itself. The
#!/bin/bash script uses the current value of the
#!/bin/sh
for f in glass??.ppm variable f to construct a file name,
for i in glass??.ppm
do removing the .ppm suffix and adding .gif
do
as the new suffix.
pnmscale -w 100 $f | ppmquant U
ppmtopgm $i | pgmedge | U
The following whirlgif call will run the
256 | ppmtogif > ${f%.ppm}.gif pgmnorm > temp.ppm
animation, g_anim.gif, in an infinite
done pnmarith -add temp.ppm glassU
loop (using the -loop option), with an
00.ppm > ${i%.ppm}.overlay.ppm
whirlgif -o g_anim.gif -loop U
interval of 8 milliseconds between the
done
-time 8 glass??.gif
images -time 8. Figure 4 shows the
ffmpeg -an -i
animation  of course you can only run
glass%02d.overlay.ppm -b 768 U
the animation, if you purchased the flip-
Listing 3: mkedge g_overlay.mpg
book plug-in for this issue. But seriously
#!/bin/bash
folks, check out the subscription CD for
for f in glass??.ppm
the file, which you can view in your web filter chain used for mkedge first,
do
browser or xanim. however, you will need an additional
ppmtopgm $i | pgmedge | U
step (pnmarith) to add the original
pgmnorm > ${i%.ppm}.pgm Animated Effects
image glass00.ppm pixel by pixel. This
done
In addition to GIF there are a few creates an interesting overlay effect.
ls glass??.pgm > frames.list
patented animation formats, such as When ffmpeg is called in mkedge (Listing
ppm2fli -g240x180 frames.list U MPEG and FLI. You can use xanim to 4), the expression for the input file
edge_anim.fli view the latter. The mkedge animation in (glass%02d.overlay.ppm) is expanded by
Listing 3 requires ppm2fli, which is not a ffmpeg itself.
Netpbm tool. You might like to perform some
and incorporate the GIF in a website. To This script also processes all the experiments of your own, and read the
prevent the GIF image from becoming individual images in the glass ball man pages, to familiarize yourself with
too large you might decide to scale down sequence, converting them to greyscale the range of filters provided by the
the image to 100 by 75 pixels. The shell (ppmtopgm) and creating lines for the Netpbm tools. The man pages for pbm,
script mkgifanim (Listing 2) takes care of edges in the image (pgmedge) and pgm, ppm, and pnm contain an
this task and goes on to call the whirlgif normalizing the brightness (pgmnorm). overview. %
tool that assembles the individual GIF The resulting images are named
images to an animated GIF. glass00.pgm through glass49.pgm. Since
In the for loop, the variable f parses ppm2fli expects a list of the individual
any file names that match the shell images in a file, you will need to run ls to
expression (see box below) glass??.ppm. create this list, before you launch
Within the filter chain pnmscale is used ppm2fli. You will also need to use the
to scale down the individual images to a option -g to tell the tool the image
width of 100 pixels. The height is formats in use.
calculated automatically to retain the You can use the ffmpeg tool to create a
original proportions. In the next step further animation  as the name would
ppmquant reduces the number of colors suggest, an MPEG. You can apply the
Shell Patterns
The shell recognizes various expressions for file and directory names, and expands them before
running the current command.The most important examples are
" the question mark ?, which represents exactly one character.
" the asterisk *, which is a wildcard for any number of random characters (even zero).
" character counts in square brackets []. Exactly one character of the type in the brackets must
occur at this position.There are various notations as is evidenced by the following examples:
The expression lx[acE].txt matches the names lxa.txt, lxc.txt, and lxE.txt.
graph[a-z][0-9][0-9].jp? matches the names graphi50.jpg, grapho01.jpg, and graphx55.jpe
(amongst others).The dash within the brackets indicates that a complete character set is to be
matched, for example lower case letters a through z,or numbers between 0 and 9.
The expression [^abc][xyz].b* matches both wy.b and 3x.ball (amongst others), but does not
match cz.bmg or rx.img.The ^ character after the opening bracket indicates a negation, meaning
any characters apart from those listed.
Figure 4: Animation
46 November 2002 www.linux-magazine.com


Wyszukiwarka

Podobne podstrony:
2005 11 Discovery Scripts Bash Based Hardware Detection for Pci and Usb
2005 11 Discovery Scripts Bash Based Hardware Detection for Pci and Usb
2002 11 Lyx Part 1 High Quality Word Processing and Text Layout
2002 11 Wyświetlacz widmowy
Of Time and Tide
Time Prepositions and Adverbs with Present Perfect
Time and Physical Geometry
2002 11 Wzmacniacz prądowy do subwoofera
CAPTAIN TSUBASA (Road to 2002) 11
Dz U 2002 240 2052 zmiana z dnia 2002 11 23
Matematyka dyskretna 2002 11 Poprawność programów
2005 11?ta Tunnel Workshop A Quick and Simple Private Tunnel with Openvpn
2002 11 Gnu M4 Creating Html Pages with the Macro Language
2002 11 Szkoła konstruktorów klasa II

więcej podobnych podstron