Example #1
0
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)
Example #2
0
def luminanceToGreyscale(lum_array):
    """Takes in an array of luminances and returns an image array"""
    for i in range(len(lum_array)):
        for j in range(len(lum_array[0])):
            lum_array[i][j] = GImage.createRGBPixel(lum_array[i][j],
                                                    lum_array[i][j],
                                                    lum_array[i][j])
    return (lum_array)
Example #3
0
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)
Example #4
0
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)
Example #5
0
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)
Example #6
0
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)
Example #7
0
def equalizeArray(array):
	length = len(array)
	width = len(array[0])
	cumulative = computeCumulativeHistogram(array)
	for col in range(length):
		for row in range(width):
			pixel = array[col][row]
			new_lum = (255 * cumulative[luminance(pixel)]) // (length * width)
			new_pixel = GImage.createRGBPixel(new_lum, new_lum, new_lum)
			array[col][row] = new_pixel
	return array
Example #8
0
    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 = []
Example #9
0
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)
Example #10
0
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)
Example #11
0
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)
Example #12
0
 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()
Example #13
0
 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 = ''
Example #14
0
def flipHorizontal(image):
    array = image.getPixelArray()
    return GImage([row[::-1] for row in array])
Example #15
0
def flipVertical(image):
    array = image.getPixelArray()
    return GImage(array[::-1])
Example #16
0
 def loadButtonAction():
     """Callback function for the Load button"""
     filename = chooseInputFile()
     if filename != "":
         setImage(GImage(filename))
Example #17
0
    def __init__(self, xpos):
        self.xpos = xpos
        self.ypos = 0

        image = "uglyworm.png"
        self.worm = GImage(image, self.xpos, self.ypos)
Example #18
0
 def load_button_action():
     """Callback function for the Load button"""
     filename = choose_input_file()
     if filename != "":
         set_image(GImage(filename))
Example #19
0
def flipHorizontal(image):
    array = image.getPixelArray()
    height = len(array)
    for i in range(height):
        array[i] = array[i][::-1]
    return GImage(array)
Example #20
0
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 add_loser(gw):
    loser_img = GImage("loser.png", 525, 300)
    gw.add(loser_img)
def add_winner(gw):
    winner_img = GImage("winner.png", 560, 300)
    gw.add(winner_img)
Example #23
0
def flip_vertical(image):
    array = image.get_pixel_array()
    return GImage(array[::-1])
Example #24
0
def createEqualizedImage(image):
	array = image.getPixelArray()
	equalized_array = equalizeArray(array)
	return GImage(equalized_array)
Example #25
0
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)
Example #26
0
def flipHorizontal(image):
    array = image.getPixelArray()
    new_array = []
    for row in array:
        new_array.append(row[::-1])
    return GImage(new_array)
Example #27
0
def rotateLeft2(image):
    array = image.getPixelArray()
    width = image.getWidth()
    return GImage([[row[i] for row in array] for i in range(width)][::-1])
Example #28
0
    def __init__(self):
        self.xpos = 400
        self.ypos = GWINDOW_HEIGHT - 300

        image = "isaacnewton.png"
        self.character = GImage(image, self.xpos, self.ypos)
Example #29
0
def flipVertical(image):
    """Creates a new GImage from the original one by flipping it vertically."""
    array = image.getPixelArray()
    return GImage(array[::-1])
Example #30
0
 def greenScreenAction():
     '''Callback function for the GreenScreen button'''
     if currentImage is not None:
         filename = chooseInputFile()
         if filename != '':
             setImage(greenScreen(currentImage, GImage(filename)))