Search code examples
pythonimagetkinterbase64pyinstaller

How do I use the base64 encoded image string in Tkinter label?


I am writing a tkinter program that utilises some JPG files for its background. However, I have found that when the script is converted to an .exe file using "pyinstaller", the images used for tkinter windows are not compiled/added to the .exe file.

Therefore, I have decided to hardcode the image in the Python script so that there is no external dependency. For this purpose, I have done the following things:

import base64
base64_encodedString= ''' b'hAnNH65gHSJ ......(continues...) '''
datas= base64.b64decode(base64_encodedString)

The above code is used for decoding the base 64 encoded Image data. I want to use this decoded image data to be used as a picture and display as a label/button in tkinter.

For example:

from tkinter import *
root=Tk()
l=Label(root,image=image=PhotoImage(data=datas)).pack()
root.mainloop()

However, tkinter is not accepting the value stored in data to be used as an image. It displays the following error -

Traceback (most recent call last):
  File "test.py", line 23, in <module>
    l=Label(root,image=PhotoImage(data=datas))
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 3394, in __init__

    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 3350, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize image data

Solution

  • Tkinter PhotoImage class (in Python 3 with tk 8.6) can only read GIF, PGM/PPM and PNG image formats. There are two ways to read an image:

    • From a file: PhotoImage(file="path/to/image.png")
    • From a base64-encoded string: PhotoImage(data=image_data_base64_encoded_string)

    First, if you want to convert an image into a base64-encoded string:

    import base64
    
    with open("path/to/image.png", "rb") as image_file:
        image_data_base64_encoded_string = base64.b64encode(image_file.read()) 
    

    Then use it in Tkinter:

    import tkinter as tk
    
    root = tk.Tk()
    
    im = tk.PhotoImage(data=image_data_base64_encoded_string)
    
    tk.Label(root, image=im).pack()
    
    root.mainloop()
    

    I think that your problem is that you decoded the string with datas= base64.b64decode(base64_encodedString) before using it in PhotoImage while you should have used base64_encodedString directly.