Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3- How to retrieve an image from the web and display in a GUI using TKINTER?

I want a function that, when a button is clicked, it will take an image from the web using URLLIB and display it in a GUI using TKINTER.

I'm new to both URLLIB and TKINTER, so I'm having an incredibly difficult time doing this.
Tried this, but it obviously doesn't work because it uses a textbox and only will display text.

 def __init__(self, root):
    self.root = root
    self.root.title('Image Retrieval Program')
    self.init_widgets()


def init_widgets(self):
    self.btn = ttk.Button(self.root, command=self.get_url, text='Get Url', width=8)
    self.btn.grid(column=0, row=0, sticky='w')

    self.entry = ttk.Entry(self.root, width=60)
    self.entry.grid(column=0, row=0, sticky='e')

    self.txt = tkinter.Text(self.root, width=80, height=20)
    self.txt.grid(column=0, row=1, sticky='nwes')
    sb = ttk.Scrollbar(command=self.txt.yview, orient='vertical')
    sb.grid(column=1, row=1, sticky='ns')
    self.txt['yscrollcommand'] = sb.set

def get_url(self):
    s = urllib.request.urlretrieve("http://www.smellymonkey.com/monkeys/images/ill-monkey.gif", "dog.gif")
    tkimage = ImageTk.PhotoImage(im)
    self.txt.insert(tkinter.INSERT, s)
like image 578
Parseltongue Avatar asked Dec 27 '22 20:12

Parseltongue


1 Answers

I don't use python 3, but I can give an answer that works in python 2.5+. I assume the code will work nearly identically on python 3.

Before we get started, we need to import Tkinter and create the root window:

import Tkinter as tk
root = tk.Tk()

Next, to download the image using urllib:

import urllib
URL = "http://www.smellymonkey.com/monkeys/images/ill-monkey.gif"
u = urllib.urlopen(URL)
raw_data = u.read()
u.close()

You now have the binary data for the image in the variable raw_data. Tkinter accepts a data option, but unfortunately you can't feed it this raw data. It expects the data to be encoded as base64. This is easy enough to do:

import base64
b64_data = base64.encodestring(raw_data)
image = tk.PhotoImage(data=b64_data)

Now that we have an image, time to put it on the screen:

label = tk.Label(image=image)
label.pack()

You should now see the image on the screen.

The above only works for .gif images, but other image formats are almost as easy to handle. The simplest method is to write the raw image data to disk (or use urllib to directly download the data to a file) and reference the file when creating the PhotoImage object. Of course, this only works for image formats directly supported by the PhotoImage class.

Your other choice is to use PIL (Python Image Library) which supports many different image formats. The technique remains roughly the same, only you must first create a PIL image, then convert it to a format usable by Tkinter. For more information about PIL consult the Python Imaging Library Handbook and also the effbot documentation on The Tkinter PhotoImage Class

like image 166
Bryan Oakley Avatar answered Dec 31 '22 12:12

Bryan Oakley