Python 如何在画布上的Tkinter中打开PIL图像

Python 如何在画布上的Tkinter中打开PIL图像,python,tkinter,python-imaging-library,tkinter-canvas,Python,Tkinter,Python Imaging Library,Tkinter Canvas,我似乎无法让我的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=

我似乎无法让我的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'
回溯(最近一次呼叫最后一次):
文件“C:/Users/Mark Malkin/Desktop/3d Graphics Testing/afdds.py”,第7行,在
image=ImageTk.PhotoImage(“ball.gif”)
文件“C:\Python27\lib\site packages\PIL\ImageTk.py”,第109行,在\uuu init中__
mode=Image.getmodebase(模式)
getmodebase中的文件“C:\Python27\lib\site packages\PIL\Image.py”,第245行
返回ImageMode.getmode(mode).basemode
文件“C:\Python27\lib\site packages\PIL\ImageMode.py”,第50行,在getmode中
返回模式[模式]
KeyError:'ball.gif'

我需要使用PIL图像而不是照片图像,因为我想调整图像的大小。请不要建议切换到Pygame,因为我想使用Tkinter。

尝试先创建PIL图像,然后使用该图像创建照片图像

from Tkinter import *
import Image, ImageTk
root = Tk()
root.geometry('1000x1000')
canvas = Canvas(root,width=999,height=999)
canvas.pack()
pilImage = Image.open("ball.gif")
image = ImageTk.PhotoImage(pilImage)
imagesprite = canvas.create_image(400,400,image=image)
root.mainloop()

您可以导入多种图像格式,并使用此代码调整大小。“basewidth”设置图像的宽度

from Tkinter import *
import PIL
from PIL import ImageTk, Image

root=Tk()
image = Image.open("/path/to/your/image.jpg")
canvas=Canvas(root, height=200, width=200)
basewidth = 150
wpercent = (basewidth / float(image.size[0]))
hsize = int((float(image.size[1]) * float(wpercent)))
image = image.resize((basewidth, hsize), PIL.Image.ANTIALIAS)
photo = ImageTk.PhotoImage(image)
item4 = canvas.create_image(100, 80, image=photo)

canvas.pack(side = TOP, expand=True, fill=BOTH)
root.mainloop()
(这是一个老问题,但到目前为止答案只完成了一半。)

阅读文档:

class PIL.ImageTk.PhotoImage(image=None, size=None, **kw)
  • 图像
    –PIL图像或模式字符串。[……]
  • 文件
    –从中加载图像的文件名(使用
    image.open(file)
因此,在您的示例中,使用

image = ImageTk.PhotoImage(file="ball.gif")
或者明确地

image = ImageTk.PhotoImage(Image("ball.gif"))

(请记住——正如您所做的那样:在Python程序中保留对image对象的引用,否则在看到它之前,它会被垃圾收集。)

我在这个问题上撞了一会儿,直到发现以下内容:


显然,Python的垃圾收集器可以垃圾处理ImageTk对象。我想使用很多小部件(比如我的)的应用程序更容易受到这种行为的影响。

我很困惑-你说你不想使用
PhotoImage
s,但你的代码使用的是
PhotoImage
。你是说你想使用
ImageTk.PhotoImage
而不是
Tkinter.PhotoImage
?你试过阅读
PhotoImage
的文档吗?它采用图像对象、模式和大小。你也没有通过它;您正在传递一个文件名。(在
return\u modes[mode]
上的
KeyError
很明显,它试图将文件名视为一种模式……但不管它尝试哪种模式,它都会失败。)ImageTk.PhotoImage是我想要使用的,因为我想成为一个能够调整大小的人。raise importorror(“未安装图像C模块”)ImportError:_ImagingC模块未安装installed@user164814啊,那你会玩得很开心的。您缺少一个PIL二进制文件-这是您的PIL安装的问题,而不是代码的问题。看见如果您使用MacPorts安装PIL,请尝试自己安装系统构建。祝你好运。@Brionius,如何保存此图像?@Mulagala你应该打开一个新问题来获得答案。