ImageTk.PhotoImage(request.content)
【以下回答由 GPT 生成】
你可以使用io.BytesIO
模块来将request
返回的内容转化为BytesIO
对象,然后再使用PIL
库来打开这个对象,并将其转化为PhotoImage
对象。下面是示例代码:
import requests
import io
from PIL import Image, ImageTk
import tkinter as tk
# 发送请求获取图片内容
response = requests.get(url)
content = response.content
# 将内容转化为BytesIO对象
image_data = io.BytesIO(content)
# 使用PIL库打开BytesIO对象
image = Image.open(image_data)
# 创建PhotoImage对象
photo = ImageTk.PhotoImage(image)
# 显示图片
root = tk.Tk()
label = tk.Label(root, image=photo)
label.pack()
root.mainloop()
请将上述代码中的url
替换为你要请求的图片地址。希望能帮助到你!