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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18369936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:35:05  来源:igfitidea点击:

How to open PIL Image in Tkinter on Canvas

pythontkinterpython-imaging-librarytkinter-canvas

提问by user164814

I can't seem to get my PIL Image to work on canvas. Code:

我似乎无法让我的 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()

Error:

错误:

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'

I need to use PIL images not PhotoImages because I want to resize my images. Please don't suggest switching to Pygame because I want to use Tkinter.

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

回答by Brionius

Try creating a PIL Image first, then using that to create the PhotoImage.

首先尝试创建一个 PIL 图像,然后使用它来创建 PhotoImage。

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()

回答by user2866860

You can import multiple image formats, and resize with this code. "basewidth" sets the width of your image.

您可以导入多种图像格式,并使用此代码调整大小。“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()

回答by One

(An old question, but the answers so far are only half-complete.)

(一个老问题,但到目前为止的答案只完成了一半。)

Read the docs:

阅读文档:

class PIL.ImageTk.PhotoImage(image=None, size=None, **kw)
  • image– Either a PIL image, or a mode string. [...]
  • file– A filename to load the image from (using Image.open(file)).
  • image– PIL 图像或模式字符串。[...]
  • file– 从中加载图像的文件名(使用Image.open(file))。

So in your example, use

所以在你的例子中,使用

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

or explicitly

或明确

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

(And remember – as you did correctly: Keep a reference to the image object in your Python program, otherwise it is garbage-collected before you seee it.)

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

回答by DanCan

I was banging my head against the wall for a while on this issue until I found the following:

我在这个问题上撞墙了一段时间,直到我发现以下内容:

http://effbot.org/pyfaq/why-do-my-tkinter-images-not-appear.htm

http://effbot.org/pyfaq/why-do-my-tkinter-images-not-appear.htm

Apparently, Python's garbage collector can trash the ImageTk object. I imagine apps using alot of widgets (like mine) are more susceptible to this behavior.

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