def greenScreen(image): array = image.getPixelArray() height = len(array) width = len(array[0]) filename = chooseInputFile() if filename != "": newImage = GImage(filename) newarray = newImage.getPixelArray() for i in range(height): for j in range(width): if isntGreen(newarray[i][j]): array[i][j] = newarray[i][j] return GImage(array)
def rotateLeft(image): array=image.getPixelArray() newArray=[[0]*len(array) for i in range(len(array[0]))] for col in range(len(newArray[0])): for row in range(len(newArray)): newArray[row][col]=array[col][len(array[0])-1-row] return GImage(newArray)
def equalize(image): array = image.getPixelArray() image_hist = computeImageHistogram(array) cuml_hist = computeCumulativeHistogram(image_hist) lum_array = computeNewLuminance(cuml_hist, array) new_array = luminanceToGreyscale(lum_array) return (GImage(new_array))
def greenScreen(image): """Creates a new GImage from the original one by replacing pixels in the original one with pixels in another chosen picture when the pixel of the chosen picture is not green.""" array = image.getPixelArray() new_filename = chooseInputFile() new_image = GImage(new_filename) new_array = new_image.getPixelArray() for i in range(min(len(array), len(new_array))): for j in range(min(len(array[0]), len(new_array[0]))): if not (bin(new_array[i][j])[18:26] > 2 * max( bin(new_array[i][j])[10:18], bin(new_array[i][j])[26:34])): array[i][j] = new_array[i][j] return GImage(array)
def GrayscaleImage(): gw = GWindow(GWINDOW_WIDTH, GWINDOW_HEIGHT) image = GImage(IMAGE_FILENAME) gw.add(image, (gw.getWidth() - IMAGE_SEP) / 2 - image.getWidth(), (gw.getHeight() - image.getHeight()) / 2) grayscale = createGrayscaleImage(image) gw.add(grayscale, (gw.getWidth() + IMAGE_SEP) / 2, (gw.getHeight() - image.getHeight()) / 2)
def rotateLeft(image): array = image.getPixelArray() height = len(array) width = len(array[0]) newarray = [[0 for x in range(height)] for x in range(width)] for i in range(height): for j in range(width): newarray[width - 1 - j][i] = array[i][j] return GImage(newarray)
def rotateRight(image): array = image.getPixelArray() new_array = [] for i in range(len(array[0])): new_array.append([]) for row in array[::-1]: for i in range(len(row)): new_array[i].append(row[i]) return GImage(new_array)
def rotateRight(image): """Creates a new GImage from the original one by transposing it along y = x.""" array = image.getPixelArray() #new_array = [[0]*len(array)]*len(array[0]) new_array = [[[0] for i in range(len(array))] for j in range(len(array[0]))] for i in range(len(array)): for j in range(len(array[0])): new_array[j][-i] = array[i][j] return GImage(new_array)
def blur(image): """Implements a blur function that takes the average of the red, green, and blue of surrounding pixels—needs to be applied several times to notice the effect, usually.""" array = image.getPixelArray() for i in range(len(array)): for j in range(len(array[0])): if (i != 0) and (i != (len(array) - 1)) and (j != 0) and ( j != (len(array[0]) - 1)): # Pick out the RBG components of surrounding pixels i_red = bin(array[i][j])[10:18] i_green = bin(array[i][j])[18:26] i_blue = bin(array[i][j])[26:34] ip1_red = bin(array[i + 1][j])[10:18] ip1_green = bin(array[i + 1][j])[18:26] ip1_blue = bin(array[i + 1][j])[26:34] im1_red = bin(array[i - 1][j])[10:18] im1_green = bin(array[i - 1][j])[18:26] im1_blue = bin(array[i - 1][j])[26:34] jp1_red = bin(array[i][j + 1])[10:18] jp1_green = bin(array[i][j + 1])[18:26] jp1_blue = bin(array[i][j + 1])[26:34] jm1_red = bin(array[i][j - 1])[10:18] jm1_green = bin(array[i][j - 1])[18:26] jm1_blue = bin(array[i][j - 1])[26:34] # Average the RBG components new_red = round( mean([ int(i_red, 2), int(ip1_red, 2), int(im1_red, 2), int(jp1_red, 2), int(jm1_red, 2) ])) new_green = round( mean([ int(i_green, 2), int(ip1_green, 2), int(im1_green, 2), int(jp1_green, 2), int(jm1_green, 2) ])) new_blue = round( mean([ int(i_blue, 2), int(ip1_blue, 2), int(im1_blue, 2), int(jp1_blue, 2), int(jm1_blue, 2) ])) # Make the new pixel with the averages array[i][j] = GImage.createRGBPixel(new_red, new_green, new_blue) return GImage(array)
def pixelize(image): array = image.getPixelArray() length = len(array) width = len(array[0]) for col in range(0, length, TILE_SIZE): for row in range(0, width, TILE_SIZE): pixel = array[col - (TILE_SIZE // 2)][row - (TILE_SIZE // 2)] for i in range(col - TILE_SIZE - 1, col + 1): for j in range(row - TILE_SIZE - 1, row + 1): array[i][j] = pixel return GImage(array)
def __init__(self, gw): """ The constructor for the EnigmaMachine class is responsible for initializing the graphics window along with the state variables that keep track of the machine's operation. """ enigmaImage = GImage("images/EnigmaTopView.png") gw.add(enigmaImage) self.keys = [] self.lamps = [] self.rotors = []
def greenScreen(old, new): old_array = old.getPixelArray() new_array = new.getPixelArray() for col in range(len(old_array)): for row in range(len(old_array[0])): if col < len(new_array) and row < len(new_array[0]): pixel = new_array[col][row] max_rb = max(GImage.getRed(pixel), GImage.getBlue(pixel)) green = GImage.getGreen(pixel) if green < 2 * max_rb: old_array[col][row] = pixel return GImage(old_array)
def createGrayscaleImage(image): """ Creates a grayscale image based on the luminance of each pixel """ array = image.getPixelArray() height = len(array) width = len(array[0]) for i in range(height): for j in range(width): gray = luminance(array[i][j]) array[i][j] = GImage.createRGBPixel(gray, gray, gray) return GImage(array)
def loadButtonAction(): """Callback function for the Load button""" nonlocal currentFile filename = chooseInputFile() currentFile = filename if filename != "": img = GImage(filename) width = len(img.getPixelArray()) height = len(img.getPixelArray()[0]) max_dim = max(width, height) sf = 750 / max_dim if max_dim > 750: img.scale(sf) setImage(img) clearMessage()
def start_round(gw, game_state, update_fn, update_fn_two, update_fn_three): if game_state.update_timer: game_state.update_timer.stop() if game_state.apple_timer: game_state.apple_timer.stop() if game_state.worm_timer: game_state.worm_timer.stop() instructions = GImage("instructions.png", 400, 270) gw.add(instructions) def onclick(e): game_state.update_timer = gw.setInterval(update_fn, TIME_STEP) game_state.apple_timer = gw.setInterval(update_fn_two, TIME_STEP_TWO) game_state.worm_timer = gw.setInterval(update_fn_three, TIME_STEP_THREE) gw.eventManager.clickListeners.pop() gw.remove(instructions) gw.addEventListener("click", onclick)
def equalize(image): array = image.getPixelArray() height = len(array) width = len(array[0]) colors = [0] * 256 cumulative = [0] * 256 total = 0 for i in range(height): for j in range(width): x = luminance(array[i][j]) colors[x] += 1 for i in range(len(colors)): total += colors[i] cumulative[i] = total for i in range(height): for j in range(width): x = luminance(array[i][j]) newLum = (255 * cumulative[x]) // (height * width) array[i][j] = GImage.createRGBPixel(newLum, newLum, newLum) return GImage(array)
def __init__(self, gw): """ The constructor for the EnigmaMachine class is responsible for initializing the graphics window along with the state variables that keep track of the machine's operation. """ enigmaImage = GImage("images/EnigmaTopView.png") gw.add(enigmaImage) self.gw = gw for letter in ALPHABET: i = ALPHABET.find(letter) key = EnigmaKey(letter) lamp = EnigmaLamp(letter) gw.add(key, KEY_LOCATIONS[i][0], KEY_LOCATIONS[i][1]) gw.add(lamp, LAMP_LOCATIONS[i][0], LAMP_LOCATIONS[i][1]) for i in range(3): selector = EnigmaRotorSelector(i) gw.add(selector, SELECTOR_LOCATIONS[i][0], SELECTOR_LOCATIONS[i][1]) rotor = EnigmaRotor(ROTOR_PERMUTATIONS[i]) gw.add(rotor, ROTOR_LOCATIONS[i][0], ROTOR_LOCATIONS[i][1]) gw.add(EnigmaReturn(), 98, 95) self.message = ''
def posterize(image): array = image.getPixelArray() for col in range(len(array)): for row in range(len(array[0])): pixel = array[col][row] red = GImage.getRed(pixel) green = GImage.getGreen(pixel) blue = GImage.getBlue(pixel) d_min = 500**2 for i in range(len(PALETTE)): pre_red = PALETTE[i][0] pre_green = PALETTE[i][1] pre_blue = PALETTE[i][2] d = ((2 * (pre_red - red)**2) + (4 * (pre_green - green)**2) + (3 * (pre_blue - blue)**2) + ((((pre_red + red) / 2) * ((pre_red - red)**2) - ((pre_blue - blue)**2)) / 256)) if d < d_min: d_min = d new_pixel = GImage.createRGBPixel(pre_red, pre_green, pre_blue) array[col][row] = new_pixel return GImage(array)
def add_winner(gw): winner_img = GImage("winner.png", 560, 300) gw.add(winner_img)
def rotateLeft2(image): array = image.getPixelArray() width = image.getWidth() return GImage([[row[i] for row in array] for i in range(width)][::-1])
def greenScreenAction(): '''Callback function for the GreenScreen button''' if currentImage is not None: filename = chooseInputFile() if filename != '': setImage(greenScreen(currentImage, GImage(filename)))
def flipVertical(image): array = image.getPixelArray() return GImage(array[::-1])
def flipHorizontal(image): array = image.getPixelArray() return GImage([row[::-1] for row in array])
def game(): gw = GWindow(GWINDOW_WIDTH, GWINDOW_HEIGHT) game_state = GameState() apples_collected = [] objects_lost = [] background = GImage("background.png", 0, 0) gw.add(background) scoreboard = GRect(GWINDOW_WIDTH - SB_WIDTH, 550, SB_WIDTH, SB_HEIGHT) scoreboard.setFilled(True) scoreboard.setColor("White") gw.add(scoreboard) collected_label = create_centered_label("Apples collected: ", GWINDOW_WIDTH - 160, 590, SB_FONT) gw.add(collected_label) collected_num_label = create_centered_label(str(len(apples_collected)), GWINDOW_WIDTH - 30, 590, SB_FONT) gw.add(collected_num_label) lost_label = create_centered_label("Apples lost: ", GWINDOW_WIDTH - 195, 650, SB_FONT) gw.add(lost_label) lost_num_label = create_centered_label(str(len(objects_lost)), GWINDOW_WIDTH - 30, 650, SB_FONT) gw.add(lost_num_label) c = Character() isaac_newton = c.character gw.add(isaac_newton) # This function adds the apples to the game, according to the timestep provided in the list of constants. Apples # added to a list and removed when they are either collected or they hit the ground. apples = [] def add_apples(): xpos = random.randint(0 + APPLE_WIDTH, GWINDOW_WIDTH - APPLE_WIDTH) f = Falling_Object(xpos) apple = f.apple gw.add(apple) apples.append(apple) # This function adds worms to the window. Worms will appear after some duration of the game (i.e. 5 apples # have been collected). They appear according to the third timestep provided in constants.py. worms = [] def add_worms(): if len(apples_collected) > 5: xpos = random.randint(0 + WORM_WIDTH, GWINDOW_WIDTH - WORM_WIDTH) w = Worm(xpos) worm = w.worm gw.add(worm) worms.append(worm) # This function increases the apples' y velocity every time 3 apples are collected so that the game becomes harder # as the player progresses. def change_yvel(): if len(apples_collected) % 3 == 0 and len(apples_collected) != 0: game_state.apple_yvel += 0.005 return game_state.apple_yvel # This is the most important function. It makes both the apple and the worm objects move. It handles collisions # between isaac_newton and objects, and deals with them accordingly. This function is called every timestep (constants) # If you lose < 3 apples and collect 25 without collecting a worm, you win the game! def update_objects(): collected_num_label.setLabel(len(apples_collected)) for apple in apples: apple_x_now = apple.getX() apple_y_now = apple.getY() + APPLE_HEIGHT isaac_x = isaac_newton.getX() isaac_y = isaac_newton.getY() if isaac_x <= apple_x_now <= ( isaac_x + ISAAC_WIDTH) and isaac_y <= apple_y_now <= ( isaac_y + ISAAC_HEIGHT): gw.remove(apple) apples.remove(apple) apples_collected.append(apple) if apple_y_now >= GWINDOW_HEIGHT: objects_lost.append(apple) lost_num_label.setLabel(len(objects_lost)) gw.remove(apple) apples.remove(apple) if len(objects_lost) >= 3: end_game(game_state) add_loser(gw) if len(apples_collected) == 25: collected_num_label.setLabel(len(apples_collected)) end_game(game_state) add_winner(gw) game_state.apple_yvel = change_yvel() apple.move(game_state.xvel, game_state.apple_yvel) for worm in worms: worm_x_now = worm.getX() worm_y_now = worm.getY() + WORM_HEIGHT isaac_x = isaac_newton.getX() isaac_y = isaac_newton.getY() if isaac_x <= worm_x_now <= ( isaac_x + ISAAC_WIDTH) and isaac_y <= worm_y_now <= ( isaac_y + ISAAC_HEIGHT): gw.remove(worm) worms.remove(worm) end_game(game_state) add_loser(gw) if worm_y_now >= GWINDOW_HEIGHT: gw.remove(worm) worms.remove(worm) worm.move(game_state.xvel, game_state.worm_yvel) # This function handles the key movement for isaac_newton. If the player touches the left arrow, isaac will move left. # This is the same for the right arrow. def key_action(event): if event.key == "<LEFT>": isaac_newton.move(-ISAAC_XVEL, ISAAC_YVEL) elif event.key == "<RIGHT>": isaac_newton.move(ISAAC_XVEL, ISAAC_YVEL) if isaac_newton.getX() >= (GWINDOW_WIDTH - ISAAC_WIDTH): isaac_newton.setLocation(GWINDOW_WIDTH - ISAAC_WIDTH, Character().ypos) gw.addEventListener("key", key_action) if isaac_newton.getX() <= 0: isaac_newton.setLocation(0, Character().ypos) gw.addEventListener("key", key_action) # Adds key event listener for the arrows and starts the round calling the appropriate functions with the right # timesteps. gw.addEventListener("key", key_action) start_round(gw, game_state, update_objects, add_apples, add_worms)
def loadButtonAction(): """Callback function for the Load button""" filename = chooseInputFile() if filename != "": setImage(GImage(filename))
def flipVertical(image): """Creates a new GImage from the original one by flipping it vertically.""" array = image.getPixelArray() return GImage(array[::-1])
def flipHorizontal(image): """Creates a new GImage from the original one by flipping it horizontally.""" array = image.getPixelArray() for row in range(len(array)): array[row] = array[row][::-1] return GImage(array)
def flipHorizontal(image): array = image.getPixelArray() height = len(array) for i in range(height): array[i] = array[i][::-1] return GImage(array)
def __init__(self): self.xpos = 400 self.ypos = GWINDOW_HEIGHT - 300 image = "isaacnewton.png" self.character = GImage(image, self.xpos, self.ypos)
def add_loser(gw): loser_img = GImage("loser.png", 525, 300) gw.add(loser_img)