reading picture from a directory every time is how normal code will do, but is that possible that we can save the picture object at the first time we open it then we copy the picture object and save in our code so that we don't need to read from file next time?
CodePudding user response:
You can use the base64 module to encode the image data in base64. You can then pass the encoded data to PhotoImage
with the data
parameter.
import tkinter as tk
image_data = """
R0lGODlhGAAYAPZvAAIaLgwmOwcnQQcoQBMxSBc4VBQ6Vhk7VRY9WhY XSI4RxJC
ZRRCZi9KYSFNbzJUbSJOcCZVeiZUeyBWfy1Zei5afB1Wgh1XgyFXgDpjgSVjkyZl
liZnmCxrmixsmjZtlSptoCpuoSxxqCxzqjh1oTV3qDR4qD5/rjV8tDV tUFlgUZp
hFNwhVVyh0ZzlUh7nnB8hUF8pkJ8pkx oTuBuFmBn3SEkHeOn3mNnUeBqUiBqUqD
q1CBpFKCpVuDoVyEokeGs0eHs0aKvkaMvlGOuGqKom Pp3yUpziEwTiFwz2KxT K
xE6VylKUw1qaxV6fx0CQ0E b1lCd1l6h0Wiq1Wyu12Wp22aq3FOi4F2s5WCt5W64
7XW24Xu743u/6omfsZymroqhso6jtJynsJ2psaS1w6S2xIvH7YvI7d/h5ODi5eDk
5 Dm6uLn7OPo7QAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAACH SUNvcHlyaWdodCBJTkNPUlMgR21iSCAod3d3Lmljb25l
eHBlcmllbmNlLmNvbSkgLSBVbmxpY2Vuc2VkIHByZXZpZXcgaW1hZ2UAIfkEBQAA
bwAsAAAAABgAGAAAB9eAb4KDhIWGh4iJiouMjY6PiGkwCgAACjBqj2ABBSw3Ny0H
AWONYwIqYmFfX2FiKwNki2kEFEZFRba3RhUEa4o2Cy41NT/FPsMuDDaKDRYfLzM8
PT0zMy8fFw2KBRoeJDI6Ojs5MSQdGwaKCCAmJ0FE8ERAJyUhCYoPIjRDTU5PT06a
CKEx4oEiHBuWMJlCpUoVKlOYKOFwQxEbB0ikXOHSpQsXK1GSQGizyIwFKFq8oDnj
JQsUDGYalYmAYsqWLVhSSIjpqM2RDBMuZDjiBpLRo0iTKjUaCAA7
"""
root = tk.Tk()
image = tk.PhotoImage(data=image_data)
label = tk.Label(root, image=image)
label.pack(padx=20, pady=20)
root.mainloop()