# Algorithmic Art

Summary: In this laboratory, you will continue your exploration of Script-Fu by using more Scheme-like operations to automatically generate art. You will also explore techniques for modifying existing images.

## Exercises

### Exercise 0: Preparation

a. Start the Gimp.

b. Open the Script-Fu console.

c. Open the Script-Fu procedure reference.

d. Make a copy of `art.scm`, the sample code for this lab.

### Exercise 1: Higher-Order Drawing

We've seen a number of ways in which Scheme can be useful for creating images. But we haven't yet used all the wonders of Scheme. For example, we have yet to take advantage of recursion or higher-order procedures. I've written a simple procedure, `(gsfu-create-image width height func)`, that creates a new image and layer by applying func to every pair of (x,y) values in a new image.

Here's a sample procedure you might use:

```(define exercise-one
(lambda (x y)
(list
(mod (+ x y) 256)
(mod (* y y) 256)
(mod (trunc (* 1000 (sin x))) 256))))
```

a. Try to create a small image (say 100 by 100) using `gsfu-create-image` and `exercise-one`.

Note that the list returned contains an image id and a layer id. You'll need to show the result using `(gimp-display-new image-id)`.

b. Try to create a somewhat larger image (say 200 by 150) using `gsfu-create-image` and `exercise-one`.

c. What, if anything, do you note about the relationship of the two images?

### Exercise 2: Your Own Functions

a. Experiment with you own variations of `exercise-one`. You might try using `sin`, `cos`, powers, addition, subtraction, and much much more.

b. See if you can make something that looks circular somewhere in your image. You may want to take advantage of the fact that the distance between two points is the square root of the sum of the squares of the horizontal and vertical distances.

Things can get even more interesting when you work with an existing graphic. It's fairly easy to load and get information about an existing graphic. You can load an image file with

```(define image (gsfu-load-image path))
```

For example,

```(define image (gsfu-load-image "/home/rebelsky/Web/CS151/Images/NorthCampus.jpg"))
```

(Yes, there are also built-in commands for loading images. Feel free to look for them in the procedure browser. I find mine easier to use.)

Try loading an image of your choice using `gsfu-load-image`. Note that the list returned contains an image id and a layer id. You'll need to show the result using `(gimp-display-new image-id)`.

### Exercise 4: Mapping Images

It is possible to map a function onto an image by getting the color at each point, applying the function, and then setting the color to the result of the function. I've implemented a map for images as

```(gsfu-map-image image layer func)
```

a. Here's a simple procedure that maps a color onto another color.

```(define four-a
(lambda (color)
(let* ((red (car color))
(list green blue red))))
```

What do you think will happen if you run `gsfu-map-image` using this procedure?

Verify your results experimentally. Note that it may take a minute or two for the procedure to run. You may want to read ahead while waiting for `gsfu-map-image` to complete.

b. Here is another simple procedure that maps a color onto another color.

```(define four-b
(lambda (color)
(let* ((red (car color))
(tmp (/ (+ red green blue) 3)))
(list tmp tmp tmp))))
```

What do you think will happen if you run `gsfu-map-image` using this procedure?

c. Here is a third simple procedure that maps a color onto another color.

```(define four-c
(lambda (color)
(let* ((fun (lambda (val) (* 32 (trunc (/ val 32)))))
(red (car color))
(list (fun red) (fun green) (fun blue)))))
```

What do you think will happen if you run `gsfu-map-image` using this procedure?

### Exercise 5: Your Own Color Transforms

Write and test a few color transformations of your own. Your transformations should include:

a. One that adds 128 to each color value and then mods by 256.

b. One that makes each new color value depend on a combination of color values in the earlier image.

c. One that uses `max` in some interesting way.

## History

Friday, 1 November 2002 [Samuel A. Rebelsky]

• Created.

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 Mon Dec 2 09:18:38 2002.
The source to the document was last modified on Fri Nov 1 09:57:11 2002.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2002F/Labs/algorithmic-art.html`.

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

Samuel A. Rebelsky, rebelsky@grinnell.edu