Display An Image From A URL (Tkinter, Python)

vegaseat 4 Tallied Votes 25K Views Share

This code shows how to obtain and display a GIF image from an internet website using the Tkinter GUI toolkit that comes with the Python installation.

'''Tk_Canvas_Image_url.py
display an image obtained from an internet web page in Tkinter
tested with Python27 and Python33  by  vagaseat   21nov2012
'''

import io
import base64
try:
    # Python2
    import Tkinter as tk
    from urllib2 import urlopen
except ImportError:
    # Python3
    import tkinter as tk
    from urllib.request import urlopen


root = tk.Tk()
root.title("display a website image")
# a little more than width and height of image
w = 520
h = 320
x = 80
y = 100
# use width x height + x_offset + y_offset (no spaces!)
root.geometry("%dx%d+%d+%d" % (w, h, x, y))

# this GIF picture previously downloaded to tinypic.com
image_url = "http://i46.tinypic.com/r9oh0j.gif"

image_byt = urlopen(image_url).read()
image_b64 = base64.encodestring(image_byt)
photo = tk.PhotoImage(data=image_b64)

# create a white canvas
cv = tk.Canvas(bg='white')
cv.pack(side='top', fill='both', expand='yes')

# put the image on the canvas with
# create_image(xpos, ypos, image, anchor)
cv.create_image(10, 10, image=photo, anchor='nw')

root.mainloop()
Lardmeister 461 Posting Virtuoso

Have been looking for something like that, thanks!
Clever application of base64 code.

Ene Uran 638 Posting Virtuoso

You can use the canvas image as a background to your turtle drawings:

''' turtle_Tk_Canvas_Image_url.py
display a GIF image obtained from an internet web page
on a Tkinter canvas, then use the canvas for some turtle drawing
modified Vegaseat code
'''

import io
import base64
import turtle
try:
    # Python2
    import Tkinter as tk
    from urllib2 import urlopen
except ImportError:
    # Python3
    import tkinter as tk
    from urllib.request import urlopen


root = tk.Tk()
root.title("turtle graphics a website image")

# this GIF picture previously downloaded to tinypic.com
image_url = "http://i46.tinypic.com/r9oh0j.gif"

image_byt = urlopen(image_url).read()
image_b64 = base64.encodestring(image_byt)
photo = tk.PhotoImage(data=image_b64)

# create a white canvas large enough to fit the image+
w = 540
h = 340
cv = tk.Canvas(bg='white', width=w, height=h)
cv.pack()

# this makes the canvas a turtle canvas
# point(0, 0) is in the center now
tu = turtle.RawTurtle(cv)

# put the image on the turtle canvas with
# create_image(xpos, ypos, image, anchor)
xpos = int(w/2 * 0.9)
ypos = int(h/2 * 0.9)
print(xpos, ypos)
cv.create_image(-xpos, -ypos, image=photo, anchor='nw')

# now do some turtle graphics
tu.pensize(2)
for radius in range(50, 200, 40):
    # pen up
    tu.up()
    # move pen to point x, y
    # keeps the center of the circle at canvas center
    tu.goto(0, -radius)
    # pen down
    tu.down()
    tu.circle(radius)

root.mainloop()
KiDo3Konvict 0 Newbie Poster

It's working, but it's not animated, I simply copy-pasted the code, didn't change anything, then changed the image, but still not animating.

Lardmeister 461 Posting Virtuoso

Tkinter cannot display animated GIFs. Nobody claimed that.

Taylor_6 0 Newbie Poster

What about jpg or png images? is there a way to do that?

creeperdetonater 0 Newbie Poster

When i tryed this myself with my own url, it said:
Traceback (most recent call last):
File "/Users/GAMEKNIGHT7/Documents/chineseCheckersAI(genius hour).py", line 14, in <module>
image_byt = url(image_url).read()
NameError: name 'url' is not defined
Is there something wrong with the module? When i tested it with your url, it worked fine.

woooee 814 Nearly a Posting Maven

NameError: name 'url' is not defined

There is no variable named 'url' in code posted above.

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.