Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 554
Boudhayan Dev Avatar asked Oct 16 '25 14:10

Boudhayan Dev


2 Answers

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.

like image 175
j_4321 Avatar answered Oct 18 '25 06:10

j_4321


Just to correct the very good answer of j_4321, the correct line for PhotoImage is :

im = tk.PhotoImage(data=image_data_base64_encoded_string)

and my solution to write the 'image' string in order to import it after :

with open("image.py", "wb") as fichier:
    fichier.write(b'imageData=b\'' + image_data_base64_encoded_string + b'\'')

A simple import image as img and the image data will be stored in the .exe file with Pyinstaller (-F option).

like image 41
Clément v Avatar answered Oct 18 '25 07:10

Clément v



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!