[Tutor] Python Image Library

Alan Gauld alan.gauld at yahoo.co.uk
Fri May 19 05:29:49 EDT 2017


On 18/05/17 18:06, Alan Gauld via Tutor wrote:

> Here is some untested Tkinter code to display an image
> for 2 seconds:

I tried this last night and it turned out to be harder
than I expected. Eventually I got to bed at 3am! But here
is something that seems to work for jpegs. I hope bmp files
will too, I didn't have any to test...

#################################################
import tkinter as tk
from PIL import Image,ImageTk

def showImage(imgName, delay=2000, wd=400, ht=400):
   '''Display the file imgName
      for delay milliseconds
      in a window sized wd x ht
      Close the window afterwards'''

   top = tk.Tk()
   top.withdraw()  # hide the root window
   win = tk.Toplevel()
   PILimage = Image.open(imgName)
   TKimg = ImageTk.PhotoImage(PILimage)
   tk.Label(win, image=TKimg, width=wd, height=ht).pack()
   top.after(delay, lambda : (win.withdraw(), top.quit()) )
   top.mainloop()


############## Test code #########
import glob,os

root = r"/full/path/to/your/files"

for filename in glob.glob(root + "/*.jpg"):
    print(filename)
    showImage(filename)

############################################


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list