【问题标题】:Displaying Image from URL in python/Tkinter在 python/Tkinter 中显示来自 URL 的图像
【发布时间】:2021-01-09 10:28:12
【问题描述】:

我正在开发一个天气应用程序并添加一些我正在考虑添加天气地图的香料,所以联系https://openweathermap.org/api/weathermaps 并获得了一个带有图像的 URL。我研究了许多在 Tkinter Widget 上显示该图像的方法,但它们都不起作用。它显示图像的大小,但不显示图像本身。这是我的代码。非常感谢。

from tkinter import *
from PIL import ImageTk, Image
import requests
import urllib.request
import base64

root = Tk()
root.title("Weather")


link = "https://tile.openweathermap.org/map/pressure_new/0/0/0.png?appid={APIkey}"

class WebImage:
     def __init__(self,url):
          u = urllib.request.urlopen(url)
          raw_data = u.read()
          u.close()
          self.image = PhotoImage(data=base64.encodebytes(raw_data))

     def get(self):
          return self.image

img = WebImage(link_6).get()
imagelab = Label(root, image = img)
imagelab.grid(row = 0, column = 0)

root.mainloop()

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    如果链接中的图像是 PNG,则您的代码可以正常工作。可能是链接中的图像是tkinter.PhotoImage 不支持的JPEG。

    您可以使用支持各种图像格式的Pillow模块:

    import tkinter as tk
    import urllib.request
    #import base64
    import io
    from PIL import ImageTk, Image
    
    root = tk.Tk()
    root.title("Weather")
    
    link = "https://openweathermap.org/themes/openweathermap/assets/img/logo_white_cropped.png"
    
    class WebImage:
        def __init__(self, url):
            with urllib.request.urlopen(url) as u:
                raw_data = u.read()
            #self.image = tk.PhotoImage(data=base64.encodebytes(raw_data))
            image = Image.open(io.BytesIO(raw_data))
            self.image = ImageTk.PhotoImage(image)
    
        def get(self):
            return self.image
    
    img = WebImage(link).get()
    imagelab = tk.Label(root, image=img)
    imagelab.grid(row=0, column=0)
    
    root.mainloop()
    

    【讨论】:

    • 它可以工作,尽管当我将它全部插入一个函数时(这样我可以在按下按钮时显示图像)它不再工作了。我该怎么办?
    • 看你在函数中如何实现。
    • 我计划执行以下操作:` def submit(): class WebImage: def __init__(self, url): with urllib.request.urlopen(url) as u: raw_data = u.read () image = Image.open(io.BytesIO(raw_data)) self.image = ImageTk.PhotoImage(image) def get(self): return self.image img = WebImage(link).get() imagelab = tk.Label (root, image=img) imagelab.grid(row=1, column=0) tk.Button(root, text = "Submit", command = submit).grid(row = 0, column = 0) `
    • 您需要保存图片的引用:imagelab.image = img
    • 我应该在哪里添加“image.image = img”到我的代码中?
    【解决方案2】:

    在这里试试这个:

    from tkinter import *
    from PIL import ImageTk, Image
    import requests
    from io import BytesIO
    
    
    root = Tk()
    root.title("Weather")
    
    
    link = "yourlink/image.jpg"
    
    class WebImage:
         def __init__(self,url):
              u = requests.get(url)
              self.image = ImageTk.PhotoImage(Image.open(BytesIO(u.content)))
              
         def get(self):
              return self.image
    
    img = WebImage(link).get()
    imagelab = Label(root, image = img)
    imagelab.grid(row = 0, column = 0)
    
    root.mainloop()
    

    【讨论】:

    • 我尝试了您的代码,但出现以下错误:TypeError: WebImage() 不接受任何参数。当我从 WebImage 中取出参数“链接”时,我得到了另一个错误: AttributeError: 'WebImage' object has no attribute 'image' 。无论如何,谢谢你的帮助。
    • @LorenzoHsu 你可能错过了__init__() 方法中的url 参数
    猜你喜欢
    • 1970-01-01
    • 2021-06-26
    • 2016-11-05
    • 1970-01-01
    • 2014-06-04
    • 2017-10-09
    • 2017-04-20
    • 2018-12-13
    相关资源
    最近更新 更多