How to display an image from a URL in Python 2.7 without installing additonal li
ID: 3843784 • Letter: H
Question
How to display an image from a URL in Python 2.7 without installing additonal libraries.
I use Python 2.7 and I can't install additional software i.e. the program has to run from within it's own library. There are many examples of how to display an image in Tkinter from a URL on the web but none of them work for me, so you must use Python 2.7 to run the code and paste the code in the answer. An example image URL could be https://denissov.ru/files/collections/o_fef04c806b9f041b6aba73d5ff2d41e79.png
Please provide the code so I can paste it into a new Python 2.7 file and run it, and the image will display in a Tkinter window.
Explanation / Answer
import io
import base64
import Tkinter as tk
from urllib2 import urlopen
root = tk.Tk()
root.title("display a website image")
w = 500
h = 320
x = 80
y = 100
root.geometry("%dx%d+%d+%d" % (w, h, x, y))
image_url = "https://media.giphy.com/media/7d7BC6Ht95q8w/giphy.gif"
image_byt = urlopen(image_url).read()
image_b64 = base64.encodestring(image_byt)
photo = tk.PhotoImage(data=image_b64)
cv = tk.Canvas(bg='white')
cv.pack(side='top', fill='both', expand='yes')
cv.create_image(10, 10, image=photo, anchor='nw')
root.mainloop()
The above code will work for gif url's only.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.