Summary: In this laboratory, you will begin to investigate the ways in which you can use Scheme to program the GNU Image Manipulation Program.
a. Start the GIMP by typing
gimp in a terminal window.
If you haven't run the GIMP before, you should allow the GIMP to
do the normal installation procedure.
b. Select Console from the Script-Fu menu available from under the Xtns menu in the primary GIMP palette. You should see a window appear with some text at the top and an entry box at the bottom. This is the console for interacting with Script-Fu, which is based on a variant of Scheme called SIOD.
c. Type a few, simple, one-line Scheme expressions to see what happens. For example, you might ask for a few simple sums, extract parts of lists, or whatever.
d. I've written a few procedures and defined a few constants that you may find helpful as you work with Script-Fu. Load them with
To create an image in the GIMP, you need to follow a number of steps (perhaps these are steps you'd expect from your own attempts to write instructions for using the GIMP), including creating the image, adding a layer in which to draw, selecting colors, and applying various tools. In this exercise, you'll step through some of the basic commands. Enter each of the commands in the console window.
a. Define the width and height of your new image with
(define width 256) (define height 384)
b. Create and name your new image with
(define image (car (gimp-image-new width height RGB)))
The image will not yet appear. Script-Fu assumes that you may want
to do some things with the image before you show it on the screen.
a one-element list consisting of the ID of the image. (No, I don't
know why it returns a list; it just does.)
c. Build a new layer and add to the image. You need layers in order to draw.
(define layer (car (gimp-layer-new image width height RGB_IMAGE "Layer" 100 0))) (gimp-image-add-layer image layer 0)
d. We're now ready to show the image. You need only one line.
e. You may see that your image already contains some stuff (typically random ``garbage''). That's because Script-Fu allocates some area of memory for your image without bothering to clear out the old contents of that memory.
You can clear your image with a few commands.
(gimp-selection-all image) ; Select everything (gimp-edit-clear image layer) ; Do "Clear" from the "Edit" menu (gimp-selection-none image) ; Select nothing
In the future, you should probably clear the image before diplaying it.
f. Now you're almost ready to draw. In order to draw, you need a foreground color and a background color.
(gimp-palette-set-background WHITE) (gimp-palette-set-foreground (list 0 0 255))
g. You also need a brush.
(gimp-brushes-set-brush "Circle (03)")
h. As some of you discovered on the first day, the simplest drawing technique is to select something and then "stroke" it. We can use that technique to draw a box.
(gimp-rect-select image 10 10 100 100 REPLACE 0 0) (gimp-edit-stroke image layer) (gimp-selection-none image)
The parameters to
i. We can also use that technique to draw a circle in a different color
(gimp-palette-set-foreground (list 255 0 0)) (gimp-ellipse-select image 50 50 80 80 REPLACE 0 0 0) (gimp-edit-stroke image layer) (gimp-selection-none image)
The parameters to
gimp-ellipse-select are similar to
gimp-rect-select except that there's an
additional antialias parameter that precedes the
j. The other common way to paint things is with one of the brush tools. I've written a simple painting procedure that takes six parameters: the image, the layer, the x coordinate of the starting point, the y coordinate of starting point, the x coordinate of the ending point, and the y coordinate of the ending point.
(gimp-palette-set-foreground (list 0 255 0)) (samr-line image layer 50 50 200 50) (samr-line image layer 200 50 50 200) (samr-line image layer 50 200 50 50)
As you may guess, it is possible to vary most of the drawing commands given in the previous exercise. Clearly, any command that takes parameters that describe locations on the screen can have different locations. In addition, you should be able to change the colors and brushes you use.
a. In the previous exercise, you wrote a command to draw a box. Try varying the numbers used in that command and see what happens.
b. In Script-Fu, colors are described by a list of three integers,
each of which is between 0 and 255. The integers represent, respectively,
the amount of red, green, and blue in the resulting color. These
colors are similar to those that come from colored lights, rather than
those that come from colored paints, and may not add in the way that you
remember from kindergarten. For example,
(list 255 255 255)
is white, rather than black.
Experiment with numeric color variations. You may find it helpful to use the GIMP color picker.
c. In Script-Fu, brushes are described by a text string. You can find
a list of all available brushes with
Experment with a few brushes.
a. Using the commands that you learned in the previous exercise, tell the GIMP to create a new image in which you use Script-Fu to draw something interesting (perhaps a stick figure, perhaps something more complicated).
b. What advantages, if any, are there to using Script-Fu to create that image?
Script-Fu and the GIMP come with an astonishing collection of procedures. You can get a terse list of these procedures by selecting DB Browser from the Xtns menu or by clicking on Browse... in the Script-Fu console.
Spend a few minutes exploring the list.
Here is the code for
samr-line, which you used earlier.
(define samr-line (lambda (image layer x1 y1 x2 y2) (gimp-paintbrush image layer 0 4 (float-array x1 y1 x2 y2))))
float-array to shield
you from the wonders (horrors?) of some of the built-in procedures.
By reading the comments for
gimp-paintbrush you should
be able to find out better ways of drawing more complex objects.
gimp-paintbrush to draw a triangle.
Tuesday, 3 April 2001 [Samuel A. Rebelsky]
Wednesday, 4 April 2001 [Samuel A. Rebelsky]
Disclaimer: I usually create these pages on the fly. This means that they are rarely proofread and may contain bad grammar and incorrect details. It also means that I may update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.
This page was generated by Siteweaver on Thu May 3 23:08:02 2001.
This page may be found at
You may validate this page's HTML.
The source was last modified Wed Apr 4 15:08:45 2001.