Internet Picture Display (Python)

vegaseat 3 Tallied Votes 311 Views Share

A short code to show you how you can download and display an image from the internet on Python's Tkinter GUI toolkit.

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

    # 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 = ""

# or you can upload one of your own images to
url = ""

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

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

# 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)

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.