SHARE
TWEET

Untitled

a guest Oct 21st, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '''
  2. Python 3 slideshow using tkinter and pillow (PIL)
  3. Usage: python3 slideShow.py [img_directory]
  4. '''
  5.  
  6. import tkinter as tk
  7. from PIL import Image, ImageTk
  8. import time
  9. import sys
  10. import os
  11.  
  12. class HiddenRoot(tk.Tk):
  13.     padding=3
  14.     dimensions="{0}x{1}+0+0"
  15.     def __init__(self):
  16.         tk.Tk.__init__(self)
  17.         #hackish way, essentially makes root window
  18.         #as small as possible but still "focused"
  19.         #enabling us to use the binding on <esc>
  20.        
  21.         self.wm_geometry("0x0+0+0")
  22.  
  23.         self.window = MySlideShow(self)
  24.         self.window.startSlideShow()
  25.  
  26.  
  27. class MySlideShow(tk.Toplevel):
  28.     slideshow = True
  29.     pad = ''
  30.     def __init__(self, *args, **kwargs):
  31.         tk.Toplevel.__init__(self, *args, **kwargs)
  32.         #remove window decorations
  33.         self.overrideredirect(True)
  34.  
  35.         #save reference to photo so that garbage collection
  36.         #does not clear image variable in show_image()
  37.         self.persistent_image = None
  38.         self.imageList = []
  39.         self.pixNum = 0
  40.  
  41.         #used to display as background image
  42.         self.label = tk.Label(self)
  43.         self.label.pack(side="top", fill="both", expand=True)
  44.  
  45.         self.getImages()
  46.  
  47.     def getImages(self):
  48.         '''
  49.         Get image directory from command line or use current directory
  50.         '''
  51.         if len(sys.argv) == 2:
  52.             curr_dir = sys.argv[1]
  53.         else:
  54.             curr_dir = './images'
  55.  
  56.         for root, dirs, files in os.walk(curr_dir):
  57.             for f in files:
  58.                 if f.endswith(".png") or f.endswith(".jpg"):
  59.                     img_path = os.path.join(root, f)
  60.                     print(img_path)
  61.                     self.imageList.append(img_path)
  62.  
  63.     def startSlideShow(self, delay=4): #delay in seconds
  64.         myimage = self.imageList[self.pixNum]
  65.         self.pixNum = (self.pixNum + 1) % len(self.imageList)
  66.         if self.slideshow == True:
  67.             print(myimage)
  68.             self.showImage(myimage)
  69.             #its like a callback function after n seconds (cycle through pics)
  70.             self.after(delay*1000, self.startSlideShow)
  71.         else:
  72.             self.numpad()
  73.  
  74.     def numpad(self):
  75.         numpadImage = Image.open('./keypad.png')
  76.         padding=50
  77.         #numpadImage = numpadImage.resize((250, 250), Image.ANTIALIAS)
  78.        
  79.         # create new image
  80.         self.numpadImage = ImageTk.PhotoImage(numpadImage)
  81.        
  82.         self.label.configure(image=self.numpadImage)
  83.         self.label.configure(bg='systemTransparent', cursor='none')
  84.    
  85.     def callback(self, something):
  86.         print("click", something.x, something.y)
  87.         if (self.winfo_screenwidth()) * .8 < something.x and (self.winfo_screenheight()) * .2 > something.y:
  88.             #Top Right
  89.             print("Top Right")
  90.             self.pad += "tr"
  91.            
  92.         elif (self.winfo_screenwidth()) * .2 > something.x and (self.winfo_screenheight()) * .8 > something.y:
  93.             #Top Left
  94.             print("Top Left")
  95.             self.pad += "tl"
  96.  
  97.         elif (self.winfo_screenwidth()) * .2 > something.x and (self.winfo_screenheight()) * .8 < something.y:
  98.             #Bottom Left
  99.             print("Bottom Left")
  100.             self.pad += "bl"
  101.  
  102.         elif (self.winfo_screenwidth()) * .8 < something.x and (self.winfo_screenheight()) * .8 < something.y:
  103.             #Bottom Right
  104.             print("Bottom Right")
  105.             self.pad += "br"
  106.         elif (self.winfo_screenwidth()) * .6 > something.x and (self.winfo_screenheight()) * .3 < something.x and (self.winfo_screenwidth()) * .6 > something.y and (self.winfo_screenheight()) * .3 < something.y :
  107.             print("center")
  108.             print(self.pad)
  109.             self.pad = ''
  110.            
  111.  
  112.         if self.slideshow == "BLBRTRTL":
  113.             self.slideshow = False
  114.             self.numpad()
  115.  
  116.  
  117.     def showImage(self, filename):
  118.         image = Image.open(filename)  
  119.  
  120.         img_w, img_h = image.size
  121.         padding=0
  122.         scr_w, scr_h = self.winfo_screenwidth()-padding, self.winfo_screenheight()-padding
  123.         width, height = min(scr_w, img_w), min(scr_h, img_h)
  124.         image.thumbnail((width, height), Image.ANTIALIAS)
  125.  
  126.         #set window size after scaling the original image up/down to fit screen
  127.         #removes the border on the image
  128.         scaled_w, scaled_h = image.size
  129.         #self.wm_geometry("{}x{}+{}+{}".format(scaled_w,scaled_h,0,0))
  130.        
  131.         self.bind("<Button-1>", self.callback)
  132.         dimensions="{0}x{1}+0+0"
  133.         width=self.winfo_screenwidth()
  134.         height=self.winfo_screenheight()
  135.  
  136.         self.wm_geometry(dimensions.format(width, height))
  137.        
  138.         # create new image
  139.         self.persistent_image = ImageTk.PhotoImage(image)
  140.         self.label.configure(image=self.persistent_image)
  141.         self.label.configure(bg='black', cursor='none')
  142.  
  143.  
  144. slideShow = HiddenRoot()
  145. slideShow.bind("<Escape>", lambda e: slideShow.destroy())  # exit on esc
  146. slideShow.mainloop()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top