[Tutor] how to display an image using python

Ertl, John john.ertl at fnmoc.navy.mil
Thu Apr 14 21:33:32 CEST 2005


Danny,

Pygame.org...I would not have thought to look there.  In my google it did
not pop up.  I will definitely take a look and thanks for the example. 

John Ertl 

-----Original Message-----
From: Danny Yoo [mailto:dyoo at hkn.eecs.berkeley.edu]
Sent: Thursday, April 14, 2005 12:25
To: Ertl, John
Cc: Tutor
Subject: Re: [Tutor] how to display an image using python


> xv on the machines and PIL uses xv to display.  I have looked at
> PythonMagick but I could not even get past installing it.  It does not
have
> a setup.py and uses boost.  I am hoping for a more straightforward Python
> way.

Hi John,


You may want to try PyGame:

    http://www.pygame.org/

Although it's mainly for game development, it provides a simple graphics
API that we can use to display images.  If you're running Linux, it's
likely that you have the Simple DirectMedia Layer (SDL) library installed.


I'm not too familiar with the API, but I was able to get some kind of
working example.  We can first construct an image surface:

    http://www.pygame.org/docs/ref/pygame_image.html

by loading one from our file:

######
>>> import pygame.image
>>> picture = pygame.image.load("na-cat.gif")
>>>
>>> picture.get_size()
(256, 48)
######


At this point, 'picture' contains a "surface":

    http://www.pygame.org/docs/ref/Surface.html

We can copy ('blit') this surface onto our main screen:


######
>>> import pygame.display
>>> pygame.display.set_mode(picture.get_size())
<Surface(256x48x16 SW)>
>>> main_surface = pygame.display.get_surface()
>>> main_surface.blit(picture, (0, 0))
>>> pygame.display.update()
######


I hope this helps!


More information about the Tutor mailing list