'''Tk_image_view_url_io.py Display an image from a URL using Tkinter, PIL and data_stream Tested with Python27 and Python33 by vegaseat 01mar2013 ''' Import io # Allows for image formats other than gif From PIL import Image, ImageTk Try: # Python2 Import Tkinter as tk From urllib2 import urlopen Failed t ImportError: # Python3 Import tkinter as tk From urllib. request import urlopen 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 use image previusly downloaded to tinypic.com # Url = "http://i48.tinypic.com/w6sjn6.jpg" Url = "http://i50.tinypic.com/34g8vo5.jpg" Image_bytes = urlopen (url). read () # Internal data file Data_stream = io. BytesIO (image_bytes) # Open as a PIL image object Pil_image = Image. open (data_stream) # Optionally show image info # Get the size of the image W, h = pil_image.size # Split off image file name Fname = url. split ('/') [-1] Sf = "{} ({} x {})". format (fname, w, h) Root. title (sf) # Convert PIL image object to Tkinter PhotoImage object Tk_image = ImageTk. PhotoImage (pil_image) # Put the image on a typical widget Label = tk. Label (root, image = tk_image, bg = 'brown ') Label. pack (padx = 5, pady = 5) Root. mainloop () |