displaying an Image using PIL w/o Image.show

Quick Dry quickdry at users.sourceforge.net
Mon Nov 19 23:24:10 EST 2001


"Tech" <vze2q3xh at verizon.net> wrote in message
news:3sjjvt0c8rnq3iuhtnlqvb1a3ekpr4ac0d at 4ax.com...
> How do to show an Image in PIL without calling the Image.Show()
> Method?  I tried to use Image.Show() but I don't have some lib that I
> need.  Anyway the online docs seemed to suggest the there may be a
> better way but did not say what it was.

you could display the image on a Canvas, thus showing it all via Python

heres a quick hack I used to show an updated image from a webcam, its
probably got all sorts of bad style, but shows an image on a canvas, and
updates it every so often

------------ code starts here ------------

from Tkinter import *
import Image, ImageTk
import webcam # has methods to get images from a webcam

Width, Height = 320, 240

ADDRESS="127.0.0.1"
PORT="8081"
REFRESH=500


class Camera(Frame):
    def __init__(self, parent=None, title='eHome', address=ADDRESS,
port=PORT, msecs=1000, **args):
        Frame.__init__(self, parent, args)
        self.title=title
        self.address=address
        self.port=port
        self.makeWidgets()
        self.pack(expand=YES, fill=BOTH)
        self.width=WIDTH
        self.height=HEIGHT
        self.image_store = None
        self.msecs  = msecs
        self.confirmQuit = 0
        self.drawn  = None
        self.fit = 1

    def makeWidgets(self):
        self.canvas = Canvas(self, bg='white', height=Height, width=Width)
        self.canvas.pack(side=LEFT, fill=BOTH, expand=YES)
        self.onoff = Button(self, text='Start', command=self.onStart)
        self.onoff.pack(fill=X)
        Button(self, text='Quit',  command=self.onQuit).pack(fill=X)


    def onStart(self):
        self.loop = 1
        self.onoff.config(text='Stop', command=self.onStop)
        self.canvas.config(height=self.height, width=self.width)
        self.onTimer()

    def onStop(self):
        self.loop = 0
        self.onoff.config(text='Start', command=self.onStart)

    def onQuit(self):
        self.onStop()
        self.update()
        if not self.confirmQuit:
            self.quit()
        else:
            if askyesno(self.title, 'Really quit now?'):
                self.quit()

    def onTimer(self):
        if self.loop:
            self.drawNext()
            self.after(self.msecs, self.onTimer)

    def drawNext(self):
        if self.drawn:
            self.canvas.delete(self.drawn)
        name = "campic"
        img  = self.getCamPic()
        img1=ImageTk.PhotoImage(image=img)
        self.drawn = self.canvas.create_image(2, 2, image=img1, anchor=NW)
        self.image = name, img1
        self.canvas.update()

    def getCamPic(self):
        img=ImageTk.PhotoImage(data=webcam.read(self.address+":"+self.port))
        return img


if __name__ == '__main__':
    root = Tk()
    root.title('CamView 1.0')
    Label(root, text="Cameras").pack()
    Camera(root, bd=3, address=ADDRESS, port=PORT, msecs=REFRESH,
relief=SUNKEN).pack()
    root.mainloop()






More information about the Python-list mailing list