How can I store images in a python script using Tkinter?
I want to render an image in tkinter, but I always get a message that the image (PieTalk.gif) cannot be found, even if the image is in the same directory as the python script (startupgui. Py):
/Home/COMP3203/COMP30203-project/src/ptgui/
Here is the method where I want to display the image in the GUI. The following code is a class called startupgui.py. It consists of a constructor and a way to load the image
Constructor:
def __init__(self):
# String to decide whether to go to client or server
self.trigger = None
#-----------------------#
# Creating window frame #
#-----------------------#
self.root = Tk()
self.root.wm_title("PieTalk")
self.root.geometry('600x450')
# creating PieTalk image
self.createimage()
# creating buttons
self.createbuttons()
Image upload method:
def createimage(self):
# Creating image frame
self.imageframe = Frame(self.root, bg='light grey')
self.imageframe.place(relx=0.1, relwidth=0.8, rely=0.05, relheight=0.65)
# Creating PieTalk image
self.pietalkimage=PhotoImage(file="PieTalk.gif")
# Creating label
self.imagelabel = Label(self.imageframe, image=pietalkimage)
self.imagelabel.image = self.pietalkimage
self.imagelabel.pack()
I only used the filename:
self.pietalkimage=PhotoImage(file="PieTalk.gif")
And I also used the absolute file path:
self.pietalkimage=PhotoImage(file="/Home/COMP3203/COMP30203-project/src/ptgui/PieTalk.gif")
Unfortunately I keep getting the same error when executing the script:
Traceback (most recent call last):
File "pietalk.py", line 361, in <module>
startview=sgui.startupgui()
File "/home/archit/COMP3203/COMP3203-project/src/ptgui/startupgui.py", line 66, in __init__
self.createimage()
File "/home/archit/COMP3203/COMP3203-project/src/ptgui/startupgui.py", line 33, in createimage
self.pietalkimage=PhotoImage(file="/Home/COMP3203/COMP30203-project/src/ptgui/PieTalk.gif")
File "/usr/lib/python3.4/tkinter/__init__.py", line 3387, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "/usr/lib/python3.4/tkinter/__init__.py", line 3343, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "/Home/COMP3203/COMP30203-project/src/ptgui/PieTalk.gif": no such file or directory
Is there something else I am doing wrong when I load the image? What else can I do to upload the image?
source to share
first convert it to base64 python variable
>>> import base64
>>> with open("my_image.py","w") as f:
... f.write('my_image="""%s"""'%base64.b64encode(open("my_gif.gif","rb").read()))
...
>>> exit()
you should now have my_image.py file ... copy it to the same directory as your tkinter script ... you can now do
from my_image import my_image
image=PhotoImage(data = my_image)
since you are having some problems try to simplify it a bit
img2base64.py
import base64,sys,os,re
assert len(sys.argv) > 2,"Error: Useage %s /path/to/image.gif outfile.py"
assert os.path.exists(sys.argv[1]),"Error Unable to find image passed in as first argument"
outfile = open(sys.argv[2],"w")
raw_binary_data = open(sys.argv[1],"rb").read()
b64_encoded_data = base64.b64encode(raw_binary_data)
varname = re.sub("[^W]","_",os.path.splitext(os.path.basename(sys.argv[1]))[0])
pyname = os.path.splitext(os.path.basename(sys.argv[2]))[0]
outfile.write("%s='''%s'''"%(varname,b64_encoded_data))
outfile.close()
print "all done please put %s in your script directory and use it as follows:"%sys.argv[2]
print "from %s import %s"%(pyname,varname)
print "image=PhotoImage(data = %s)"%(varname)
just save this and then call it
$ python img2base64.py /path/to/image.gif pyimage.py
I guess at least I haven't tried ...
source to share