我似乎无法让我的PIL图像在画布上工作。代码:如何在画布上打开Tkinter中的PIL图像
from Tkinter import*
import Image, ImageTk
root = Tk()
root.geometry('1000x1000')
canvas = Canvas(root,width=999,height=999)
canvas.pack()
image = ImageTk.PhotoImage("ball.gif")
imagesprite = canvas.create_image(400,400,image=image)
root.mainloop()
错误:
Traceback (most recent call last):
File "C:/Users/Mark Malkin/Desktop/3d Graphics Testing/afdds.py", line 7, in <module>
image = ImageTk.PhotoImage("ball.gif")
File "C:\Python27\lib\site-packages\PIL\ImageTk.py", line 109, in __init__
mode = Image.getmodebase(mode)
File "C:\Python27\lib\site-packages\PIL\Image.py", line 245, in getmodebase
return ImageMode.getmode(mode).basemode
File "C:\Python27\lib\site-packages\PIL\ImageMode.py", line 50, in getmode
return _modes[mode]
KeyError: 'ball.gif'
我需要使用PIL图像不PhotoImages因为我想调整我的图片。请不要建议切换到Pygame,因为我想使用Tkinter。
我很困惑 - 你说你不想使用'PhotoImage's,但你的代码使用'PhotoImage'。你的意思是你想使用'ImageTk.PhotoImage'而不是'Tkinter.PhotoImage'? – Brionius
你有没有试过阅读'PhotoImage'的文档?它需要一个图像对象,或者一个模式和一个尺寸。你也没有通过它;你传递一个文件名。 ('return _modes [mode]'上的'KeyError'很明显它试图将文件名视为一种模式...但是尝试使用哪一个并不重要,它会以任何方式失败。) – abarnert