def same_dimensions(): filename = media.choose_file() pic = media.load_picture(filename) filename2 = media.choose_file() pic2 = media.load_picture(filename2) h1 = media.get_height(pic) w1 = media.get_width(pic) h2 = media.get_height(pic2) w2 = media.get_width(pic2) if h1 == h2 and w1 == w2: print "true" else: print "false"
def same_dimensions(): filename = media.choose_file() pic = media.load_picture(filename) filename2 = media.choose_file() pic2 = media.load_picture(filename2) h1=media.get_height(pic) w1=media.get_width(pic) h2=media.get_height(pic2) w2=media.get_width(pic2) if h1==h2 and w1==w2: print "true" else: print "false"
def smart_difference(pic_1, pic_2): ''' Return an integer value indicating the difference between two Pictures, pic_1 and pic_2, with different dimensions and colors. Assume that the heights and widths of the two pictures are integer multiples of each other.''' #if the height of one pic is larger than the other, we create a new pic #that is the same as the first one but with reduced height. Otherwise, we #make a pic that is the same as pic_1, but this time, stretched. height_pic_1 = media.get_height(pic_1) width_pic_1 = media.get_width(pic_1) height_pic_2 = media.get_height(pic_2) width_pic_2 = media.get_width(pic_2) if height_pic_1 > height_pic_2: height_factor = float(height_pic_1 / height_pic_2) shorter_pic = reduce_height(pic_1, height_factor) else: height_factor = height_pic_2 / height_pic_1 shorter_pic = expand_height(pic_1, height_factor) width_shorter_pic = media.get_width(shorter_pic) if width_shorter_pic > width_pic_2: width_factor = width_shorter_pic / width_pic_2 adjusted_pic = reduce_width(shorter_pic, width_factor) else: width_factor = width_pic_2 / width_shorter_pic adjusted_pic = expand_width(shorter_pic, width_factor) #scale the new pic we have based on the average colours of pic_2 scale_red_to = red_average(pic_2) scale_green_to = green_average(pic_2) scale_blue_to = blue_average(pic_2) adjusted_pic = scale_red(adjusted_pic, scale_red_to) adjusted_pic = scale_green(adjusted_pic, scale_green_to) adjusted_pic = scale_blue(adjusted_pic, scale_blue_to) scale_red_to = red_average(adjusted_pic) scale_green_to = green_average(adjusted_pic) scale_blue_to = blue_average(adjusted_pic) #run simple_difference function between new pic and pic_2 true_difference = simple_difference(adjusted_pic, pic_2) return true_difference
def smart_difference(pic, pic2): '''Return the simple_difference of two pictures after setting the dimensions of the pictures equal and scaling their averages to be the same.''' height = media.get_height(pic) width = media.get_width(pic) height2 = media.get_height(pic2) width2 = media.get_width(pic2) size = height * width size2 = height2 * width2 if height >= height2 and width >= width2: factor_h = height / height2 factor_w = width / width2 picnew = reduce_height(pic, factor_h) pic1new = reduce_width(picnew, factor_w) pic2new = pic2 elif height <= height2 and width >= width2: factor_h = height2 / height factor_w = width / width2 pic1new = reduce_height(pic2, factor_h) pic2new = reduce_width(pic, factor_w) elif height >= height2 and width <= width2: factor_h = height / height2 factor_w = width2 / width pic1new = reduce_height(pic, factor_h) pic2new = reduce_width(pic2, factor_w) elif height <= height2 and width <= width2: factor_h = height2 / height factor_w = width2 / width picnew = reduce_height(pic2, factor_h) pic2new = reduce_width(picnew, factor_w) pic1new = pic # Scale the RGB values of pic2new to those of pic1new red = red_average(pic1new) blue = blue_average(pic1new) green = green_average(pic1new) scale_red(pic2new, red) scale_blue(pic2new, blue) scale_green(pic2new, green) # Use simple_difference to calculate and return an int. simple_d = simple_difference(pic1new, pic2new) return simple_d
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 blue_average(pic): '''Returns the average blue value of the entire Picture pic as an int.''' sum_blue = 0 height = media.get_height(pic) width = media.get_width(pic) total_pixel = height * width for pixel in pic: blue = media.get_blue(pixel) sum_blue += blue average = sum_blue / total_pixel return int(average)
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 green_average(pic): '''Returns the average green value of the entire Picture pic as an int.''' sum_green = 0 height = media.get_height(pic) width = media.get_width(pic) total_pixel = height * width for pixel in pic: green = media.get_green(pixel) sum_green += green average = sum_green / total_pixel return int(average)
def mirror_horizontal(pic): '''Create and return a copy of the picture pic which has its bottom half appear as a mirror of its top half. This code is based on CSC108 LEC5101 Week 5 Wednesday notes.''' new_pic = pic.copy() width = media.get_width(new_pic) height = media.get_height(new_pic) middle = height / 2 for x in range(width): for y in range(middle): mirror_pixels(new_pic, x, y, x, height - 1 - y) return new_pic
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 mirror_vertical(pic): '''Create and return a copy of the picture pic which has its right half appear as a mirror of its left half. This code is based on CSC108 LEC5101 Week 5 Wednesday notes.''' new_pic = pic.copy() width = media.get_width(new_pic) height = media.get_height(new_pic) middle = width / 2 for x in range(middle): for y in range(height): mirror_pixels(new_pic, x, y, width - 1 - x, y) 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 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 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)
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_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."
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 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)
def getWidth(pic): return media.get_width(pic)
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)