We can download a PNG file and show it in a label.
We don’t need to download the image to the file system – we can download it directly into memory using requests.get and BytesIO.
Tkinter supports PNG image files natively, so there is no need to use the Pillow library to show PNG images.
Snippet
12345678
# Download the image into memory (binary stream).binary_stream=download_file(url)# Load the binary stream into an image format that Tkinter can use.tk_pizza=tk.PhotoImage(data=binary_stream.getvalue())# Show the imagelbl_test=ttk.Label(root,image=tk_pizza)
importrequestsimporttkinterastkfromtkinterimportttkfromioimportBytesIOdefdownload_file(url)->BytesIO:""" Download the file in chunks and return the finished download as a binary stream (BytesIO). """try:# Initializer:requests.models.Response=requests.get(url=url,stream=True)mystream=BytesIO()# Chunk downloadforchunkinr.iter_content(chunk_size=128):# Write the downloaded chunk to the stream variable.mystream.write(chunk)returnmystreamexceptrequests.exceptions.ConnectionErrorase:print(f"Connection error: {e}")url=r"https://some_valid_url_here/pizza.png"root=tk.Tk()binary_stream=download_file(url)# Load the binary stream into an image format that Tkinter can use.tk_pizza=tk.PhotoImage(data=binary_stream.getvalue())lbl_test=ttk.Label(root,image=tk_pizza)lbl_test.pack()root.mainloop()