Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Python 3 slideshow using tkinter and pillow (PIL)
- Usage: python3 slideShow.py [img_directory]
- '''
- import tkinter as tk
- from PIL import Image, ImageTk
- import time
- import sys
- import os
- class HiddenRoot(tk.Tk):
- padding=3
- dimensions="{0}x{1}+0+0"
- def __init__(self):
- tk.Tk.__init__(self)
- #hackish way, essentially makes root window
- #as small as possible but still "focused"
- #enabling us to use the binding on <esc>
- self.wm_geometry("0x0+0+0")
- self.window = MySlideShow(self)
- self.window.startSlideShow()
- class MySlideShow(tk.Toplevel):
- slideshow = True
- pad = ''
- def __init__(self, *args, **kwargs):
- tk.Toplevel.__init__(self, *args, **kwargs)
- #remove window decorations
- self.overrideredirect(True)
- #save reference to photo so that garbage collection
- #does not clear image variable in show_image()
- self.persistent_image = None
- self.imageList = []
- self.pixNum = 0
- #used to display as background image
- self.label = tk.Label(self)
- self.label.pack(side="top", fill="both", expand=True)
- self.getImages()
- def getImages(self):
- '''
- Get image directory from command line or use current directory
- '''
- if len(sys.argv) == 2:
- curr_dir = sys.argv[1]
- else:
- curr_dir = './images'
- for root, dirs, files in os.walk(curr_dir):
- for f in files:
- if f.endswith(".png") or f.endswith(".jpg"):
- img_path = os.path.join(root, f)
- print(img_path)
- self.imageList.append(img_path)
- def startSlideShow(self, delay=4): #delay in seconds
- myimage = self.imageList[self.pixNum]
- self.pixNum = (self.pixNum + 1) % len(self.imageList)
- if self.slideshow == True:
- print(myimage)
- self.showImage(myimage)
- #its like a callback function after n seconds (cycle through pics)
- self.after(delay*1000, self.startSlideShow)
- else:
- self.numpad()
- def numpad(self):
- numpadImage = Image.open('./keypad.png')
- padding=50
- #numpadImage = numpadImage.resize((250, 250), Image.ANTIALIAS)
- # create new image
- self.numpadImage = ImageTk.PhotoImage(numpadImage)
- self.label.configure(image=self.numpadImage)
- self.label.configure(bg='systemTransparent', cursor='none')
- def callback(self, something):
- print("click", something.x, something.y)
- if (self.winfo_screenwidth()) * .8 < something.x and (self.winfo_screenheight()) * .2 > something.y:
- #Top Right
- print("Top Right")
- self.pad += "tr"
- elif (self.winfo_screenwidth()) * .2 > something.x and (self.winfo_screenheight()) * .8 > something.y:
- #Top Left
- print("Top Left")
- self.pad += "tl"
- elif (self.winfo_screenwidth()) * .2 > something.x and (self.winfo_screenheight()) * .8 < something.y:
- #Bottom Left
- print("Bottom Left")
- self.pad += "bl"
- elif (self.winfo_screenwidth()) * .8 < something.x and (self.winfo_screenheight()) * .8 < something.y:
- #Bottom Right
- print("Bottom Right")
- self.pad += "br"
- 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 :
- print("center")
- print(self.pad)
- self.pad = ''
- if self.slideshow == "BLBRTRTL":
- self.slideshow = False
- self.numpad()
- def showImage(self, filename):
- image = Image.open(filename)
- img_w, img_h = image.size
- padding=0
- scr_w, scr_h = self.winfo_screenwidth()-padding, self.winfo_screenheight()-padding
- width, height = min(scr_w, img_w), min(scr_h, img_h)
- image.thumbnail((width, height), Image.ANTIALIAS)
- #set window size after scaling the original image up/down to fit screen
- #removes the border on the image
- scaled_w, scaled_h = image.size
- #self.wm_geometry("{}x{}+{}+{}".format(scaled_w,scaled_h,0,0))
- self.bind("<Button-1>", self.callback)
- dimensions="{0}x{1}+0+0"
- width=self.winfo_screenwidth()
- height=self.winfo_screenheight()
- self.wm_geometry(dimensions.format(width, height))
- # create new image
- self.persistent_image = ImageTk.PhotoImage(image)
- self.label.configure(image=self.persistent_image)
- self.label.configure(bg='black', cursor='none')
- slideShow = HiddenRoot()
- slideShow.bind("<Escape>", lambda e: slideShow.destroy()) # exit on esc
- slideShow.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement