Building on the earlier image manipulation and logic sections, here we see how to implement a movie special effect.
The stop.jpg image and the leaves.jpg image
The above bluescreen example code detects the red part of the sign, and for those pixels, copies over the pixels from the back image. The bluescreen code below has 3 key differences from the earlier examples:
back = new SimpleImage("leaves.jpg");-- open a second image and store it in the variable "back" (thus far we've only opened one image at a time)
pixel2 = back.getPixel(x, y);-- say we are looking at pixel x,y in the main image. Get the x,y pixel from the other image and store it in the variable "pixel2". We can get the x and y of the current pixel in the loop with the functions pixel.getX() and pixel.getY().
pixel.setRed(pixel2.getRed());-- copy the red value from pixel2 to pixel. Also do this for the other two channels. In effect, for the x,y location, this copies the pixel from back to image.
In this example we have the abby.jpg image -- make it appear that the green parts of her little chair are sprouting leaves.
Now we'll do one like the movies -- film the movie start in front of a blue screen. Replace the blue background behind the monkey with pixels from moon.jpg.
Here is our monkey movie star:
Here is our background, the famous Apollo 8 photo of the earth shown rising
above the moon horizon.
Working with a main image and a back image, there is a problem if the back image is smaller than the main image -- the code will try to access a non-existent pixel, and we get an "x/y out of bounds" error. The following line fixes this problem:
The above line resizes the back image if necessary, so it is at least as big as the main image, which is exactly what we need for blue screening, picking pixels from the back image to place into the main image. You'll see this line in the starter code for some later problems.
Another effect we can try is blending a "ghost" version of the monkey into the back image. We blend the colors from the two images.