Reputation: 177
I am loading an image from a server, and I keep getting this error when I use Base64 on the data.
Here's my code:
import tkinter as tk
from PIL import ImageTk
root = tk.Tk()
import urllib.request
URL = "https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcSm7eLMSp4NbjGwkjU3rPokWaQI6224lQCR1qIIGIfldm4M0TgY0JKTGQLQ"
u = urllib.request.urlopen(URL)
raw_data = u.read()
u.close()
import base64
b64_data = base64.encodestring(raw_data)
image = ImageTk.PhotoImage(data=b64_data)
label = tk.Label(image=image)
label.pack()
I get this error:
Traceback (most recent call last):
File "C:/testt.py", line 11, in <module>
image = ImageTk.PhotoImage(data=b64_data)
File "C:\Python34\lib\site-packages\PIL\ImageTk.py", line 88, in __init__
image = Image.open(BytesIO(kw["data"]))
File "C:\Python34\lib\site-packages\PIL\Image.py", line 2287, in open
% (filename if filename else fp))
OSError: cannot identify image file <_io.BytesIO object at 0x0000000003152048>
Upvotes: 0
Views: 291
Reputation: 5844
Your code works, if you remove some of it. The base64 encoding is not needed:
import tkinter as tk
from PIL import ImageTk
import urllib.request
root = tk.Tk()
URL = "https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcSm7eLMSp4NbjGwkjU3rPokWaQI6224lQCR1qIIGIfldm4M0TgY0JKTGQLQ"
u = urllib.request.urlopen(URL)
raw_data = u.read()
u.close()
image = ImageTk.PhotoImage(data=raw_data)
label = tk.Label(image=image)
label.pack()
tk.mainloop()
Result:
Upvotes: 2