Skip to content

Instantly share code, notes, and snippets.

@terencewu
Created March 8, 2015 03:03
Show Gist options
  • Save terencewu/034e09f0e318c621516b to your computer and use it in GitHub Desktop.
Save terencewu/034e09f0e318c621516b to your computer and use it in GitHub Desktop.
'''
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):
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):
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 = '.'
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)
self.showImage(myimage)
#its like a callback function after n seconds (cycle through pics)
self.after(delay*1000, self.startSlideShow)
def showImage(self, filename):
image = Image.open(filename)
img_w, img_h = image.size
scr_w, scr_h = self.winfo_screenwidth(), self.winfo_screenheight()
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))
# create new image
self.persistent_image = ImageTk.PhotoImage(image)
self.label.configure(image=self.persistent_image)
slideShow = HiddenRoot()
slideShow.bind("<Escape>", lambda e: slideShow.destroy()) # exit on esc
slideShow.mainloop()
@iainrs
Copy link

iainrs commented Aug 21, 2018

Nice routine.
I've modded it to process time-lapse images.
Thanks for the code.

@isdito
Copy link

isdito commented Aug 3, 2020

Hello I am searching the form to create slider with transition but ... Not works. Any sugestion why not change the image inside the while. reggards

while 1.0 > alpha:
				
				fini = (Image.blend(old_resized,resized,alpha)).convert("RGB") 
				im2 = Image.new('RGB',old_resized.size)
				alpha = alpha + 0.1
				self.picture_display.configure(text="%i s" % alpha)
				new_img = ImageTk.PhotoImage(fini)
				self.picture_display.configure(image=new_img)
				self.picture_display.image = new_img
				
				self.title(os.path.basename(x))
				
			self.after(self.delay, self.show_slides)

@idopongo
Copy link

So simple!
thank you ♥

@ken-techno
Copy link

Hello! Appreciate this is 6 years old but could anyone help a newbie? I have Python 3.9.1 on MacOS Catalina but when I run this neat script I just get a list of files in the shell window, not opened in a picture viewer. Any ideas? I think I have installed PIL and Tkinter correctly ...

@rajputankur56
Copy link

@ken-techno same thing happened with me, check with below code to confirm if you there is anything wrong with your machine or code. This worked for me.

import tkinter as tk
from tkinter import *
from PIL import Image
from PIL import ImageTk
import os

root = tk.Tk()
root.geometry("800x800")

l = Label()
l.pack()
img_fp = ImageTk.PhotoImage(Image.open('IMAGE_PATH'))
l.config(image=img_fp)
root.mainloop()

@QuantoC
Copy link

QuantoC commented Aug 18, 2023

It is great. Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment