Reputation: 28662
I'm new to python and I'm on a windows 7 64 bit with python 3.3. I can display a gif image with the following code. However I can't make it work with png files. How to do that? Thanks.
import urllib
import urllib.request
import tkinter as tk
root = tk.Tk()
url = "http://www.baidu.com/img/bdlogo.gif"
u = urllib.request.urlopen(url)
raw_data = u.read()
u.close()
import base64
b64_data = base64.encodestring(raw_data)
image = tk.PhotoImage(data=b64_data)
label = tk.Label(image=image)
label.pack()
Upvotes: 1
Views: 3138
Reputation: 369494
You should use PIL (or pillow). You can find pillow windows binary here.
Try following example after you install pillow:
from io import BytesIO
import urllib
import urllib.request
import tkinter as tk
from PIL import Image, ImageTk
root = tk.Tk()
url = "http://imgs.xkcd.com/comics/python.png"
with urllib.request.urlopen(url) as u:
raw_data = u.read()
im = Image.open(BytesIO(raw_data))
image = ImageTk.PhotoImage(im)
label = tk.Label(image=image)
label.pack()
root.mainloop()
Upvotes: 2