using the 'data' option on the PhotoImage class of Tkinter

Fredrik Lundh fredrik at effbot.org
Thu Nov 9 12:10:45 EST 2000


Seung Chan Lim wrote:
> I'm doing the following to load the binary data of a GIF file and trying to
> instantiate a PhotoImage object using that data through its "data" option,
> and I'm having some trouble...
>
> import Tkinter
> root=Tk()
> f=open("temp.gif","rb")
> img=f.read()
> pimg=Tkinter.PhotoImage(data=img)
>
> and I get a
>
> TclError: couldn't recognize image data
>
> my GIF file is just a simple gif file saved in Photoshop...

how about "pimg=Tkinter.PhotoImage(file="temp.gif")" ;-)

but assuming you have good reasons for loading the image
in a non-direct way, you can find a hint in the tkinter intro
docs [1]:

    "Read image data from a string. In the current
    version of Tk, this only works for base64-encoded
    GIF files. If the file option is given, this option is
    ignored.

in other words, this should work:

    f=open("temp.gif","rb")
    img=base64.encodestring(f.read())
    pimg=Tkinter.PhotoImage(data=img)

for less convoluted ways to get binary image data into
Tkinter, check out the Python Imaging Library [2]

</F>

1) http://www.pythonware.com/library/tkinter/introduction/photoimage.htm
=> options

2) http://www.pythonware.com/products/pil





More information about the Python-list mailing list