Python 使用枕头进行图像处理时出现的奇怪错误(在tkinter中)

Python 使用枕头进行图像处理时出现的奇怪错误(在tkinter中),python,jpeg,gif,pillow,Python,Jpeg,Gif,Pillow,在tkinter中显示图像时,我遇到了一个奇怪的问题。 我最初尝试以默认的tkinter方式显示图像,这对GIF很有效: import tkinter as tk root = tk.Tk() src = tk.PhotoImage(file = "C:\\Users\\Matt\\Desktop\\K8pnR.gif") label = tk.Label(root, image = src) label.pack() (这只是我在imgur上找到的随机gif) 这很好,但唯一的问题是我想显

在tkinter中显示图像时,我遇到了一个奇怪的问题。
我最初尝试以默认的tkinter方式显示图像,这对GIF很有效:

import tkinter as tk
root = tk.Tk()
src = tk.PhotoImage(file = "C:\\Users\\Matt\\Desktop\\K8pnR.gif")

label = tk.Label(root, image = src)
label.pack()
(这只是我在imgur上找到的随机gif)
这很好,但唯一的问题是我想显示其他文件类型。这让我想到了枕头,因为我正在使用Python 3.4。我试图从显示相同的文件开始,但使用枕头:

import tkinter as tk
from PIL import Image
root = tk.Tk()

src = Image.open("C:\\Users\\Matt\\Desktop\\K8pnR.gif")
img = tk.PhotoImage(file = src)

label = tk.Label(image = img, master = root)
label.pack()
这导致了一个非常奇怪和丑陋的错误,没有这样的文件或目录:

Traceback (most recent call last):
File "C:\Users\Matt\Desktop\pil test.py", line 7, in <module>
  img = tk.PhotoImage(file = src)
File "C:\Python34\lib\tkinter\__init__.py", line 3416, in __init__
  Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Python34\lib\tkinter\__init__.py", line 3372, in __init__
  self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "<PIL.GifImagePlugin.GifImageFile image   mode=P size=494x260 at 0x26A6CD0>": no such file or directory
回溯(最近一次呼叫最后一次):
文件“C:\Users\Matt\Desktop\pil test.py”,第7行,在
img=tk.PhotoImage(file=src)
文件“C:\Python34\lib\tkinter\\uuuu init\uuuu.py”,第3416行,在\uuu init中__
图像。_u初始(自我,“照片”,名称,cnf,主机,**千瓦)
文件“C:\Python34\lib\tkinter\\uuuu init\uuuu.py”,第3372行,在\uuu init中__
self.tk.call(('image','create',imgtype,name,)+选项)
_tkinter.TclError:无法打开“”:没有此类文件或目录
我尝试了不同的文件、不同的文件类型,甚至重新安装了Pillow,但仍然出现了错误。
有人知道这里发生了什么吗?我是不是错过了一些显而易见的东西

编辑:
当我尝试修复时,会出现以下可怕的错误:

Traceback (most recent call last):
File "C:\Users\Matt\Desktop\pil test.py", line 6, in <module>
  img = ImageTk.PhotoImage(file = src)
File "C:\Python34\lib\site-packages\PIL\ImageTk.py", line 84, in __init__
  image = Image.open(kw["file"])
File "C:\Python34\lib\site-packages\PIL\Image.py", line 2297, in open
  prefix = fp.read(16)
File "C:\Python34\lib\site-packages\PIL\Image.py", line 632, in __getattr__
  raise AttributeError(name)
AttributeError: read
回溯(最近一次呼叫最后一次):
文件“C:\Users\Matt\Desktop\pil test.py”,第6行,在
img=ImageTk.PhotoImage(文件=src)
文件“C:\Python34\lib\site packages\PIL\ImageTk.py”,第84行,在\uuu init中__
image=image.open(kw[“文件”])
文件“C:\Python34\lib\site packages\PIL\Image.py”,第2297行,打开
前缀=fp.read(16)
文件“C:\Python34\lib\site packages\PIL\Image.py”,第632行,在\uuu getattr中__
提升属性错误(名称)
属性错误:读取

问题在于这一行:

img = tk.PhotoImage(file = src)
您正在使用tkinter的stock
PhotoImage
。它与要从
PIL
使用
ImageTk
PIL
不兼容

import tkinter as tk
from PIL import Image, ImageTk
root = tk.Tk()

src = Image.open("C:\\Users\\Matt\\Desktop\\K8pnR.gif")
img = ImageTk.PhotoImage(file = src)

label = tk.Label(image = img, master = root)
label.pack()

下面是stock
PhotoImage
class:,它只接受构造函数中的路径。

问题在于这一行:

img = tk.PhotoImage(file = src)
您正在使用tkinter的stock
PhotoImage
。它与要从
PIL
使用
ImageTk
PIL
不兼容

import tkinter as tk
from PIL import Image, ImageTk
root = tk.Tk()

src = Image.open("C:\\Users\\Matt\\Desktop\\K8pnR.gif")
img = ImageTk.PhotoImage(file = src)

label = tk.Label(image = img, master = root)
label.pack()

这是stock
PhotoImage
class:,它只接受构造函数中的路径。

我得到另一个奇怪的错误。。。请检查我的编辑。很抱歉延迟,问题似乎是
file=src
。不要给那个参数命名或正确命名,也就是说,
image=src
。。。非常感谢。我知道这可能是我错过的一件小事。。。我在扯头发。你真棒!我又犯了一个奇怪的错误。。。请检查我的编辑。很抱歉延迟,问题似乎是
file=src
。不要给那个参数命名或正确命名,也就是说,
image=src
。。。非常感谢。我知道这可能是我错过的一件小事。。。我在扯头发。你真棒!