import media
# Create a gray scale image
def gray_scale(picture): new_pic = media.duplicatePicture(picture) for pixel in media.getPixels(new_pic):
# Average the brightness (luminance) of all color channels # and preserve the overall brightness of the pixel
intensity = (media.getRed(pixel) + media.getGreen(pixel) + media.getBlue(pixel)) // 3
# Set all values to the same value (makes black, white, or gray)
media.setColor(pixel, media.makeColor(intensity, intensity, intensity)) return new_pic
# Create a gray scale image using weights
def weighted_gray_scale(picture): new_pic = media.duplicatePicture(picture) for pixel in media.getPixels(new_pic):
# We reduce each channel by a weighted value # the total of all weights is 1 # models the way the human eye perceives # brightness.
new_red = media.getRed(pixel) * 0.229 new_green = media.getGreen(pixel) * 0.587 new_blue = media.getBlue(pixel) * 0.114 intensity = new_red + new_green + new_blue media.setColor(pixel, media.makeColor(intensity, intensity, intensity)) return new_pic
# Accessing an array of pixels produced by # media.getPixels using the index operator []
def decrease_red(picture): new_pix = media.duplicatePicture(picture) pixels = media.getPixels(new_pix) for index in range(len(pixels)):
# Access the pixel at the index
pixel = pixels[index] red_value = media.getRed(pixel) media.setRed(pixel, red_value * 0.5) return new_pix
# Reduce the red value for the top half of a picture
def decrease_red_top_half(picture): new_pix = media.duplicatePicture(picture) pixels = media.getPixels(new_pix)
# Get a range from zero (the first pixel index) # to index of the middle pixel in the list
for index in range(len(pixels) // 2): pixel = pixels[index] red_value = media.getRed(pixel) media.setRed(pixel, red_value * 0.5) return new_pix
# Reduce the red for the lower half of a picture
def decrease_red_bottom_half(picture): new_pix = media.duplicatePicture(picture) pixels = media.getPixels(new_pix)
# Get a range starting at the middle pixel index # and ending at the last pixel index in the list
for index in range(len(pixels) // 2, len(pixels)): pixel = pixels[index] red_value = media.getRed(pixel) media.setRed(pixel, red_value * 0.5) return new_pix
# Duplicate the top half of an image to the bottom half
def copy_top_half(picture): new_pix = media.duplicatePicture(picture) pixels = media.getPixels(new_pix)
# Get a range from zero (the first pixel index) up to # the middle pixel index in the list
for index in range(len(pixels) // 2): source_pixel = pixels[index] source_color = media.getColor(source_pixel)
# THIS IS IMPORTANT # We get the pixel we need to change # by adding the index of the middle pixel to our current index # In our first loop index will be 0 that means we will copy our # pixel information to the pixel at location (0 + middle index) # Each subsequent loop will edit an additional pixel AFTER the # middle of the image.
target_pixel = pixels[index + len(pixels)//2] media.setColor(target_pixel, source_color) return new_pix
# Reflect the top half of the image to the bottom half
def mirror_half(picture): new_pix = media.duplicatePicture(picture) pixels = media.getPixels(new_pix)
# We will use this variable to keep track of where # we want to copy our pixel data in the list. # We start by targeting the last pixel in the list
target = len(pixels) - 1
# We will make a range from 0 (the index of the first # pixel in the list) up to the middle pixel index.
for index in range(len(pixels)//2): source_pixel = pixels[index] source_color = media.getColor(source_pixel) target_pixel = pixels[target] media.setColor(target_pixel,source_color)
# We reduce our target index by one to move # backward in the list (towards the 0 index).
target = target - 1 return new_pix
# Write the code you want to run and your function calls # below your function definitions.
filename = 'llama.jpg' picture = media.makePicture(filename) modified_picture = gray_scale(picture) media.show(modified_picture) modified_picture = weighted_gray_scale(picture) media.show(modified_picture) modified_picture = decrease_red(picture) media.show(modified_picture) modified_picture = decrease_red_top_half(picture) media.show(modified_picture) modified_picture = decrease_red_bottom_half(picture) media.show(modified_picture) modified_picture = copy_top_half(picture) media.show(modified_picture) modified_picture = mirror_half(picture) media.show(modified_picture) media.quit()