Wednesday, September 10, 2008

Homework 3 (Python Image Viewer)

First off, I must give credit as I found a base program that was essentially what we were doing for this program which I then altered to view multiple images rather than just bring up one. It was originally created by Kevin Harris, I guess as a teaching implement. So thank you Kevin. It didn't take long to figure out how it worked. It uses the Pygame package of Python and was really not hard to alter so that the user can switch between the 9 images that I chose to program it for. It can't just take any old image we want nor can it dynamically change the screen size depending on the individual image, but considering I hadn't ever used Python until this, I'll settle. Here is the code:

#------------------------------------------------------------------------------
# Name: pyg_image.py
# Author: Kevin Harris (Modified by Derek Wilson)
# Last Modified: 9/10/08
# Description: This Python/Pygame script demonstrates how to load and
# display an image file. Altered to switch between multiple
# images using the number keys.
#------------------------------------------------------------------------------

import pygame
from pygame.locals import *

def main():
pygame.init()

screen = pygame.display.set_mode( (500,500) )

background = pygame.Surface( screen.get_size() )

background.fill( (0,0,0) )

image = pygame.image.load( "pygame_powered.bmp" )
# image = pygame.image.load( "Devil.jpg" )

imagePosition = image.get_rect()

imagePosition.bottom = 250
imagePosition.left = 120

screen.blit( background, (0,0) )
screen.blit( image, imagePosition )

pygame.display.flip()

while 1:
pygame.event.pump()

keyinput = pygame.key.get_pressed()

if keyinput[K_ESCAPE] or pygame.event.peek(QUIT):
break

if keyinput[K_1]:
screen = pygame.display.set_mode( (620,877) )
image = pygame.image.load( "Devil.jpg" )
imagePosition.top = 0
imagePosition.left = 0

if keyinput[K_2]:
screen = pygame.display.set_mode( (520,700) )
image = pygame.image.load( "Dragonfly.jpg" )
imagePosition.top = 0
imagePosition.left = 0

if keyinput[K_3]:
screen = pygame.display.set_mode( (1148,887) )
image = pygame.image.load( "Forest.jpg" )
imagePosition.top = 0
imagePosition.left = 0

if keyinput[K_4]:
screen = pygame.display.set_mode( (558,561) )
image = pygame.image.load( "Forest Spirit.jpg" )
imagePosition.top = 0
imagePosition.left = 0

if keyinput[K_5]:
screen = pygame.display.set_mode( (662,936) )
image = pygame.image.load( "Ga Man.jpg" )
imagePosition.top = 0
imagePosition.left = 0

if keyinput[K_6]:
screen = pygame.display.set_mode( (574,838) )
image = pygame.image.load( "Hellsing.jpg" )
imagePosition.top = 0
imagePosition.left = 0

if keyinput[K_7]:
screen = pygame.display.set_mode( (498,709) )
image = pygame.image.load( "Ice Dragon.jpg" )
imagePosition.top = 0
imagePosition.left = 0

if keyinput[K_8]:
screen = pygame.display.set_mode( (716,947) )
image = pygame.image.load( "Morgan_le_Fay.jpg" )
imagePosition.top = 0
imagePosition.left = 0

if keyinput[K_9]:
screen = pygame.display.set_mode( (898,787) )
image = pygame.image.load( "Oak Dragon.jpg" )
imagePosition.top = 0
imagePosition.left = 0

if keyinput[K_0]:
screen = pygame.display.set_mode( (500,500) )
image = pygame.image.load( "pygame_powered.bmp" )
imagePosition.bottom = 250
imagePosition.left = 120

screen.blit( background, (0,0) )
screen.blit( image, imagePosition )

pygame.display.flip()

if __name__ == '__main__': main()

No comments: