CPSC150A
Scientific Computing

Activity 28

Image Processing

Lighten

Create a Python program that reads a image file, and draws a lightened version of the read image.

In order to lighten an image, you need to take a color value, and make it larger using addition. After increasing a color value, it may be greater than the maximum color value, 255. To fix a color value that is too large, replace it with the maximum value, 255.

Example

Before
After

Grayscale

Create a Python program that reads a image file, and draws a grayscale version of the read image.

The program should convert every pixel into a grey color. A grey pixel is simply a pixel whose 3 color values are exactly the same. A simple way to accomplish this is to compute the average of the 3 color values for a pixel, and use this as the red, green, and blue values of each pixel.

Example

Before
After

Posterize

While you might not recognize the name "posterize," you've probably seen an example of it. Posterization is a method by which the number of colors in the image is reduced. In the case of the most famous example, it was reduced to a 4 color poster. Here, we are going to only use 2 colors: black and white.

Details

Create a Python that reads in animage file, and draws a posterized version of the read image.

To posterize a picture, you must read 3 color values at a time from the file. Read these 3 colors values and compute the average of them. If the value is above a threshold, change the use one color, otherwise use a different color.

Example

Before
After

  • For each pixel in the image, compute the average of the red, green, and blue values.
  • If the average is above a threshold (you pick), set the new pixel's red, green, and blue to a certain color (you pick). Otherwise, set the new pixel's red, green, and blue, values to another color (again, you pick).

Challenge

Modify your program so that it does some other number of colors. In the popular Obama example, they used 4 colors: red, light blue, dark blue, and beige. For this, at least use 3 colors!

Green Screen

It can be pretty expensive to film movies and TV shows on location. However, with a little bit of Movie Magic, we can pretend to be anywhere in the world. Ever want to pretend you traveled to the expanses of Moscow? Now is your chance! Ever want to go to Delaware? Yeah… Delaware.

Use Google image search to find a green screen image, and a background image that is at least the same size, if not larger. You are going to write a program that will take these two images, and composite them together to create one image.

Details

Create a Python program that replaces the background in a green screen image with the pixels of another image. The program should read two image files, a green screen image and a background image, and draw a new image that replaces all of the green pixels in the green screen image with the cooresponding pixel in the background image.

It's really hard to get a green screen to be exactly green. So, it is not sufficient to check just the the green value of a pixel. The program must make sure the green value is high and that the red and blue values are low enough to make sure the color is truly "green."

Example

Here is a very small example of what the program should do:

+ =

  • Make sure that the background image is larger than the green screen image in both width and height. Otherwise, you will get an index out of bounds error when you try to read a pixel that doesn't exist.
  • The program will need to analyze every pixel in the green screen image. Use nested for loops to iterate over all indices for the rows and columns.
  • Test if a pixel in the green screen image is green by testing if the green value is above some threshold, the green value is below another threshold , and the blue value is below yet another threshold. You You will likely have to play around with the threshold values you to get a better green screen effect.
  • Modify a pixel that is green by replacing its RGB values with the RGB values in the background image at the exact same row and column.

Challenge

It's one thing to use a green screen image you found on the Internet. It's another thing entirely to make your own green screen images! Luckily, we have this nice (hopefully pretty clean) chalkboard that we could you to mimic a green screen.

Take a picture with your cellphone of yourself standing in front of the chalkboard. Email the picture to yourself, and try to composite your image with a different image you found on the web. You will likely have to make drastic changes to your threshold values, but you should be able to get something that looks decent.