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 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 simple_difference(pic_1, pic_2): ''' Return an integer value indicating the difference between two Pictures, pic_1 and pic_2, with the same dimensions''' sum_diff = 0 #sum up all the distances for the corresponding pixels in the two pictures. #(add up all RGB value differences between pixels in the two pics) for pixel in pic_1: x_coordinate = media.get_x(pixel) y_coordinate = media.get_y(pixel) sum_diff += distance(media.get_pixel(pic_1, x_coordinate, y_coordinate), media.get_pixel(pic_2, x_coordinate, y_coordinate)) return sum_diff
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 test_get_pixel_bottom_right(self): """Test getting the bottom left pixel.""" pic = media.create_picture(WIDTH, HEIGHT) p = media.get_pixel(pic, WIDTH - 1, HEIGHT - 1) self.assert_(media.get_x(p) == WIDTH - 1) self.assert_(media.get_y(p) == HEIGHT - 1)
def test_get_pixel_top_left(self): """Test getting the top left pixel.""" pic = media.create_picture(WIDTH, HEIGHT) p = media.get_pixel(pic, 0, 0) self.assert_(media.get_x(p) == 0) self.assert_(media.get_y(p) == 0)
def reduce_width(pic, reducing_factor): ''' Take Picture pic and return a duplicate of it that is horizontally compressed by an Integer reducing_factor''' # Create a new Picture with the appropriate old height and new width, and # initialize the colour to black (all colour components are zero). new_width = (pic.get_width() - 1) / reducing_factor + 1 new_height = pic.get_height() newpic = media.create_picture(new_width, new_height, media.black) # Iterate through all the Pixels in the large image, and copy # a portion of that Pixel's colour components into the correct # Pixel position in the smaller image. for pixel in pic: # Find the corresponding Pixel in the new Picture. x_coordinate = media.get_x(pixel)/reducing_factor; y_coordinate = media.get_y(pixel); newpixel = media.get_pixel(newpic, x_coordinate, y_coordinate) # Add the appropriate fraction of this Pixel's colour components # to the components of the corresponding Pixel in the new Picture. new_red = newpixel.get_red() + pixel.get_red()/reducing_factor new_blue = newpixel.get_blue() + pixel.get_blue()/reducing_factor new_green = newpixel.get_green() + pixel.get_green()/reducing_factor media.set_red(newpixel, int(new_red)) media.set_blue(newpixel, int(new_blue)) media.set_green(newpixel, int(new_green)) return newpic
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 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 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 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 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 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 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 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 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 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 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 simple_difference(pic, pic2): '''Return an int that is the sum of the differences between each pixel in pic and pic2.''' sum_difference = 0 for pixel in pic: x_pic = media.get_x(pixel) y_pic = media.get_y(pixel) pixel2 = media.get_pixel(pic2, x_pic, y_pic) difference = distance(pixel, pixel2) sum_difference += difference return sum_difference
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 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 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 expand_width(pic, expand_factor): '''Create a new picture new_pic that has an expanded width of pic by a factor of expand_factor.''' height = media.get_height(pic) width = expand_factor * media.get_width(pic) new_pic = media.create_picture(width, height, media.black) for pixel in new_pic: x = media.get_x(pixel) y = media.get_y(pixel) new_pixel = media.get_pixel(pic, x / expand_factor, y ) new_red = new_pixel.get_red() + pixel.get_red() new_blue = new_pixel.get_blue() + pixel.get_blue() new_green = new_pixel.get_green() + pixel.get_green() media.set_red(pixel, new_red) media.set_blue(pixel, new_blue) media.set_green(pixel, new_green) return new_pic
def reduce_width(pic, factor): '''Create a new picture newpic that has a reduced width of pic by a factor of factor.''' new_height = pic.get_height() new_width = (pic.get_width() + factor - 1) / factor newpic = media.create_picture(new_width, new_height, media.black) for pixel in pic: x = media.get_x(pixel) y = media.get_y(pixel) newpixel = media.get_pixel(newpic, x/factor, y) new_red = newpixel.get_red() + pixel.get_red()/factor new_blue = newpixel.get_blue() + pixel.get_blue()/factor new_green = newpixel.get_green() + pixel.get_green()/factor media.set_red(newpixel, new_red) media.set_blue(newpixel, new_blue) media.set_green(newpixel, new_green) return newpic
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)
assert isinstance(result, media.Picture), \ '''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
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)
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 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()
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)
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)