def distance(Pixel_1, Pixel_2): ''' Return an integer value indicating the difference between the two pixels Pixel_1 and Pixel_2)''' #find the difference b/w values of pixel 2 and pixel 1 and sum the #difference of R, G and B, then return the total diff. distance_red = abs(media.get_red(Pixel_1) - media.get_red(Pixel_2)) distance_green = abs(media.get_green(Pixel_1) - media.get_green(Pixel_2)) distance_blue = abs(media.get_blue(Pixel_1) - media.get_blue(Pixel_2)) distance_total = distance_red + distance_green + distance_blue return distance_total
def overlay_color(pix1, pix2): '''(Pix1, Pix2) -> Color Return a new color made up of 80% of the color values of the first pixel and 20% of the color values of the second pixel.''' r1 = int(media.get_red(pix1)) r2 = int(media.get_red(pix2)) g1 = int(media.get_green(pix1)) g2 = int(media.get_green(pix2)) b1 = int(media.get_blue(pix1)) b2 = int(media.get_blue(pix2)) red = (r1 * 0.8) + (r2 * 0.2) green = (g1 * 0.8) + (g2 * 0.2) blue = (b1 * 0.8) + (b2 * 0.2) return media.create_color(red, green, blue)
def moderate_blue(pic): new_pic=media.copy(pic) for pix in new_pic: red=media.get_red(pix) green=media.get_green(pix) red1=int(red) green1=int(green) modblue=(red1+green1)/2 media.set_blue(pix, modblue) media.show(new_pic) return new_pic
def test_pixel_set_get_RGB(self): """Test setting and getting the red, green, and blue of a pixel.""" pic = media.create_picture(WIDTH, HEIGHT) p = media.get_pixel(pic, WIDTH - 1, HEIGHT - 1) media.set_red(p, 1) self.assert_(media.get_red(p) == 1) media.set_green(p, 2) self.assert_(media.get_green(p) == 2) media.set_blue(p, 3) self.assert_(media.get_blue(p) == 3)
def moderate_blue(pic): new_pic = media.copy(pic) for pix in new_pic: red = media.get_red(pix) green = media.get_green(pix) red1 = int(red) green1 = int(green) modblue = (red1 + green1) / 2 media.set_blue(pix, modblue) media.show(new_pic) return new_pic
def scale_red(pic, new_red): '''Set the average red of a Picture pic to a new average new_red''' average_red = red_average(pic) pixel_ratio = float(new_red)/ average_red for pixel in pic: red = media.get_red(pixel) target_red = pixel_ratio * red if target_red > 255.0: target_red = 255 change_red = media.set_red(pixel, int(target_red))
def red_average(pic): ''' Return an average red value of all the pixels in the Picture pic as an integer. Truncate the result if the average calculation yields a non-integer value.''' sum_red = 0 total_pix = 0 for pixel in pic: sum_red += media.get_red(pixel) total_pix += 1 average_red = (sum_red/total_pix) return average_red
def scale_red(pic, new_red_average): ''' Take the Picture pic and set the average value of all red pixels in the picture to the Integer new_average. Return scaled picture. Assume that the picture will have some colour component in it''' old_average = float(red_average(pic)) for pixel in pic: new_red = (new_red_average / old_average) * media.get_red(pixel) if new_red > 255: new_red = 255 media.set_red(pixel, int(new_red)) return pic
def red_average(pic): '''Returns the average red value of the entire Picture pic as an int.''' sum_red = 0 height = media.get_height(pic) width = media.get_width(pic) total_pixel = height * width for pixel in pic: red = media.get_red(pixel) sum_red += red average = sum_red / total_pixel return int(average)
def chromakey(person, background): '''Replace blue pixels in the person picture with the corresponding pixels in the background picture. The pictures must have the same dimensions.''' for pixel in person: # If the blue dominates the pixel, replace its colour with the colour of # the corresponding pixel from the background picture. if media.get_blue(pixel) > media.get_green(pixel) and \ media.get_blue(pixel) > media.get_red(pixel): x = media.get_x(pixel) y = media.get_y(pixel) background_px = media.get_pixel(background, x, y) media.set_color(pixel, media.get_color(background_px))
import media import random # make a new 100 by 100 picture pic = media.create_picture(100, 100) # get 2 random numbers between 0 and 99 to use as coordinates x = random.randint(0, 99) y = random.randint(0, 99) # get the pixel at this x,y coordinate pix = media.get_pixel(pic, x, y) # get the red, blue and green values of this pixel red = media.get_red(pix) green = media.get_green(pix) blue = media.get_blue(pix) # introduce a new colour new_color = media.orange # make a 10 x 10 rectangle of the new colour inside our # picture, starting with our x and y as the upper # left corner. (In this case, it doesn't matter if some # of the rectangle is outside the picture, as long as # the x,y corner is inside.) media.add_rect_filled(pic, x, y, 10, 10, new_color) # display the picture media.show(pic)
# -*- coding: utf-8 -*- import media pic = media.load_picture(media.choose_file()) for p in media.get_pixels(pic): new_red = int(2 * media.get_red(p)) media.set_red(p, new_red) media.show(pic)
import media f = media.choose_file() pic = media.load_picture(f) for p in media.get_pixels(pic): r = media.get_red(p) g = media.get_green(p) b = media.get_blue(p) gray = (g + b + r ) / 3 media.set_green(gray) media.set_blue(gray) media.set_red(gray)