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 word_to_pic(pic_word): '''(pic_word) -> Picture Return a picture that contains a string.''' p = media.create_picture(get_picture_width(pic_word), 20) p.add_text(media.black, 0, 0, pic_word) return p
def copyright(): pic = media.create_picture(20, 20) black = media.black media.add_oval(pic, 0, 0, 16, 16, black) media.add_text(pic, 6, 3, "C", black) media.show(pic) return pic
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 copyright(): pic=media.create_picture(20,20) black=media.black media.add_oval(pic,0,0,16,16,black) media.add_text(pic, 6,3,"C",black) media.show(pic) return pic
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_white_pic(): """Test if average_brightness calculates the correct value for a 5x5 picture consisting of only white pixels.""" new_pic = media.create_picture(5, 5, media.white) assert ( average_brightness.average_brightness(new_pic) == 255 ), "The expected average brightness for a white picture was not obtained."
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 draw_board(self): '''(self) -> NoneType draw player board and opponent board and return in list''' # Each space will be 25 pixels long, with a 10 pixel border on top board_l = self.l * 25 + 10 # Each space will be 25 pixels wide, with a 10 pixel border on the side board_w = self.w * 25 + 10 list_boards = [] # list holding boards # Draw player's board board_player = media.create_picture(board_w, board_l, media.navy) # Draw opponent's board board_opponent = media.create_picture(board_w, board_l, media.gray) list_boards.append(board_player) # Add player board to list list_boards.append(board_opponent) # Add opponent board to list for item in list_boards: # loop through list of boards to add borders # Draw bar along top media.add_rect_filled(item, 0, 0, 10, board_l, media.white) media.add_rect_filled(item, 0, 0, board_w, 10, media.white) counter = 0 # counter for while loop to draw numbers to board while counter < self.w: # loop to draw x coordinates on board temp = str(counter) x_val = (counter) * 25 + 10 media.add_text(item, x_val, 2, temp, media.black) counter += 1 counter = 0 while counter < self.l: # draw y coordinates on board temp = str(counter) y_val = (counter) * 25 + 10 media.add_text(item, 2, y_val, temp, media.black) counter += 1 # Loop through list of player ships to place them on board for item in self.coordinates: media.add_rect_filled(list_boards[0], item[0] * 25 + 10,\ item[1] * 25 + 10, 25, 25, media.gray) media.show(list_boards[0]) media.show(list_boards[1]) self.boards = list_boards
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 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 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 widen(original): '''(original) -> Picture Return a new picture that is twice as wide as the given picture.''' new_pic = media.create_picture(original.get_width() * 2, original.get_height()) for pix in original: original_x = pix.get_x() original_y = pix.get_y() new_pic.get_pixel(2 * original_x, original_y).set_color(pix.get_color()) new_pic.get_pixel(2 * original_x + 1, original_y).set_color(pix.get_color()) return new_pic
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 create_pic(x, y): '''Create and return a picture of width x and height y with a random colour in each pixel, to be used for testing.''' #This is a helper function. new_pic = media.create_picture(x, y) for pix in new_pic: pix.set_red(random.randint(0, 255)) pix.set_blue(random.randint(0, 255)) pix.set_green(random.randint(0, 255)) return new_pic
def close_pic_GUI (label): '''Make the picture loaded in Tkinter Label label blank.''' #Clear all the temp pics in front of the current pic current_pic global temp_label global current_pic temp_label = temp_label[0:current_pic+1] #Create a blank picture label.picture = media.create_picture(1, 1, media.white) #Keep a copy of the current pic for the functions Undo and Redo temp_label.append(label.picture) current_pic += 1 #Update the label update_label(label)
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 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 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
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 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()
result = a1.get_picture_width('bye') expected = 30 assert isinstance(result, int),\ '''a1.get_picture_width('bye') should return an int, but returned %s.''' % (type(result)) # Type check a1.word_to_pic result = a1.word_to_pic('today') assert isinstance(result, media.Picture), \ '''a1.word_to_pic(\'today\') should return a Picture, but it returned %s.''' % (type(result)) # Type check a1.count_black pic = media.create_picture(2, 3) result = a1.count_black(pic) assert isinstance(result, int), \ '''a1.count_black(pic) should return an int, but it returned %s.''' % (type(result)) # Type check a1.strikethrough pic = media.create_picture(30, 20) result = a1.strikethrough(pic) assert isinstance(result, media.Picture), \ '''a1.strikethrough(pic) should return a Picture, but it returned %s.''' % (type(result)) # Type check a1.widen
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 if __name__ == "__main__": pic = media.create_picture(50, 50, media.red) pic.inspect() pic2 = media.create_picture(50, 50, media.blue) pic2.inspect() str1 = 'redundancy' str2 = 'redundancy' filename = media.choose_file() mypic = media.load_picture(filename)
def play_sound_clip(sound_clip, loop=False): '''Plays the sound clip sound_clip with an option to loop the music.''' pygame.mixer.init() pygame.mixer.music.load(sound_clip) pygame.mixer.music.set_volume(.1113) if loop: pygame.mixer.music.play(-1) else: pygame.mixer.music.play() if __name__ == '__main__': pic = media.create_picture(WIDTH, HEIGHT) highscore = scoring.Score('highscore.txt') # Play the game as long as the user wants, adding one to the number of # nodes every round. print_title() play_sound_clip('intro.mp3') #Obtained intro music from the tron video game. name = raw_input('What is your name? ') #clear() print "Hello %s and welcome to the menial labour simulator" % (name) print ''' The rules are simple: Rotate the nodes in the tree to recreate the target tree. There are eight levels, each with one more node then the last, and your score per level
'''(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 if __name__ == "__main__": pic = media.create_picture(50, 50, media.red) pic.inspect() pic2 = media.create_picture(50, 50, media.blue) pic2.inspect() str1 = 'redundancy' str2 = 'redundancy' filename = media.choose_file() mypic = media.load_picture(filename)