-
-
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() |
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)
So simple!
thank you ♥
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 ...
@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()
It is great. Thanks a lot!
Nice routine.
I've modded it to process time-lapse images.
Thanks for the code.