| Schedule | Readings | Labs | Homework | Mechanics | Contact | |
| CSC 151-01, 2007S » Lab 28 » Writing Script-Fu Procedures | ||||||
Summary: In today's laboratory, you will write and install new procedures for use within Script-Fu and the Gimp.
Contents:
a. Open a terminal window.
b. Type the following (substituting your user name)
ln -s /home/rebelsky/Web/Courses/CS151/2007S/Examples/gimp.scm /home/username/.gimp-2.2/scripts/gimp.scm
This makes a symbolic link (similar to a shortcut in Windows or MacOS) from the second filename to the first filename.
(If you are working outside the MathLAN, you'll need to save a copy of gimp.scm
and put it in a similar folder. On a Macintosh running OS X, save the
file on the desktop, open a terminal window (in the
Applications/Utilities folder), and type mv Desktop/gimp.scm .gimp-2.2/scripts/gimp.scm. Windows users get to figure it out themselves.)
c. Start GIMP by typing gimp in the terminal window.
d. Open a new Script-Fu console.
e. If all has gone well, you should be able to use our GIMP utilities without loading the file. To ensure that you can, create an image and draw an oval.
(set-fgcolor GREEN)
(set-bgcolor BLACK)
(define img (create-image 100 100))
(select-ellipse img REPLACE 20 20 60 60)
(set-bgcolor WHITE)
(fill-bgcolor img)
(stroke img)
(show-image img)
The
corresponding reading includes code for a procedure called
no-failure.
You can find that procedure in the file no-failure.scm.
a. Make your own copy of that file.
b. Load the file into the Script-Fu console by typing
(load filename). For example, if you've stored
your stuff as images.scm on your desktop, you should use
(load "/home/username/Desktop/no-failure.scm")
c. Type (no-failure) in the Script-Fu console and see what
happens.
d. Open no-failure.scm in DrScheme. Change the
background color (say, to GREY) and the color for the letter
(say, to BLACK). Save the file from DrScheme.
e. In the Script-Fu console, reload no-failure.scm and
type (no-failure).
Using no-failure as a template, write and test your own
procedure to draw an interesting picture. You might draw a smiley face, a stick figure, or something else.
One disadvantage of no-failure is that it always draws an image
of the same size. What if we want a bigger or smaller image? One
possibility is to make the image size a parameter to no-failure
and then make the various numbers depend on the size. Since our image is
naturally square, the width and height should probably be the same.
You can find the modified procedure in the file new-no-failure.scm.
a. Make your own copy of this file.
b. Load the file into the Script-Fu console.
c. Test the procedure by typing each of the following commands in the Script-Fu console.
(new-no-failure 256 GREEN)
(new-no-failure 100 PURPLE)
Generalize your procedure from exercise 2 so that it takes the image
size as a parameter. You may choose to build square images (as in new-no-failure)
and take only the length of a side as a parameter or you may choose
to build rectangular images and take both width and height as
parameters.
As the reading suggests, you can make your own procedures available in the GIMP menus.
a. Make a copy of no-failure-menu.scm in the directory /home/username/.gimp-2.2/scripts
b. Tell GIMP to refresh its Script-Fu list by selecting from the menu in the menu.
c. You should now be able to select from a new submenu of the submenu of the menu. Try it and see what happens.
d. Using no-failure-menu.scm as a template, register
your own script from the previous exercise. See if you
can get it to run.
When doing more complex drawings, you will often find that you draw the same thing again and again at different places and in different sizes. Hence, it is sometimes useful to write helper procedures that can do the precise drawing for you. For example, if you often make the "No" sign, you might write a helper procedure for that sign. Here's the procedure.
;;; Procedure:
;;; forbidden
;;; Parameters:
;;; image, an image
;;; x, the x coordinate of the center of the sign
;;; y, the y coordinate of the center of the sign
;;; radius, the radius of the sign
;;; Purpose:
;;; Draws a "forbidden" sign (a circle with a slash) in
;;; red centered at the specified location.
;;; Produces:
;;; (nothing)
;;; Preconditions:
;;; image and layer are initialized.
;;; x, y, and radius are not negative.
;;; Postconditions:
;;; The image now contains the specified sign.
;;; The brush and foreground color may have changed.
(define forbidden
(lambda (image x y radius)
; Choose a color and paintbrush that seem appropriate.
(set-fgcolor RED)
(set-brush "Circle (05)")
; Select the ellipse for the circle. Do the math to see why.
; the particular values were set.
(select-ellipse image REPLACE
(- x radius) (- y radius)
(* 2 radius) (* 2 radius))
; Draw the nice red circle.
(stroke image)
; Now we're ready to draw the slash. Some math tells us that
; it's offset by radius/(sqrt 2) from the center (in each
; direction).
(let ((offset (/ radius (sqrt 2))))
(line image
(- x offset) (- y offset)
(+ x offset) (+ y offset)))
; Unselect all
(select-nothing image)))
a. Store that code in a new file.
b. Create a new image programmatically and use forbid
to draw on that image in various places and sizes.
c. Write your own helper procedure to draw a rectangle.
d. Write your own helper procedure to draw a circle.
Janet Davis (davisjan@cs.grinnell.edu)
Created March 30, 2007 based on http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2007S/Labs/script-fu-procs.html