ImageTk.PhotoImage直接读取request不能读取


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替换为你要请求的图片地址。希望能帮助到你!


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^