tkinter photoimage, couldn't recognize image data (PPM)

Terry Reedy tjreedy at udel.edu
Wed Dec 9 12:51:55 EST 2009


Martin P. Hellwig wrote:
> Hi all,
> 
> I've tried to display an image with the source being a string but it 
> fails (see below). Is there a way to display PPM without writing it 
> first to a file?
> 
> Thanks,
> 
> Martin
> 
> ----- snippet -----
> '''
> Ubuntu 9.04 64bit, python 3.1
> '''
> import tkinter
> 
> DATA="""P3
> 3 2
> 255
> 255   0   0     0 255   0     0   0 255
> 255 255   0   255 255 255     0   0   0"""

Should the string really have the newlines? Or should this be
DATA="""P3\
3 2\
255\
255   0   0     0 255   0     0   0 255\
255 255   0   255 255 255     0   0   0"""

> def display():
>     tk = tkinter.Tk()
>     canvas = tkinter.Canvas(tk, width=3, height=2)
>     canvas._image_reference = tkinter.PhotoImage(format='ppm', data=DATA)
>     canvas.create_image((0,0), image=canvas._image_reference)
>     canvas.pack()
>     tk.after(1500, tk.quit)
>     tk.mainloop()
> 
> if __name__ == '__main__':
>     display()
> -----
> 
> ----- traceback -----
> Traceback (most recent call last):
>   File "/home/martin/DCUK 
> Technologies/workspace/mhellwig/src/test/tkintering.py
> ", line 24, in <module>
>     display()
>   File "/home/martin/DCUK 
> Technologies/workspace/mhellwig/src/test/tkintering.py
> ", line 17, in display
>     canvas._image_reference = tkinter.PhotoImage(format='ppm', data=DATA)
>   File "/usr/local/lib/python3.1/tkinter/__init__.py", line 3269, in 
> __init__
>     Image.__init__(self, 'photo', name, cnf, master, **kw)
>   File "/usr/local/lib/python3.1/tkinter/__init__.py", line 3225, in 
> __init__
>     self.tk.call(('image', 'create', imgtype, name,) + options)
> _tkinter.TclError: couldn't recognize image data
> -----
> 




More information about the Python-list mailing list