1

我从网上用 aiohttp 抓取了一些图片,并将它们保存在一个列表中,其中包含这样的数组('\xe25\xd7\xeeP{\x08\x18-6\x809\xabQ1LQ\xf0\x02hC\x11\x97* .\xc8...')

我正在尝试使用画布和 Photoimage 显示图片,但它不起作用

这是我的代码的一些片段,在此先感谢,对不起我的英语

bytesfoto=self.fotobytes[indice]
    img = ImageTk.PhotoImage(Image.open(BytesIO(bytesfoto)))
    self.canvas.create_image(20, 20, anchor=NW, image=img)



 self.canvas = Canvas(width=300, height=300)

    self.canvas.grid(row=2, column=1)
4

2 回答 2

0

只要您提供文件格式,您就可以使用字节初始化 Tkinter PhotoImage 类:

image_content = b'...' # Data you scraped from some URL
file_format = 'jpg' # The file extension of the sourced data

canvas = tk.Canvas(window, width=300, height=300)
canvas.pack()
img = tk.PhotoImage(data=image_content, format=file_format)
canvas.create_image(20, 20, anchor=tk.NW, image=img)
于 2021-11-03T01:50:34.160 回答
0

有一种解决方法,但不是很好:您可以将每个图像保存为 jpg,然后使用 PIL 重新打开,然后使用 Photoimage 显示,但这需要 python 程序来保存图像。我这样保存它们:

with open(r"example.jpg", 'wb') as f:

    f.write(list_arrays[0])

然后使用 PhotoImage 显示该图像:

root = tk.Tk()

image = PIL.Image.open("example.jpg")

photo = PIL.ImageTk.PhotoImage(image)

ttk.Label(root,image=photo).pack()

root.mainloop()

这不是解决方案,而是一种解决方法,但我希望它有所帮助

于 2021-06-22T09:09:28.807 回答