tags:

views:

335

answers:

6

What is the easiest way to show a .jpg or .gif image from Python console?

I've got a Python console program that is checking a data set which contains links to images stored locally. How should I write the script so that it would display images pop-up graphical windows?

+2  A: 

You cannot display images in a console window. You need a graphical toolkit such as Tkinter, PyGTK, PyQt, PyKDE, wxPython, PyObjC or PyFLTK. There are plenty of tutorial how to create siomple windows and loading images iun python.

codymanix
+7  A: 

Using the awesome PIL library:

>>> from PIL import Image                                                                                
>>> img = Image.open('test.png')
>>> img.show()

This will open the image in your default image viewer.

lost-theory
+3  A: 

Why not just display it in the user's web browser?

anthony
Indeed, `webbrowser.open()` works perfectly for any file type you can open from browser: images, video, office documents, mp3, etc (it starts corresponding program e.g., MPlayer for video files).
J.F. Sebastian
+3  A: 

Or simply execute the image through the shell, as in

import subprocess
subprocess.call([ fname ], shell=True)

and whatever program is installed to handle images will be launched.

THC4k
This solution is Windows specific (namely for cmd.exe as a shell) It won't work on common *nix shells in default configuration e.g., sh, bash, etc.
J.F. Sebastian
+3  A: 

Since you are probably running Windows (from looking at your tags), this would be the easiest way to open and show an image file from the console without installing extra stuff like PIL.

import os
os.system('start pic.png')
Unknown
subprocess is preferred to os.system in Python 2.4 and later.
Jason R. Coombs
A: 

Python aalib baby! http://jwilk.net/software/python-aalib.html

jskaggz