CPSC150A
Scientific Computing

Activity 25

Image I/O

Decrease Contrast

Create a Python program that reads an image file and writes a different file that is the original image with decreased contrast. Decrease the contrast of an image by rescaling all numbers into the range 0.25 to 0.75. The program should not use any loops.

Example

Before
After

Grayscale

Details

Create a Python program that that reads an image file and writes a different file that is the original image converted to grayscale. In a grayscale image, for each pixel, all three color values are identical. To compute the grayscale value of a pixel, average the values of the red, green, and blue channels. The program should not use any loops.

Example

Before
After

Posterize

While you might not recognize the name "posterize," I'm certain you have 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 an image file, and writes a different image file which is the posterized version of the read file.

To posterize a picture, replace all pixels with a grayscale value above 0.5 with one color and all pixels with a grayscale value equal to or below 0.5 with another color.

Example

Before
After

  • Use the same code that created the grayscale image to find the grayscale value of each pixel.
  • Remember that numpy also supports comparison operations, <, >, etc., on arrays. Use this to create an array of booleans that can be used as a mask to reassign a color to only some pixels.

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!