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

  • Thread starter Martin P. Hellwig
  • Start date
M

Martin P. Hellwig

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"""

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
 
T

Terry Reedy

Martin said:
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"""
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top