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 pic_process(): pic = media.load_image("/home/auroua/workspace/lena.jpg") width,height = media.get_width(pic),media.get_height(pic) for x in range(0,height,2): for y in range(0,width+1): p = media.get_pixel(pic,x,y) media.set_color(p,media.black) media.show(pic)
def test_3_white_pixels_1_black_pixel(): """Test if average_brightness calculates the correct value for a 2x2 picture consisting of 3 white pixels and 1 black pixel.""" new_pic = media.create_picture(2, 2, media.white) media.set_color(media.get_pixel(new_pic, 0, 0), media.black) assert ( average_brightness.average_brightness(new_pic) == 255 * 3 / 4.0 ), "The expected average brightness for a picture with 3 white 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 rotate_left180_right180 (pic): '''Return new_pic, a picture created by rotating picture pic 180 degrees.''' width = pic.get_width() height = pic.get_height() new_pic = media.create_picture(width, height, media.white) for pix in pic: media.set_color(media.get_pixel(new_pic, width - pix.get_x() - 1, \ height - pix.get_y() - 1), pix.get_color()) return new_pic
def horizontal_reflection (pic): '''Create and return a copy of the picture pic which appears as a horizontal reflection of pic.''' width = pic.get_width() height = pic.get_height() new_pic = media.create_picture(width, height, media.white) for pix in pic: media.set_color(media.get_pixel(new_pic, width - pix.get_x() - 1, \ pix.get_y()), pix.get_color()) return new_pic
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 rotate_left90_right270 (pic): '''Return new_pic, a picture created by rotating picture pic 90 degrees to the left.''' width = pic.get_width() height = pic.get_height() new_pic = media.create_picture(height, width, media.white) for pix in pic: media.set_color(media.get_pixel(new_pic, pix.get_y(), \ width - pix.get_x() - 1), pix.get_color()) return new_pic
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 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 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 overlay_picture(pic, pic2): '''(Pic, Pic2) -> Picture Return a new picture with each pixel's color values made up of 80% of the color values of the corresponding pixel in the first picture and 20% of the color values of the corresponding pixel in the second picture''' new_pic=media.copy(pic) for pix in new_pic: x = media.get_x(pix) y = media.get_y(pix) p1 = media.get_pixel(pic, x,y) p2 = media.get_pixel(pic2,x,y) red = (int(p1.get_red()) * 0.8) + (int(p2.get_red()) * 0.2) green = (int(p1.get_green()) * 0.8) + (int(p2.get_green()) * 0.2) blue = (int(p1.get_blue()) * 0.8) + (int(p2.get_blue()) * 0.2) new_color = media.create_color(red, green, blue) media.set_color(pix, new_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
def overlay_picture(pic, pic2): '''(Pic, Pic2) -> Picture Return a new picture with each pixel's color values made up of 80% of the color values of the corresponding pixel in the first picture and 20% of the color values of the corresponding pixel in the second picture''' new_pic = media.copy(pic) for pix in new_pic: x = media.get_x(pix) y = media.get_y(pix) p1 = media.get_pixel(pic, x, y) p2 = media.get_pixel(pic2, x, y) red = (int(p1.get_red()) * 0.8) + (int(p2.get_red()) * 0.2) green = (int(p1.get_green()) * 0.8) + (int(p2.get_green()) * 0.2) blue = (int(p1.get_blue()) * 0.8) + (int(p2.get_blue()) * 0.2) new_color = media.create_color(red, green, blue) media.set_color(pix, new_color) return new_pic
def flood_fill(pic, x, y, intensity, c): '''Set all pixels in Picture pic brighter than int intensity and connected to x, y to Color c. Preconsition: c's intensity < intensity.''' pixel = media.get_pixel(pic, x, y) # Only process the pixel if it's bright. if get_intensity(pixel) > intensity: media.set_color(pixel, c) if 0 < x: flood_fill(pic, x - 1, y, intensity, c) if x < media.get_width(pic) - 1: flood_fill(pic, x + 1, y, intensity, c) if 0 < y: flood_fill(pic, x, y - 1, intensity, c) if x < media.get_height(pic) - 1: flood_fill(pic, x, y + 1, intensity, c)
import media lake = media.load_picture('lake.png') width, height = media.get_width(lake), media.get_height(lake) for y in range(0, height, 2): # Skip odd-numbered lines for x in range(0, width): p = media.get_pixel(lake, x, y) media.set_color(p, media.black) media.show(lake)
'''a1.strikethrough(pic) should return a Picture, but it returned %s.''' % (type(result)) # Type check a1.widen pic = media.create_picture(30, 20) result = a1.widen(pic) assert isinstance(result, media.Picture), \ '''a1.widen(pic) should return a Picture, but it returned %s.''' % (type(result)) # Type check a1.overlay_color pic = media.create_picture(2, 3) pix1 = media.get_pixel(pic, 0, 0) pix2 = media.get_pixel(pic, 1, 1) media.set_color(pix1, media.orange) media.set_color(pix2, media.blue) result = a1.overlay_color(pix1, pix2) assert isinstance(result, media.Color), \ '''a1.overlay_color(pix1, pix2) should return a Color, but it returned %s.''' % (type(result)) # Type check a1.overlay_picture pic1 = media.create_picture(2, 3) pix1 = media.get_pixel(pic, 0, 0) pix2 = media.get_pixel(pic, 1, 1) r1, g1, b1 = 50, 100, 200 r2, g2, b2 = 40, 150, 0 media.set_color(pix1, media.create_color(r1, g1, b1)) media.set_color(pix2, media.create_color(r2, g2, b2))
# -*- 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)
import media import color pic = media.create_picture(100, 200, media.black) for i in range(media.get_width(pic)): for j in range(media.get_height(pic)): pixel = media.get_pixel(pic, i, j) media.set_color(pixel, media.create_color(i % 255, j % 255, 0)) pic.show()
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)
def turn_stars_blue(pic, intensity): '''Find stars in Picture pic brighter than int intensity and turn them blue.''' for pixel in pic: if get_intensity(pixel) > intensity: media.set_color(pixel, media.blue)
def setColor(pic, x, y, col): if len(col) == 3: clr = color.Color(col[0], col[1], col[2]) if getWidth(pic) > x and getHeight(pic) > y: pix = media.get_pixel(pic, x, y) media.set_color(pix, clr)
for c in 'alpha': print(c) # ... # a # l # p # h # a import media lake = media.load_picture('lake.png') width, height = media.get_width(lake), media.get_height(lake) for y in range(0, height, 2): # Skip odd-numbered lines for x in range(0, width): p = media.get_pixel(lake, x, y) media.set_color(p, media.black) media.show(lake) 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)