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.''' colour = media.get_color(media.get_pixel(person, 60, 60)) for p in person: # 42 is a guess. Other values might work much better. if media.distance(media.get_color(p), colour) < 42: x = media.get_x(p) y = media.get_y(p) background_px = media.get_pixel(background, x, y) media.set_color(p, media.get_color(background_px))
def getColor(pic, x, y): if getWidth(pic) > x and getHeight(pic) > y: pix = media.get_pixel(pic, x, y) clr = media.get_color(pix) return list(clr.get_rgb()) else: return None
def flip(pic): '''(Pic) -> Picture Return a new picture that contains the pixels of the original picture flipped across the vertical axis.''' copy = media.copy(pic) max_x = media.get_width(pic) max_y = media.get_height(pic) for x in range(max_x/2): for y in range(max_y): originalpix = copy.get_pixel(x,y) reversepix = copy.get_pixel((max_x - x - 1), y) color = media.get_color(originalpix) reversecolor = media.get_color(reversepix) media.set_color(originalpix,reversecolor) media.set_color(reversepix,color) return copy
def flip(pic): '''(Pic) -> Picture Return a new picture that contains the pixels of the original picture flipped across the vertical axis.''' copy = media.copy(pic) max_x = media.get_width(pic) max_y = media.get_height(pic) for x in range(max_x / 2): for y in range(max_y): originalpix = copy.get_pixel(x, y) reversepix = copy.get_pixel((max_x - x - 1), y) color = media.get_color(originalpix) reversecolor = media.get_color(reversepix) media.set_color(originalpix, reversecolor) media.set_color(reversepix, color) return copy
def test_pixel_set_get_color(self): """Test setting and getting the color of a pixel.""" pic = media.create_picture(WIDTH, HEIGHT) p = media.get_pixel(pic, WIDTH - 1, HEIGHT - 1) c = media.aqua media.set_color(p, c) self.assert_(media.get_color(p) == media.aqua)
def count_black(pic): '''(Pic) -> int Returns the quantity of pixels in pic that are black as an integer.''' black_pixels = 0 for pix in pic: if media.get_color(pix) == media.black: black_pixels += 1 return black_pixels
def count_black(pic): '''(Pic) -> int Returns the quantity of pixels in pic that are black as an integer.''' black_pixels=0 for pix in pic: if media.get_color(pix) == media.black: black_pixels += 1 return black_pixels
def mirror_pixels(pic, x1, y1, x2, y2): '''Set the colour of the pixel (x2, y2) to the colour of pixel (x1, y1) in picture pic. This code is based on CSC108 LEC5101 Week 5 Wednesday notes.''' #This is a helper function pix = media.get_pixel(pic, x1, y1) pix2 = media.get_pixel(pic, x2, y2) col = media.get_color(pix) media.set_color(pix2, col)
def test_crop_image(): '''Test the function crop_image in img_manip.''' new_pic = create_pic(4, 4) new_pic_cropped = img_manip.crop_image(new_pic, 1, 1, 2, 2) #Test if the colour of each pixel in new_pic is equal to the colour of the #corresponding pixel in new_pic_cropped. If not, the boolean cropped is #made False. cropped = True for x in range(2): for y in range(2): new_pic_pix = media.get_pixel(new_pic, x + 1, y + 1) new_pic_cropped_pix = media.get_pixel(new_pic_cropped, x, y) col1 = media.get_color(new_pic_pix) col2 = media.get_color(new_pic_cropped_pix) if col1 != col2: cropped = False assert cropped, \ "crop_image failed to crop the picture correctly."
def test_rotate_left270_right90(): '''Test the function rotate_left270_right90 in img_manip.''' new_pic = create_pic(4, 4) new_pic_rotated = img_manip.rotate_left270_right90(new_pic) #Test if the colour of each pixel in new_pic is equal to the colour of the #corresponding pixel in new_pic_rotated. If not, the boolean rotated is #made False. rotated = True for x in range(4): for y in range(4): new_pic_pix = media.get_pixel(new_pic, x, y) new_pic_rotated_pix = media.get_pixel(new_pic_rotated, 3 - y, x) col1 = media.get_color(new_pic_pix) col2 = media.get_color(new_pic_rotated_pix) if col1 != col2: rotated = False assert rotated, \ "rotate_left270_right90 failed to rotate the picture correctly."
def test_horizontal_reflection(): '''Test the function horizontal_reflection in img_manip.''' new_pic = create_pic(4, 4) new_pic_reflected = img_manip.horizontal_reflection(new_pic) #Test if the colour of each pixel in new_pic_horizontal_reflection is equal to #the colour of the corresponding pixel in new_pic. If not, the boolean #reflected is made False. reflected= True width = media.get_width(new_pic) height = media.get_height(new_pic) for x in range(width): for y in range(height): pix1 = media.get_pixel(new_pic, x, y) pix2 = media.get_pixel(new_pic_reflected, width - x - 1, y) col1 = media.get_color(pix1) col2 = media.get_color(pix2) if col1 != col2: reflected = False assert reflected, \ "horizontal_reflection failed to reflect the image horizontally."
def test_create_pic_color(self): """Create a WIDTH by HEIGHT picture and check for proper dimensions and color.""" pic = media.create_picture(WIDTH, HEIGHT, media.magenta) width = media.get_width(pic) height = media.get_height(pic) self.assert_(width == WIDTH, 'expected pic width of %s, saw %s' % (WIDTH, width)) self.assert_(height == HEIGHT, 'expected pic width of %s, saw %s' % (HEIGHT, height)) for p in pic: self.assert_(media.get_color(p) == media.magenta)
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))
def test_mirror_horizontal(): '''Test the function mirror_horizontal in img_manip.''' new_pic = create_pic(4, 4) new_pic_mirror_horizontal = img_manip.mirror_horizontal(new_pic) #Test if the colour in each pixel in new_pic_mirror_horizontal is equal to #the colour of the pixel that is its horizontal mirror. If not, the boolean #mirrored is made False. mirrored = True width = media.get_width(new_pic_mirror_horizontal) height = media.get_height(new_pic_mirror_horizontal) middle = width / 2 for x in range(width): for y in range(middle): pix1 = media.get_pixel(new_pic_mirror_horizontal, x, y) pix2 = media.get_pixel(new_pic_mirror_horizontal, x, height - y - 1) col1 = media.get_color(pix1) col2 = media.get_color(pix2) if col1 != col2: mirrored = False assert mirrored, \ "mirror_vertical failed to mirror the picture vertically."
def expand_width(pic, expanding_factor): ''' Take Picture pic and return a duplicate of it that is horizontally stretched by an Integer expanding_factor''' new_width = (pic.get_width()) * expanding_factor new_height = pic.get_height() newpic = media.create_picture(new_width, new_height, media.black) for pixel in newpic: x_coordinate = media.get_x(pixel) / expanding_factor y_coordinate = media.get_y(pixel) newpixel = media.get_pixel(pic, x_coordinate, y_coordinate) new_color = media.get_color(newpixel) media.set_color(pixel, new_color) return newpic
def crop_image (pic, x1, y1, x2, y2): '''Crop from the picture pic a rectangular area from (x1, y1) to (x2, y2), and return the area as a picture.''' top_left_x = min(x1, x2) top_left_y = min(y1, y2) btm_right_x = max(x1, x2) btm_right_y = max(y1, y2) new_pic = media.create_picture(abs(x1 - x2) + 1, abs(y1 - y2) + 1) for row in range(top_left_x, btm_right_x + 1): for column in range(top_left_y, btm_right_y + 1): x = row - top_left_x y = column - top_left_y pix = media.get_pixel(new_pic, x, y) color = media.get_color(media.get_pixel(pic, row, column)) media.set_color(pix, color) return new_pic
def expand_height(pic, expanding_factor): ''' Take Picture pic and return a duplicate of it that is vertically stretched by an Integer expanding_factor''' new_width = pic.get_width() new_height = pic.get_height() * expanding_factor #create a new pic with new width, height and color black newpic = media.create_picture(new_width, new_height, media.black) #in new pic, align the x/y coordinate to that of old pic, get pixel and #use that pixel to get its color and set it as the new color for new pic for pixel in newpic: x_coordinate = media.get_x(pixel) y_coordinate = media.get_y(pixel) / expanding_factor newpixel = media.get_pixel(pic, x_coordinate, y_coordinate) new_color = media.get_color(newpixel) media.set_color(pixel, new_color) return newpic
import media baseball = media.load_picture('baseball.png') lake = media.load_picture('lake.png') width, height = media.get_width(baseball), media.get_height(baseball) for y in range(0, height): for x in range(0, width): # Position the top-left of the baseball at (50, 25) from_p = media.get_pixel(baseball, x, y) to_p = media.get_pixel(lake, 50 + x, 25 + y) media.set_color(to_p, media.get_color(from_p)) media.show(lake)
# -*- coding: utf-8 -*- import media pic = media.load_picture(media.choose_file()) for p in pic: c = media.get_color(p) c.r = c.g = c.b = (c.r + c.g + c.b) / 3 media.set_color(p, c) media.show(pic)