Python 用枕头处理JPG格式

Python 用枕头处理JPG格式,python,tkinter,python-imaging-library,jpeg,Python,Tkinter,Python Imaging Library,Jpeg,代码试图通过Pillow的ImageTk模块将jpg格式的图片加载到Tkinter。但是,系统生成了以下错误: from tkinter import * from glob import glob import demoCheck import random import quitter from PIL import ImageTk gifdir = 'd:/Python/Jupyter/Programming Python/Chap 8 - GUI/pic/' class butto

代码试图通过Pillow的ImageTk模块将jpg格式的图片加载到Tkinter。但是,系统生成了以下错误:

from tkinter import *
from glob import glob
import demoCheck
import random
import quitter
from PIL import ImageTk

gifdir = 'd:/Python/Jupyter/Programming Python/Chap 8 - GUI/pic/'


class buttonpics(Frame):
    def __init__(self, parent=None, dir='./gifs/', **kwargs):
        Frame.__init__(self, parent, **kwargs)
        self.pack()
        self.lbl = Label(self, text='None', bg='blue', fg='red')
        self.lbl.pack(fill=BOTH)
        self.btn = Button(self, text='Press me', bg='white', command=self.draw)
        self.btn.pack()
        self.quitter = quitter.Quitter(self)
        self.quitter.pack(anchor=S)
        self.files = glob(dir+'*.jpg')
        self.images = [(file, ImageTk.PhotoImage(file=file))
                       for file in self.files]

    def draw(self):
        name, photo = random.choice(self.images)
        self.btn.config(image=photo)
        self.lbl.config(text=name)


buttonpics(dir=gifdir).mainloop()
>回溯(最近一次调用上次):文件
>“d:/Python/Jupyter/Programming Python/Chap 8-
>GUI/buttonpics func.py”,第31行,在
>buttonpics(dir=gifdir).mainloop()文件“d:/Python/Jupyter/Programming Python/Chap 8-
>GUI/buttonpics func.py”,第22行,在初始化中__
>self.images=[(file,ImageTk.PhotoImage(file=file))file“d:/Python/Jupyter/Programming Python/Chap 8-
>GUI/buttonpics func.py”,第22行,在
>self.images=[(文件,ImageTk.PhotoImage(文件=file))文件“C:\ProgramData\Anaconda3\lib\site packages\PIL\ImageTk.py”,第89行,
>在初始化中__
>image=\u从\u kw(kw)文件“C:\ProgramData\Anaconda3\lib\site packages\PIL\ImageTk.py”第58行获取\u image,
>在中,从中获取图像
>返回Image.open(源代码)文件“C:\ProgramData\Anaconda3\lib\site packages\PIL\Image.py”,第2930行,
>公开
>引发UnidentifiedImageError(PIL.UnidentifiedImageError:无法识别图像文件'd:/Python/Jupyter/Programming Python/Chap 8-
>GUI/pic\\IMG_4147.jpg中忽略异常:PhotoImage.\uu del_uuuuat 0x00000226CF6CFD30>回溯(最近调用
>最后):文件
>“C:\ProgramData\Anaconda3\lib\site packages\PIL\ImageTk.py”,第118行,
>在__
>name=self.\u photo.name AttributeError:'PhotoImage'对象没有属性'\u PhotoImage\u photo'

如果扩展名更改为png,程序可以顺利运行。这是由于jpg格式的兼容性问题还是其他原因造成的?感谢您的帮助!

始终将完整的错误消息(从单词“Traceback”开始)作为文本(而非屏幕截图)放在问题中(不是注释)。还有其他有用的信息。
pillow
应该可以使用
。jpg
没有问题。我从来没有遇到过
jpg
的问题,但是
pillow
可能会使用外部C/C++库来处理不同的格式,如果您没有用于
jpg
的C/C++库,那么它可能会有此格式的问题。谢谢或者通知。我已经发布了完整的错误消息。当我尝试打开非图像的文件时,我会收到相同的
UnidentifiedImageError
。因此,您可能会遇到两个问题之一:(1)您的
img4147.jpg
不是图像或它不是jpg(尝试在任何imge查看器中打开它,看看它是否可以打开并显示其属性)。(2)pilow需要-要使用JPG,它需要C/C++库
libjpeg
,您可能需要手动安装它。
> Traceback (most recent call last):   File
> "d:/Python/Jupyter/Programming Python/Chap 8 -
> GUI/buttonpics-func.py", line 31, in <module>
>     buttonpics(dir=gifdir).mainloop()   File "d:/Python/Jupyter/Programming Python/Chap 8 -
> GUI/buttonpics-func.py", line 22, in __init__
>     self.images = [(file, ImageTk.PhotoImage(file=file))   File "d:/Python/Jupyter/Programming Python/Chap 8 -
> GUI/buttonpics-func.py", line 22, in <listcomp>
>     self.images = [(file, ImageTk.PhotoImage(file=file))   File "C:\ProgramData\Anaconda3\lib\site-packages\PIL\ImageTk.py", line 89,
> in __init__
>     image = _get_image_from_kw(kw)   File "C:\ProgramData\Anaconda3\lib\site-packages\PIL\ImageTk.py", line 58,
> in _get_image_from_kw
>     return Image.open(source)   File "C:\ProgramData\Anaconda3\lib\site-packages\PIL\Image.py", line 2930,
> in open
>     raise UnidentifiedImageError( PIL.UnidentifiedImageError: cannot identify image file 'd:/Python/Jupyter/Programming Python/Chap 8 -
> GUI/pic\\IMG_4147.jpg' Exception ignored in: <function
> PhotoImage.__del__ at 0x00000226CF6CFD30> Traceback (most recent call
> last):   File
> "C:\ProgramData\Anaconda3\lib\site-packages\PIL\ImageTk.py", line 118,
> in __del__
>     name = self.__photo.name AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'