-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathslideshow.py
More file actions
executable file
·381 lines (298 loc) · 13.5 KB
/
Copy pathslideshow.py
File metadata and controls
executable file
·381 lines (298 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/usr/bin/env python3
import sys
import os
import argparse
import random
import time
import re
import threading
import tkinter
from PIL import Image, ImageTk
class FileManager:
extensions = {'jpeg', 'jpg', 'png'}
files = []
def __init__(self, args):
self.path = args.path
def getFiles(self):
if self.path:
os.chdir(self.path)
with os.scandir() as entries:
for entry in entries:
if (not entry.is_dir()):
if (self.checkFileExtension(entry.name)):
self.files.append(entry.name)
def checkFileExtension(self, file):
extension = os.path.splitext(file)[1]
if (extension.replace('.', '') in self.extensions):
return True
else:
return False
class SlideShow:
screen = {}
currentPosition = 0
pause = False
slide_time = 5
def __init__(self, imagesList, parameters):
self.parameters = parameters
self.imagesList = imagesList
self.setDisplay()
self.setKeyBindings()
self.configuration()
if len(self.imagesList) == 0:
print('[-] No images could be found... Exiting')
sys.exit(-1)
if self.parameters.verbosity == 1:
print('[+] Loaded %i images' % (len(self.imagesList)))
self.imagesCache = ImagesCache(self.imageManager, self.imagesList, self.parameters)
self.displayImage(self.imagesCache.getCurrentImage())
self.updateTimer()
self.root.mainloop()
def setDisplay(self):
self.root = tkinter.Tk()
self.screen['width'] = self.root.winfo_screenwidth()
self.screen['height'] = self.root.winfo_screenheight()
self.imageManager = ImageManager(self.screen)
self.root.overrideredirect(True)
if sys.platform.startswith('darwin'):
self.root.overrideredirect(False)
self.root.geometry('%dx%d+%d+%d' % (self.screen['width'], self.screen['height'], 0, 0))
self.root.focus_set()
self.label = tkinter.Label(self.root, image = None, width = self.screen['width'], height = self.screen['height'])
self.label.pack()
self.label.configure(background = 'black', borderwidth = 0)
def setKeyBindings(self):
self.root.bind("<Escape>", self.exit)
self.root.bind("<q>", self.exit)
self.root.bind("<Return>", self.showNextImage)
self.root.bind("<space>", self.togglePause)
self.root.bind("<Right>", self.showNextImage)
self.root.bind("<Left>", self.showPrevImage)
self.root.bind("<Up>", self.showPrevImage)
self.root.bind("<Down>", self.showNextImage)
def configuration(self):
if self.parameters.random:
random.shuffle(self.imagesList)
if self.parameters.filter:
r = re.compile('.*%s.*' % (self.parameters.filter))
self.imagesList = list(filter(r.match, self.imagesList))
if self.parameters.time:
self.slide_time = self.parameters.time
def updateTimer(self):
if not self.pause:
if time.time() - self.lastTimeView >= self.slide_time:
self.showNextImage()
self.root.after(int(self.slide_time * 1000), self.updateTimer)
else:
nextTime = int((self.slide_time - (time.time() - self.lastTimeView)) * 1000) + 1
if nextTime < 0:
nextTime = 0
self.root.after(nextTime, self.updateTimer)
def exit(self, e = None):
self.root.destroy()
def togglePause(self, e = None):
self.pause = not self.pause
if not self.pause:
self.root.after(self.slide_time * 1000, self.updateTimer)
def showNextImage(self, e = None):
if not self.parameters.loop and self.currentPosition + 1 > len(self.imagesList) - 1:
self.lastTimeView = time.time()
return
image = self.imagesCache.getNextImage()
self.currentPosition += 1
if image:
self.displayImage(image)
def showPrevImage(self, e = None):
if not self.parameters.loop and self.currentPosition - 1 < 0:
self.lastTimeView = time.time()
return
image = self.imagesCache.getPreviousImage()
self.currentPosition -= 1
if image:
self.displayImage(image)
def displayImage(self, image):
if self.parameters.verbosity == 1:
print('Current Image: %s' % (image.name))
self.label.configure(image = image.imageTk)
self.label.image = image.imageTk
self.lastTimeView = time.time()
class ImagesCache():
cache = 3
def __init__(self, imageManager, imagesList, parameters):
self.imageManager = imageManager
self.imagesList = imagesList
if parameters.cache != None:
print(parameters.cache)
if parameters.cache < 1:
print('[-] %s is not a valid cache size, using 1 instead' % (parameters.cache))
self.cache = 1
else:
self.cache = parameters.cache
self.verbosity = parameters.verbosity
self.imagesListSize = len(self.imagesList)
self.current_node = None
self.start_node = None
self.end_node = None
self.loadFirst()
self.e = threading.Event()
self.updater = threading.Thread(target=self.updateImages, args=(self.e,), daemon=True)
self.updater.start()
def loadFirst(self):
self.insert_start(self.imageManager.loadImage(self.imagesList[0]))
def updateImages(self, e):
while True:
if self.verbosity == 2:
print('Start: %s, Current %s, End: %s' % (self.start_node.position, self.current_node.position, self.end_node.position))
next_preloaded = self.end_node.position - self.current_node.position
if next_preloaded < self.cache:
for _ in range(self.cache - next_preloaded):
if self.verbosity == 2:
print('\tloading end image %s: (%s -> %s)' % (self.end_node.position + 1, (self.end_node.position + 1) % self.imagesListSize, self.imagesList[(self.end_node.position + 1) % self.imagesListSize]))
self.insert_end(self.imageManager.loadImage(self.imagesList[(self.end_node.position + 1) % self.imagesListSize]))
if next_preloaded > self.cache:
for _ in range(next_preloaded - self.cache):
if self.verbosity == 2:
print('\tdelete end %s' % (self.end_node.position))
self.delete_end()
previous_preloaded = self.current_node.position - self.start_node.position
if previous_preloaded < self.cache:
for _ in range(self.cache - previous_preloaded):
if self.verbosity == 2:
print('\tloading start image %s: (%s -> %s)' % (self.start_node.position -1, (self.start_node.position - 1) % self.imagesListSize, self.imagesList[(self.start_node.position - 1) % self.imagesListSize]))
self.insert_start(self.imageManager.loadImage(self.imagesList[(self.start_node.position - 1) % self.imagesListSize]))
if previous_preloaded > self.cache:
for _ in range(previous_preloaded - self.cache):
if self.verbosity == 2:
print('\tdelete start %s' % (self.start_node.position))
self.delete_start()
e.wait()
e.clear()
def getCurrentImage(self):
return self.current_node.image
def getNextImage(self):
self.e.set()
if self.current_node.next:
self.current_node = self.current_node.next
image = self.current_node.image
return image
else:
return None
def getPreviousImage(self):
self.e.set()
if self.current_node.prev:
self.current_node = self.current_node.prev
image = self.current_node.image
return image
else:
return None
def insert_start(self, image):
if self.start_node is None:
self.start_node = ImageNode(image)
self.start_node.position = 0
self.end_node = self.start_node
self.current_node = self.start_node
else:
new_node = ImageNode(image)
new_node.position = self.start_node.position - 1
new_node.next = self.start_node
self.start_node.prev = new_node
self.start_node = new_node
def insert_end(self, image):
if self.start_node is None:
self.start_node = ImageNode(image)
self.start_node.position = 0
self.end_node = self.start_node
self.current_node = self.start_node
else:
new_node = ImageNode(image)
new_node.position = self.end_node.position + 1
new_node.prev = self.end_node
self.end_node.next = new_node
self.end_node = new_node
def delete_start(self):
if self.start_node is None:
return
if self.start_node.next is None:
self.start_node = None
return
self.start_node = self.start_node.next
self.start_node.prev = None
def delete_end(self):
if self.end_node is None:
return
if self.end_node.prev is None:
self.end_node = None
self.end_node = self.end_node.prev
self.end_node.next = None
class ImageNode():
def __init__(self, image):
self.image = image
self.position = 0
self.next = None
self.prev = None
class ImageManager():
def __init__(self, screen):
self.width = screen['width']
self.height = screen['height']
def loadImage(self, filename):
image = Image.open(filename)
img_width, img_height = image.size
if img_width != self.width or img_height != self.height:
image = self.resizeImage(image)
imageTk = ImageTk.PhotoImage(image)
name = filename
return MyImage(imageTk, name)
def resizeImage(self, image):
img_width, img_height = image.size
ratio = min(self.width / img_width, self.height / img_height)
img_width = int(img_width * ratio)
img_height = int(img_height * ratio)
image = image.resize((img_width, img_height), Image.ANTIALIAS)
return image
class MyImage():
def __init__(self, imageTk, name):
self.imageTk = imageTk
self.name = name
def parse_arguments():
description = '''
Simple slideshow written in pyhton with a buffer that allow to preload images for a smoother transition.
This slideshow will allow form randomize the order of the images, change the time between images (in seconds) and some other features.
All the images will be resized to full screen.
Keyboard Controls:
<Escape> <q> Exit
<space> Pause/Unpause
<Return> <Right> <Down> Next image
<Left> <Up> Previous image
'''
epilog = '''
EXAMPLES:
python3 slideshow.py -r
This command will show all the images in the current folder with a random order
python3 slideshow.py -t 3 -l
This command will show all the images in the current folder with 3 seconds between them (-t 3) and once it reaches the last image it will start from the beginning (-l)
python3 slideshow.py -p /home/user/pictures
This command will show all the images in the folder "pictures" situated at "/home/user/pictures/"
python3 slideshow.py -r -f moon
This command will show all the images in the current folder that has "moon" in their name in a random order
'''
parser = argparse.ArgumentParser(
description=description,
epilog=epilog, formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument('-r', '--random', action='store_true', help='The images will be displayed in random order')
parser.add_argument('-t', '--time', type=int, help='It defines the time it will take to slide an image in seconds. The default time is 5 seconds')
parser.add_argument('-p', '--path', help='the path to the folder to show in the slideshow. If no path is presented, the current folder will be displayed')
parser.add_argument('-l', '--loop', action='store_true', help='Once reached the last image, start again from the beginning')
parser.add_argument('-f', '--filter', help='Show only images that contain certain word in their filename')
parser.add_argument('--cache', type=int, help='It allow you to modify how many images are loaded in advance, this is specially useful when working with big images that take some time to load and resize. The default value is 3')
parser.add_argument('-v', '--verbosity', action='count', help='(-v) Show the name of the image currently being dilsplayed on the console. (-vv) Show what images are being loaded and deleted')
# parser.add_argument('-R', '--recursive', action='store_true', help='Display images in subdirectories too')
# parser.add_argument('--depth', type=int, help='Max depth of subdirectories to look for when recursivity is on. Default depth is 3')
args = parser.parse_args()
return args
def main(args):
fileManager = FileManager(args)
fileManager.getFiles()
SlideShow(fileManager.files, args)
if __name__ == "__main__":
args = parse_arguments()
main(args)