我想将 BytesIO 对象中的图像渲染到画布上,为此我正在使用以下代码:
from io import BytesIO
from Tkinter import *
from PIL import Image as ImageModule
f = open("test.png","rb")
bdata = BytesIO()
bdata.write(f.read())
bdata.seek(0)
pilImage = ImageModule.open(bdata)
canvas_width = 794
canvas_height =559
master = Tk()
canvas = Canvas(master,
width=canvas_width,
height=canvas_height)
canvas.pack()
img = PhotoImage(pilImage)
i = canvas.create_image(0,0, anchor=NW, image=img)
mainloop()
我收到带有画布和标签的错误消息:
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 2058, in __init__
(widgetName, self._w) + extra + self._options(cnf))
TypeError: __str__ returned non-string (type instance)
如果您知道如何修复此代码或以其他方式从 BytesIO 对象在画布上渲染图像,那就太好了。谢谢