A short code to show you how you can download and display an image from the internet on Python's Tkinter GUI toolkit.
Internet Picture Display (Python)
''' 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()
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.