我似乎无法在画布上使用我的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
,但是你的代码却使用了PhotoImage
。你的意思是想使用ImageTk.PhotoImage
而不是Tkinter.PhotoImage
吗? - BrioniusPhotoImage
的文档吗?它需要一个图像对象或模式和大小。你没有传递任何一个;你传递了一个文件名。(在return _modes [mode]
上的KeyError
非常明显,它试图将文件名视为模式...但无论它尝试哪个,它都会失败。) - abarnert