#!/usr/bin/env python # Import everything import sys import pygame import glob from pygame.locals import * ############################################################################## # GoImageSurf # # A fast image viewer controlled via joystick or mouse # (c) 2004 by Georg Gogo. BERNHARD, GPL # # Configuration ############################################################## DELAY = 25 RESOLUTION = (640,480) DEPTH = 16 BGCOLOR = (0,0,0) FLAGS = HWSURFACE|DOUBLEBUF|FULLSCREEN # |HWPALETTE ############################################################################## # Print usage def printusage() : print sys.argv[0] print """ USAGE: python GoImageSurf.py Path Path would be something like "images/*.jpg" """ # Draw Image def showimage(display, image) : off_x = int(RESOLUTION[0]/2 - image.get_width()/2) off_y = int(RESOLUTION[1]/2 - image.get_height()/2) display.fill(BGCOLOR) display.blit(image, (off_x, off_y)) # Draw text def showtext(sur, text) : if (sur) : f = pygame.font.Font(None, 26) textimg = f.render(text, 1, (255,255,255), (0,0,0)) sur.fill((0,0,0)) sur.blit(textimg, (0,0)) pygame.display.flip() else : print text # Returns an image that is already converted and has the right size... def loadimage(Path): try : # Load image orig_image = pygame.image.load(Path).convert() # Scale the image to fit to screen, but keep aspect ratio orig_w = orig_image.get_width() orig_h = orig_image.get_height() dest_w = float(RESOLUTION[0]) dest_h = float(RESOLUTION[1]) scale = dest_w / orig_w if orig_h*scale > dest_h : scale = dest_h / orig_h size = (int(orig_w * scale), int(orig_h * scale)) fit_image = pygame.transform.scale(orig_image, size) # Explicitly delete the unneeded original image and return the scaled one del orig_image return fit_image except : showtext(None, "WARNING:" + str(file) + " could not be loaded.") return None # The core routine def main(): # Print the welcome message print """ GoImageSurf =========== (c) 2004 by Georg Gogo. BERNHARD gogo@bluedynamics.com (GPL) """ # Ask user for the path to load images from if len(sys.argv) > 1 : Path = sys.argv[1] else : printusage() Path = raw_input("Please enter wildchar path for images:") pygame.init() display = pygame.display.set_mode(RESOLUTION, FLAGS, DEPTH) showtext(display, repr(pygame.display.Info())) # Try to enable a joystick Joystick = None if pygame.joystick.get_count() == 0 : showtext(None, "No joystick found...") else : for x in range(pygame.joystick.get_count()): Joystick = pygame.joystick.Joystick(x) Joystick.init() showtext(display, "Enabled joystick: " + Joystick.get_name()) # Preload all images filelist = glob.glob(Path) showtext(display, "Image count: " + str(len(filelist))) imagelist = [] for file in filelist : for event in pygame.event.get(): if event.type in (QUIT,KEYDOWN): showtext(display, "Image load aborted.") sys.exit(1) image = loadimage(file) if image : imagelist.append(image) showtext(display, "(" + str(len(imagelist)) + "/" + str(len(filelist)) +") " + file + " loaded. " + str(image.get_size())) showimage(display, image) pygame.display.flip() else : showtext(display, file + " could not be loaded!") # Check if there are images at all if imagelist == [] : showtext(None, "No images found in "+ Path + ".") sys.exit(0) #Initialize main loop length = len(imagelist) count = 0 count_d = 0 delay = 0 direct = 1 # Repeat for ever while 1: # Check if the user wants to quit for event in pygame.event.get(): if event.type in (QUIT,KEYDOWN): return pygame.event.pump() if Joystick : # Use joystick input count_d = Joystick.get_axis(1) else : # Use mouse input # count_d = (float(pygame.mouse.get_pos()[0])/RESOLUTION[0]-0.5)*2 count = float(pygame.mouse.get_pos()[0])/RESOLUTION[0]*length # Let the images fly count = count + count_d * direct # Keep count in boundarys if count >= length-1 : direct = direct * -1 if count <= 0 : direct = direct * -1 count = count + 2 * (count_d * direct) print "count=", count image = imagelist[int(count)] showimage(display, image) pygame.display.flip() pygame.time.delay(DELAY) if __name__ == '__main__': main()