[Tutor] How to open a file with images

Kent Johnson kent37 at tds.net
Fri Dec 16 12:36:47 CET 2005


vikas mohan wrote:
> Hi everybody!
>  
> In Java we have the appletviewer and frames, through which we can access 
> image files. In python, if I want to open an image file in IDLE, how can 
> I do that, and what should my command look like?

Here is a simple program to open an image file and display it using PIL and Tkinter. It is 
based on an example program that comes with PIL.
http://effbot.org/imagingbook/imagetk.htm

import Image, ImageTk
from Tkinter import Tk, Label

im = Image.open('wire.png')

class UI(Label):

     def __init__(self, master, im):

         if im.mode == "1":
             # bitmap image
             self.image = ImageTk.BitmapImage(im, foreground="white")
             Label.__init__(self, master, image=self.image, bg="black", bd=0)

         else:
             # photo image
             self.image = ImageTk.PhotoImage(im)
             Label.__init__(self, master, image=self.image, bd=0)

root = Tk()
UI(root, im).pack()
root.mainloop()


Kent



More information about the Tutor mailing list