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

Discussion in 'Python' started by Martin P. Hellwig, Dec 9, 2009.

  1. 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
    -----

    --
    MPH
    http://blog.dcuktec.com
    'If consumed, best digested with added seasoning to own preference.'
     
    Martin P. Hellwig, Dec 9, 2009
    #1
    1. Advertising

  2. Martin P. Hellwig

    Terry Reedy Guest

    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
    > -----
    >
     
    Terry Reedy, Dec 9, 2009
    #2
    1. Advertising

  3. Terry Reedy wrote:
    <cut>
    >> 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"""
    >


    I've tried it, still same error, I did had a look at
    http://netpbm.sourceforge.net/doc/ppm.html and the wikipedia page.

    <cut>
    >> self.tk.call(('image', 'create', imgtype, name,) + options)
    >> _tkinter.TclError: couldn't recognize image data
    >> -----
    >>

    >



    --
    MPH
    http://blog.dcuktec.com
    'If consumed, best digested with added seasoning to own preference.'
     
    Martin P. Hellwig, Dec 9, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark 'Kamikaze' Hughes

    Tkinter PhotoImage or PIL transparent subimages

    Mark 'Kamikaze' Hughes, Jul 20, 2003, in forum: Python
    Replies:
    0
    Views:
    808
    Mark 'Kamikaze' Hughes
    Jul 20, 2003
  2. Laszlo Zsolt Nagy
    Replies:
    1
    Views:
    1,414
    Kartic
    Jan 26, 2005
  3. defn noob
    Replies:
    2
    Views:
    841
    Eric Brunel
    Jun 30, 2008
  4. Marko Faldix

    ppm: ppm install Spreadsheet::ParseExcel

    Marko Faldix, Jan 26, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    456
    James Willmore
    Jan 27, 2004
  5. Andre Caron
    Replies:
    3
    Views:
    195
    Andre Caron
    Jan 9, 2006
Loading...

Share This Page