Advertisement
Guest User

Random Slideshow

a guest
Oct 18th, 2018
500
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.37 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. # Using Python 3.5.4
  4.    
  5. import tkinter as Tk
  6. import os
  7. from PIL import Image, ImageTk
  8. import random
  9.  
  10. root = Tk.Tk()
  11.  
  12. ############## PARAMETER ##############
  13.  
  14. # Change the directory that is going to be scanned
  15. path = r"C:\PATH\TO\THE\PICTURES\FOLDER"
  16.  
  17. #######################################
  18.  
  19. print("Start of indexation")
  20. print("Indexation can last some time")
  21.  
  22. #loading all the pictures in a list
  23. imagelist = []
  24. for dirpath, subdirs, files in os.walk(path):
  25.     for x in files:
  26.         try : image = Image.open(os.path.join(dirpath, x))
  27.         except : pass
  28.         photo = ImageTk.PhotoImage(image)
  29.         imagelist.append(photo)
  30.        
  31. print(str(len(imagelist)) + " pictures loaded")
  32.  
  33. if len(imagelist) == 0:
  34.     raise Exception("no pictures to show in this directory")
  35.    
  36.    
  37. #this label will be used to show pictures
  38. lbl = Tk.Label(root)
  39.  
  40. print("Starting the slideshow")
  41.  
  42. def slideshow():
  43.     ok = False
  44.     while ok == False:
  45.         random.seed()
  46.         try:
  47.             #choosing a random picture
  48.             lbl.config(image = imagelist[random.randint(0, len(imagelist)-1)])
  49.             ok = True
  50.         except:
  51.             pass
  52.     random.seed()
  53.     root.after(random.randint(500, 2500), slideshow)  #recalling the function after some time
  54.  
  55. lbl.pack()
  56. root.after(100, slideshow)
  57. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement