A simple image viewer with pygame

Posted:

The following python code demonstrates the power of pygame to make even non-game tasks easy.

This command line program is to be invoked with a filename of a graphic file (any supported by pygame including jpg, png, gif, etc). To exit, press escape. To reduce the image by half, press the minus key. To increase the image by x2, press the equals/plus button.

Sure, the scaling is lossy, but this is a quick and dirty program.

Enjoy!

import os
import sys
import pygame

def reset_screen(res) :
    screen = pygame.display.set_mode(res)
    pygame.display.set_caption("pyview")
    return screen

imgfile = ""
if len(sys.argv) < 2 :
    print "USAGE: %s [FILENAME]" % sys.argv[0]
    sys.exit()
imgfile = sys.argv[1]
if not os.path.exists(imgfile):
    print "USAGE: %s [FILENAME]" % sys.argv[0]
    sys.exit()

S = pygame.image.load(imgfile)
screen = reset_screen((S.get_width(), S.get_height()))
S = S.convert() # Need screen init

while True :
    for e in pygame.event.get() :
        if e.type == pygame.QUIT :
            sys.exit()
        if e.type == pygame.KEYDOWN :
            if e.key == pygame.K_ESCAPE :
                sys.exit()
            if e.key == pygame.K_MINUS :
                S = pygame.transform.scale(S, (S.get_width()/2, S.get_height()/2))
                screen = reset_screen((S.get_width(), S.get_height()))

            if e.key == pygame.K_EQUALS :
                S = pygame.transform.scale(S, (S.get_width()*2, S.get_height()*2))
                screen = reset_screen((S.get_width(), S.get_height()))
    screen.blit(S, (0,0))
    pygame.display.flip()
    pygame.time.delay(25)