Laboratory: Collage


This lab was newly created for Spring 2016, and was only finished the evening before it was conducted. It is likely to have bugs.

Summary: In this laboratory, you will experiment with collage, or at least with copy and paste.

Preparation

a. Bring up the Procedure Browser by selecting Procedure Browser from the GIMP Help menu>.

b. Review the steps in copying and pasting. These steps follow.

  • Select the area you want to copy in the source image.
  • Call (gimp-edit-copy-visible source), where source is the number that identifies the source image.
  • Select the area you want want to paste into in the target image. (We've used the same image for the source and the target, but you can use separate images.)
  • Call (gimp-edit-paste (image-get-layer target) 1) where target is the number that identifies the source image.
  • Take note of the number that is returned. You can do so manually, or you can write code that does so.
    > (define pasted (car (gimp-edit-paste (image-get-layer target) 1)))
    
  • Call (image-select-nothing! target) to make sure nothing is currently selected.
  • Transform the pasted material as appropriate with gimp-layer-scale, gimp-item-transform-rotate, and other related procedures.
  • Call (gimp-image-flatten target).

Exercises

Exercise 1: Simple Copying and Pasting

a. Open an existing image, such as our kitten image.

> (define source (image-show (image-load "/home/rebelsky/Desktop/kitten.jpg")))

b. Open a new 300x200 image for our collage.

> (define collage (image-show (image-new 300 200)))

c. Copy a 75x50 portion of the original image.

> (image-select-rectangle! source REPLACE ___ ___ 75 50)
> (gimp-edit-copy-visible source)

d. Paste the copied portion to elsewhere in the collage.

> (image-select-rectangle! collage REPLACE 150 50 75 50)
> (gimp-edit-paste (image-get-layer collage) 1)
> (image-select-nothing! collage)
> (gimp-image-flatten collage)

e. Paste the copied portion into four other locations in the collage. (You can choose the locations.)

Exercise 2: Helper Procedures

As you likely noted, you did almost the exact same thing five times in the previous code. That is, you selected a rectangle in the collage, you called gimp-image-paste, you selected nothing, and you flattened the result.

Write a procedure, (my-paste! image left top width height), that encapsulates those steps.

Check your code with the following.

(define kitten (image-show (image-load "/home/rebelsky/Desktop/kitten.jpg")))
(define collage (image-show (image-new 300 200)))
(image-select-rectangle! kitten REPLACE 170 110 100 100)
(gimp-edit-copy-visible kitten)
(my-paste! collage 0 0 100 100)
(my-paste! collage 100 0 100 100)
(my-paste! collage 200 0 100 100)
(my-paste! collage 0 100 100 100)
(my-paste! collage 100 100 100 100) 

Exercise 3: Scaling

a. Open an existing image, such as our kitten image.

> (define source (image-show (image-load "/home/rebelsky/Desktop/kitten.jpg")))

b. Open a new 300x200 image for our collage.

> (define collage (image-show (image-new 300 200)))

c. Copy a 75x50 portion of the original image.

> (image-select-rectangle! source REPLACE ___ ___ 75 50)
> (gimp-edit-copy-visible source)

d. Paste the copied portion at a scale appropriate for the whole collage.

> (image-select-rectangle! collage REPLACE 0 0 300 200)
> (define pasted (car (gimp-edit-paste (image-get-layer collage) 1))
> (image-select-nothing! collage)
> (gimp-layer-scale pasted 300 200 1)
> (gimp-image-flatten collage)

Exercise 4: Another Helper

Consider the following procedure.

(define p-and-s!
  (lambda (image left top width height)
    (image-select-rectangle! image REPLACE left top width height)
    (let ([pasted (car (gimp-edit-paste (image-get-layer image) 1))])
      (image-select-nothing! image)
      (gimp-layer-scale pasted width height 1)
      (gimp-image-flatten image)
      image)))

a. What does this procdure do?

b. Why do we use a let in the middle of the procedure?

c. How could we achieve the same result without using let?

d. Use this procedure to add a few more scaled versions of your copied selection to our collage.

Exercise 5: Self-Pasting

Consider the following procedure.

(define self!
  (lambda (image)  
    (let ([width (image-width image)]
          [height (image-height image)])
      (image-select-rectangle! image REPLACE 0 0 width height)
      (p-and-s! image 
                (* 1/10 width) (* 1/10 height)
                (* 9/10 width) (* 8/10 height)))))

a. What does this procedure do?

b. What image do you expect to result from the following?

> (define kitten (image-show (image-load "/home/rebelsky/Desktop/kitten.jpg")))
> (self! kitten)

c. Check your answer experimentally.

d. What do you expect to happen if we call self! again on the same image?

> (self! kitten)

e. Check your answer experimentally.

Exercise 6: Rotation

Write a procedure, (my-paste-scale-and-rotate! image angle left top width height) that pastes the copy region into the selected area and then rotates by the given angle. You will likely base this procedure on the p-and-s! from an earlier problem.

Here's a typical call to rotate a pasted selection.

(gimp-item-transform-rotate pasted angle 1 0 0)

For Those With Extra Time

If you find that you have some extra time, you may play with making your own collages or you may attempt some of the following problems.

Extra 1: Alternate Rotations

In the reading and in the rotation problems above, we used 1 0 0 as the last three paramaeters to gimp-item-transform-rotate. By reading the documentation and by playing with different values, see what other rotation effects you can achieve. (Hint: Use 0 x y with different values of x and y.)

Extra 2: Other Transformations

We've explored how to scale and rotate copied portions of an image. But there are many other transformations available. Search for gimp-item-transform in the Procedure Browser and see what other interesting things we can do.

Extra 3: Andy

Write a procedure, (andy image), that creates a new image that is the same size as image and contains four identical (but smaller) copies of image in a 2x2 grid.

Extra 4: Drella

You can transform only part of an image by using the image-select-rectangle! and image-transform! procedures. For example,

> (define kitten (image-show (image-load "/home/rebelsky/Desktop/kitten.jpg")))
> (define half-width (* 1/2 (image-width kitten)))
> (define half-height (* 1/2 (image-height kitten)))
> (define irgb-only-blue (section irgb-subtract <> (irgb 255 255 0)))
> (image-select-rectangle! kitten REPLACE half-width 0 half-width half-height)
> (image-transform! kitten irgb-only-blue)
> (image-select-nothing! kitten)

Using a technique like this, make a more sophisticated drella procedure that acts like andy, but recolors each of the copies.