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 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_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 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 count_stars(pic, intensity): '''Count stars in Picture pic brighter than int intensity.''' count = 0 for pixel in pic: if get_intensity(pixel) > intensity: count += 1 x = media.get_x(pixel) y = media.get_y(pixel) flood_fill(pic, x, y, intensity, media.darkblue) return count
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 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 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 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 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 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