在这里,我尝试运行一个代码来接收图像并调整大小,然后使用带有文本的tkinter框架打开它。
root2 = Tk. Tk()
filename = "pichart.png"
basewidth = 500
img = Image.open(filename)
wpercent = (basewidth / float(img.size[0]))
hsize = int((float(img.size[1]) * float(wpercent)))
img = img.resize((basewidth, hsize), Image.ANTIALIAS)
os.remove(filename) # deletes the original image after you have got the resized image
img.save('pichart.png')
bard = Image.open("pichart.png")
bardejov = ImageTk.PhotoImage(bard)
label1 = Tk.Label(root2,image=bardejov)
label1.image = bardejov
label1.place(x=20, y=20)
samp="sfaagdgaegadgadfasasdasda"
label2 = Label(root2, text=samp)
label2.place(x=600, y=20)
root2.geometry("%dx%d+%d+%d" % (1400, 700, 00, 00))
root2.mainloop()
图像被转换并保存但在显示时会出现错误:
Traceback (most recent call last):
File "E:\Mainproject\Best final backup - Copy\sentimentsummarizer.py", line 611, in <module>
label1 = Tk.Label(root2,image=bardejov)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 2475, in __init__
Widget.__init__(self, master, 'label', cnf, kw)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1974, in __init__
(widgetName, self._w) + extra + self._options(cnf))
TclError: image "pyimage10" doesn't exist
虽然单独运行显示片段可以很好地处理指令中的图像。