Fundamentals of Computer Science 1 (CS151 2003S)

Generating Gimp Images with Script-Fu

Summary: In this laboratory, you will begin to investigate the ways in which you can use Scheme to program the GNU Image Manipulation Program, the Gimp.

Exercises

Exercise 0: Getting Started

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 (Scheme in one Day or Scheme in One Define).

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. What happens if you try to type a multi-line expression in the console?

e. 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

(load "/home/rebelsky/Web/Glimmer/ScriptFu/Code/gsfu.scm")

Exercise 1: Drawing Your First Images

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. You use car because gimp-image-new returns 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 it 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.

(gimp-display-new image)

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 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 layer)
(gimp-selection-none image)

The parameters to gimp-rect-select are

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 layer)
(gimp-selection-none image)

The parameters to gimp-ellipse-select are similar to those of gimp-rect-select except that there's an additional antialias parameter that precedes the feather parameter.

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))
(gsfu-line layer 50 50 200 50)
(gsfu-line layer 200 50 50 200)
(gsfu-line layer 50 200 50 50)

Exercise 2: Experimenting With Variations

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 often 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 (gimp-brushes-list).

Experment with a few brushes.

Exercise 3: Designing Your Own Images

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?

Exercise 4: Other Procedures

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.

Exercise 5: Unpacking gsfu-line

Here is the code for gsfu-line, which you used earlier.

(define gsfu-line
  (lambda (layer x1 y1 x2 y2)
     (gimp-paintbrush-default layer 4 (float-array x1 y1 x2 y2))))

I wrote gsfu-line and float-array to shield you from the wonders (horrors?) of some of the built-in procedures.

By reading the comments for gimp-paintbrush-default you should be able to find out better ways of drawing more complex objects. You may also want to look at the comments for gimp-paintbrush.

Use gimp-paintbrush-default or gimp-paintbrush to draw a triangle.

 

History

Tuesday, 3 April 2001 [Samuel A. Rebelsky]

Wednesday, 4 April 2001 [Samuel A. Rebelsky]

Monday, 28 October 2002 [Samuel A. Rebelsky]

Tuesday, 1 April 2003 [Samuel A. Rebelsky]

 

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Tue May 6 09:29:15 2003.
The source to the document was last modified on Tue Apr 1 09:19:25 2003.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2003S/Labs/script-fu.html.

You may wish to validate this document's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu