[Tutor] how to display an image using python

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Thu Apr 14 21:25:18 CEST 2005



> 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