如何在画布上的 Tkinter 中打开 PIL 图像 [英] How to open PIL Image in Tkinter on Canvas

查看:34
本文介绍了如何在画布上的 Tkinter 中打开 PIL 图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法让我的 PIL 图像在画布上工作.代码:

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

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:Python27libsite-packagesPILImageTk.py", line 109, in __init__
    mode = Image.getmodebase(mode)
  File "C:Python27libsite-packagesPILImage.py", line 245, in getmodebase
    return ImageMode.getmode(mode).basemode
  File "C:Python27libsite-packagesPILImageMode.py", line 50, in getmode
    return _modes[mode]
KeyError: 'ball.gif'

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

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 Image,然后使用它来创建 PhotoImage.

Try creating a PIL Image first, then using that to create the 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()

这篇关于如何在画布上的 Tkinter 中打开 PIL 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆