def distance(Pixel_1, Pixel_2): ''' Return an integer value indicating the difference between the two pixels Pixel_1 and Pixel_2)''' #find the difference b/w values of pixel 2 and pixel 1 and sum the #difference of R, G and B, then return the total diff. distance_red = abs(media.get_red(Pixel_1) - media.get_red(Pixel_2)) distance_green = abs(media.get_green(Pixel_1) - media.get_green(Pixel_2)) distance_blue = abs(media.get_blue(Pixel_1) - media.get_blue(Pixel_2)) distance_total = distance_red + distance_green + distance_blue return distance_total
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 overlay_color(pix1, pix2): '''(Pix1, Pix2) -> Color Return a new color made up of 80% of the color values of the first pixel and 20% of the color values of the second pixel.''' r1 = int(media.get_red(pix1)) r2 = int(media.get_red(pix2)) g1 = int(media.get_green(pix1)) g2 = int(media.get_green(pix2)) b1 = int(media.get_blue(pix1)) b2 = int(media.get_blue(pix2)) red = (r1 * 0.8) + (r2 * 0.2) green = (g1 * 0.8) + (g2 * 0.2) blue = (b1 * 0.8) + (b2 * 0.2) return media.create_color(red, green, blue)
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 scale_blue(pic, new_blue): '''Set the average blue of a Picture pic to a new average new_blue''' average_blue = blue_average(pic) pixel_ratio = float(new_blue) / average_blue for pixel in pic: blue = media.get_blue(pixel) target_blue = pixel_ratio * blue if target_blue > 255.0: target_blue = 255 change_blue = media.set_blue(pixel, int(target_blue))
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 blue_average(pic): ''' Return an average blue value of all the pixels in the Picture pic as an integer. Truncate the result if the average calculation yields a non-integer value.''' sum_blue = 0 total_pix = 0 for pixel in pic: sum_blue += media.get_blue(pixel) total_pix += 1 average_blue = sum_blue/total_pix return average_blue
def scale_blue(pic, new_blue_average): ''' Take the Picture pic and set the average value of all blue pixels in the picture to the Integer new_blue_average. Return scaled Picture Assume that the picture will have some colour component in it''' old_average = float(blue_average(pic)) for pixel in pic: new_blue = (new_blue_average * media.get_blue(pixel)) / old_average if new_blue > 255: new_blue = 255 media.set_blue(pixel, int(new_blue)) return pic
#encoding: utf8 import media pic = media.load_picture('207.jpg') media.show(pic) for p in media.get_pixels(pic): new_blue = int(0.7 * media.get_blue(p)) new_green = int(0.7 * media.get_green(p)) media.set_blue(p, new_blue) media.set_green(p, new_green) media.show(pic)
def test_above_freezing(): '''Test function for above_freezing.''' pass # we'll fill this in too if __name__ == '__main__': nose.runmodule() import media pic = media.load_picture('pic207.jpg') media.show(pic) for p in media.get_pixels(pic): new_blue = int(0.7 * media.get_blue(p)) new_green = int(0.7 * media.get_green(p)) media.set_blue(p, new_blue) media.set_green(p, new_green) media.show(pic) def to_celsius(t): return (t - 32.0) * 5.0 / 9.0 def above_freezing(t):
def get_intensity(pixel): '''Return the average of the RGB components of Pixel pixel.''' return (media.get_blue(pixel) + media.get_blue(pixel) + \ media.get_blue(pixel)) / 3
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) # the colours should have changed in our pixel
import media f = media.choose_file() pic = media.load_picture(f) for p in media.get_pixels(pic): r = media.get_red(p) g = media.get_green(p) b = media.get_blue(p) gray = (g + b + r ) / 3 media.set_green(gray) media.set_blue(gray) media.set_red(gray)
import media if __name__ == '__main__': filename = media.choose_file() pic = media.load_picture(filename) media.show(pic) # Reduce the blue and green by 30% each to make # the red stand out. for pixel in pic: value = media.get_blue(pixel) new_blue = int(value * 0.7) media.set_blue(pixel, new_blue) value = media.get_green(pixel) new_green = int(value * 0.7) media.set_green(pixel, new_green) media.show(pic)