Python Forum
Display image from URL - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Display image from URL (/thread-12461.html)



Display image from URL - mr_byte31 - Aug-25-2018

Hi All,

I try to display an image from a URL but that doesn't seem to work.
do I have to download it first before I display ?


from tkinter import *
from PIL import ImageTk, Image
import os

root = Tk()
imgURL = 'https://www.marketbeat.com/scripts/temp/estimateswide4879.png'
img = ImageTk.PhotoImage(Image.open(imgURL))
panel = Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
root.mainloop()



RE: Display image from URL - snippsat - Aug-25-2018

(Aug-25-2018, 09:13 PM)mr_byte31 Wrote: do I have to download it first before I display ?
Could to that,but can also get image as bytes and give data to image.open() using BytesIO.
import tkinter as tk
from PIL import ImageTk, Image
import os
import requests
from io import BytesIO

root = tk.Tk()
img_url = "https://www.marketbeat.com/scripts/temp/estimateswide4879.png"
response = requests.get(img_url)
img_data = response.content
img = ImageTk.PhotoImage(Image.open(BytesIO(img_data)))
panel = tk.Label(root, image=img)
panel.pack(side="bottom", fill="both", expand="yes")
root.mainloop()
Some PEP-8 fixes and * is gone.