''' Tk_ImageTest_url_23.py
display an image from a URL using Tkinter
tested with Python27 and Python33  by  vegaseat  13feb2013
'''

try:
    # Python2
    import urllib2 as url_lib
    import Tkinter as tk
except ImportError:
    # Python3
    import urllib.request as url_lib
    import tkinter as tk

root = tk.Tk()

# find yourself a picture on an internet web page you like
# (right click on the picture, under properties copy the address)
#url = "http://www.google.com/intl/en/images/logo.gif"

# or you can upload one of your own images to tinypic.com
url = "http://i48.tinypic.com/9aod3n.gif"

picture = url_lib.urlopen(url).read()

# save the image as a file
fname = 'xyz_' + url.split('/')[-1]
root.title(fname)
with open(fname, "wb") as fout:
    fout.write(picture)

# read image file back in
img = tk.PhotoImage(file=fname)

# put the image on a typical widget
label = tk.Label(root, image=img)
label.pack(padx=5, pady=5)

root.mainloop()
1
Contributor
0
Replies
25
Views
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.