Fundamentals of Computer Science I: Media Computing (CS151.01 2008S)

Laboratory: Programming with the GIMP Tools

Summary: In this laboratory, you will explore how one instructs the GIMP to create some simple images.


Start DrFu.


Exercise 1: Simple Faces

Here are the instructions for drawing a simple face from the reading.

(define smiley (image-new 200 200))
(image-show smiley)
; Draw the primary circle
(image-select-ellipse! smiley selection-replace 
                       10 10 180 180)
(context-set-fgcolor! "yellow")
(image-fill! smiley)
(context-set-fgcolor! "black")
(context-set-brush! "Circle (09)")
(image-stroke! smiley)
; Draw the eyes
(image-select-ellipse! smiley selection-replace 
                       50 60 30 20)
(image-select-ellipse! smiley selection-add
                       120 60 30 20)
(context-set-fgcolor! "white")
(image-fill! smiley)
(context-set-fgcolor! "black")
(context-set-brush! "Circle Fuzzy (07)")
(image-stroke! smiley)
(image-select-ellipse! smiley selection-replace
                       60 60 10 20)
(image-select-ellipse! smiley selection-add
                       130 60 10 20)
(context-set-fgcolor! "light steel blue")
(image-fill! smiley)
; Smile
(image-select-ellipse! smiley selection-replace 
                       40 60 120 100)
(image-select-ellipse! smiley selection-subtract 
                       40 45 120 100)
(context-set-fgcolor! "white")
(image-fill! smiley)
(context-set-fgcolor! "red")
(context-set-brush! "Calligraphic Brush#3")
(image-stroke! smiley)
; Get ready to show
(image-select-nothing! smiley)

a. Copy these instructions into your definitions pane. Read through the instructions to see what image you predict they will draw.

b. Click Run and observe what happens. If all has gone well, you should see a new image with a smiling face.

c. In your interactions window, write instructions for drawing a nose onto the image. Your instructions might look something like the following:

> (image-select-rectangle! smiley selection-replace 90 90 20 20)
> (image-select-ellipse! smiley selection-intersect 80 85 30 30)
> (context-set-fgcolor! color-green)
> (image-fill! smiley)

If the instructions don't seem to change the image, try clicking on the image window or typing (context-update-displays!).

e. It is possible to clear an image by selecting everything and then clearing the selection. Do so now.

> (image-select-all! smiley)
> (image-clear-selection! smiley)
> (context-update-displays!)

f. Sometimes it's more fun to see an image drawn little-by-little. DrFu allows you to pause a bit between actions, as when you want to show each step in the creation of a drawing. In particular, the procedure (usleep n) pauses for n microseconds. Hence, if you want to pause for a half a second, you might write (usleep 500000).

Edit the instructions in the definitions pane as follows. Immediately after each call to image-fill! and image-stroke!, insert instructions to update the displays and then sleep half a second. (Hint: Type the instructions once, then use copy and paste.)

Run your modified code.

Exercise 2: Drawing Faces, Revisited

a. Open a new DrScheme window and write a series of instructions to draw a face different than the sample we gave above. You should try a different shape of face, different brushes or colors, and different facial features.

b. What do you expect to happen if you change the size of the image in which you're drawing, such as a 100x100 image or a 300x300 image?

c. Check your answer experimentally.

d. If changing the size of the image yielded results that you found undesirable, summarize how you might fix them. (Don't fix them right now, just indicate how you might fix them.)

Exercise 3: Drawing Houses

a. Write a series of instructions that create a new image and then draw a house in in the image.

b. Suppose we start by defining two names, width and height. Think about how you might change your house code to take those values into account.

(define width 300)
(define height 200)
(define canvas (image-new 300 200))

Exercise 4: Modifying Existing Images

Suppose we've defined picname as a string naming an image.

(define picname "/home/rebelsky/glimmer/samples/rebelsky-stalkernet.jpg")

a. Write a series of instructions to load and show the picture.

b. Write a series of instructions to draw a diagonal line from the upper-left-hand-corner to the lower-right-hand corner of the image. Note that you'll need to use image-width and image-height to determine the width and height of the image.

c. Write a series of instructions to put some kind of border at the edges of the image. (E.g., select a rectangle ten pixels from each edge and then stroke that rectangle.)

For Those with Extra Time

Extra 1: Generalizing smiley faces

Revise your instructions from Exercise 2 so that the drawing adapts to the size of the image.

Recall that you can obtain the width and height of the image using the image-width and image-height procedures.

Creative Commons License

Samuel A. Rebelsky,

Copyright (c) 2007-8 Janet Davis, Matthew Kluber, and Samuel A. Rebelsky. (Selected materials copyright by John David Stone and Henry Walker and used by permission.)

This material is based upon work partially supported by the National Science Foundation under Grant No. CCLI-0633090. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation.

This work is licensed under a Creative Commons Attribution-NonCommercial 2.5 License. To view a copy of this license, visit or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.