CPSC150A
Scientific Computing

Activity 12

Numpy

Decrease Contrast

Write the function decrease_contrast(image_array), which has 1 parameter, a numpy array that represents a color RGB image. The function should return a new numpy array that is the input image with the range of values scaled to between the range 63.75 to 191.25, that is half the range, but still centered on 127.5. The function should not use any loops. Test the function with a small test array and printing the result before testing on a larger image and writing the result to a file.

Example

Test Code Output
print(decrease_contrast(numpy.array([[[0, 255, 0]]])) [[[ 63.75 191.25 63.75]]]

Grayscale

Write the function grayscale(image_array), which has 1 parameter, a numpy array that represents a color RGB image. The function should return a new numpy array that is the input image converted to a grayscale image. A grayscale image has only one value per pixel, instead of three, in the range 0 to 255. To compute the grayscale value of a pixel, average the values of the red, green, and blue channels. The function should not use any loops. Again, test the function with a small test array and printing the result before testing on a larger image and writing the result to a file.

Test Code Output
print(grayscale(numpy.array([[[0, 255, 0]]])) [[85.]]

Posterize

Write the function posterize(image_array, threshold, dark_color, light_color), which has 3 parametera, image_array, a numpy array that represents a color RGB image, threshold, an intensity between 0 and 255, and dark_color and light_color, numpy arrays that each represent a single color. The function should return a new numpy array that is a posterized version of the input image. A posterized image has only a few colors, in this case 2. The function should use the average value of each pixel to determine what the output color of the pixel is. If the average is above threshold, the output pixel color should be light_color. If the average is below threshold, the output pixel color should be dark_color. The function should not use any loops. Again, test the function with a small test array and printing the result before testing on a larger image and writing the result to a file.

Example

Test Code Output
print(posterize(numpy.array([[[0, 255, 0], [255, 255, 0]]])), 127.5, numpy.array([50, 200, 50]), numpy.array(200, 50, 200)) [[[ 50. 200. 50.][ 200. 50. 200.]]]

Submission

Please show your source code and run your programs. Only programs that have perfect functionality will be accepted as complete.