-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshow_image.py
78 lines (53 loc) · 1.63 KB
/
show_image.py
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
#!/usr/bin/env python
"""
Displays an image that is passed as the first argument to this script.
If a folder is passed as an argument, all images in the folder are searched
recursively, and a random image is picked and displayed.
"""
import argparse
import os
import random
from PIL import Image
from inky.inky_uc8159 import Inky
""" Returns a list of all images in the path.
It's assumed that the path exists.
"""
def returnImagesInPath(path):
if os.path.isdir(path):
imagePaths = []
for path,dirs,files in os.walk(path):
for filename in files:
if filename.lower()[-4:] in [".jpg", ".png", "jpeg"]:
imagePaths.append(os.path.join(path, filename))
return imagePaths
elif path.lower()[-4:] in [".jpg", ".png", "jpeg"]:
return [path]
else:
return []
""" Receives a list of image file paths, chooses a random image,
makes some color adjustments and displays the image.
"""
def displayRandomImage(imageList):
limit = len(imageList)
index = random.randrange(0, len(imageList))
imagePath = imageList[index]
print("Displaying {}".format(imagePath))
inky = Inky()
saturation = 0.8
image = Image.open(imagePath)
inky.set_image(image, saturation=saturation)
inky.show()
print("Done.")
""" Returns an error if the string is not a valid path
"""
def dir_path(string):
if os.path.exists(string):
return string
else:
raise NotADirectoryError(string)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-p ", "--path", type=dir_path, required=True)
arguments = parser.parse_args()
images = returnImagesInPath(arguments.path)
displayRandomImage(images)