title: Handling JPG format by Pillow
body: <pre><code>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()
  </code></pre>
  <p>The codes are trying to load jpg formatted pictures to Tkinter through Pillow's ImageTk module. However, the system generated the following error:</p>
  <pre><code>&gt; Traceback (most recent call last):   File
  &gt; &quot;d:/Python/Jupyter/Programming Python/Chap 8 -
  &gt; GUI/buttonpics-func.py&quot;, line 31, in &lt;module&gt;
  &gt;     buttonpics(dir=gifdir).mainloop()   File &quot;d:/Python/Jupyter/Programming Python/Chap 8 -
  &gt; GUI/buttonpics-func.py&quot;, line 22, in __init__
  &gt;     self.images = [(file, ImageTk.PhotoImage(file=file))   File &quot;d:/Python/Jupyter/Programming Python/Chap 8 -
  &gt; GUI/buttonpics-func.py&quot;, line 22, in &lt;listcomp&gt;
  &gt;     self.images = [(file, ImageTk.PhotoImage(file=file))   File &quot;C:\ProgramData\Anaconda3\lib\site-packages\PIL\ImageTk.py&quot;, line 89,
  &gt; in __init__
  &gt;     image = _get_image_from_kw(kw)   File &quot;C:\ProgramData\Anaconda3\lib\site-packages\PIL\ImageTk.py&quot;, line 58,
  &gt; in _get_image_from_kw
  &gt;     return Image.open(source)   File &quot;C:\ProgramData\Anaconda3\lib\site-packages\PIL\Image.py&quot;, line 2930,
  &gt; in open
  &gt;     raise UnidentifiedImageError( PIL.UnidentifiedImageError: cannot identify image file 'd:/Python/Jupyter/Programming Python/Chap 8 -
  &gt; GUI/pic\\IMG_4147.jpg' Exception ignored in: &lt;function
  &gt; PhotoImage.__del__ at 0x00000226CF6CFD30&gt; Traceback (most recent call
  &gt; last):   File
  &gt; &quot;C:\ProgramData\Anaconda3\lib\site-packages\PIL\ImageTk.py&quot;, line 118,
  &gt; in __del__
  &gt;     name = self.__photo.name AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
  </code></pre>
  <p>If the extension is changed to png, the program can be run smoothly. Does this result from a compatibility issue with jpg format or there are some other reasons? Thank you for the help!</p>