-
Notifications
You must be signed in to change notification settings - Fork 0
/
what-to-show.py
executable file
·153 lines (113 loc) · 4.47 KB
/
what-to-show.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
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
#!/usr/bin/env python3
import os
import sys
import argparse
import buttonshim
import time
import signal
from PIL import Image
from inky import InkyWHAT
# locally defines modules
# from showimage import showImage
# this has to be refactored to a (few) classes
def showImage(img_file: str):
# Open our image file that was passed in from the command line
print("displaying image, please wait")
img = Image.open(img_file)
# Get the width and height of the image
w, h = img.size
# Calculate the new height and width of the image
h_new = 300
w_new = int((float(w) / h) * h_new)
w_cropped = 400
# Resize the image with high-quality resampling
img = img.resize((w_new, h_new), resample=Image.LANCZOS)
# Calculate coordinates to crop image to 400 pixels wide
x0 = (w_new - w_cropped) / 2
x1 = x0 + w_cropped
y0 = 0
y1 = h_new
# Crop image
img = img.crop((x0, y0, x1, y1))
# Convert the image to use a white / black / red colour palette
pal_img = Image.new("P", (1, 1))
pal_img.putpalette((255, 255, 255, 0, 0, 0, 255, 0, 0) + (0, 0, 0) * 252)
img = img.convert("RGB").quantize(palette=pal_img)
# Display the final image on Inky wHAT
inky_display.set_image(img)
inky_display.show()
print("Image ready")
# initialize default buttonhandler for buttonshim
@buttonshim.on_press(buttonshim.BUTTON_A)
def button_a(button, pressed):
global button_flag
button_flag = "button_1"
@buttonshim.on_press(buttonshim.BUTTON_B)
def button_b(button, pressed):
global button_flag
button_flag = "button_2"
@buttonshim.on_press(buttonshim.BUTTON_C)
def button_c(button, pressed):
global button_flag
button_flag = "button_3"
@buttonshim.on_press(buttonshim.BUTTON_D)
def button_d(button, pressed):
global button_flag
button_flag = "button_4"
@buttonshim.on_press(buttonshim.BUTTON_E)
def button_e(button, pressed):
global button_flag
button_flag = "button_5"
try:
if __name__ == '__main__':
print("""Inky wHAT: Dither image
Converts and displays dithered images on Inky wHAT.
Button SHIM:
Light up the LED a different color of the rainbow with each button pressed.
Show an image depending on button pressed
press <ctrl>+<c> to quit
""")
# Command line arguments to set display type and colour, and enter your name
parser = argparse.ArgumentParser()
parser.add_argument('--colour', '-c', type=str, required=True, choices=["red", "black", "yellow"], help="ePaper display colour")
parser.add_argument('--image', '-i', type=str, required=True, help="Input image to be converted/displayed")
args = parser.parse_args()
colour = args.colour
img_file = args.image
button_flag = "null"
# Set up the inky wHAT display and border colour
inky_display = InkyWHAT(colour)
inky_display.set_border(inky_display.WHITE)
showImage(img_file)
print("press a button to show a different image...")
while True:
time.sleep(.1)
if button_flag == "button_1":
buttonshim.set_pixel(0x94, 0x00, 0xd3)
button_flag = "null"
showImage("resources/Escher_Dag-en-Nacht.jpg")
print("press a button to show a different image...")
elif button_flag == "button_2":
buttonshim.set_pixel(0x00, 0x00, 0xff)
button_flag = "null"
showImage("resources/Escher_het_oog.jpg")
print("press a button to show a different image...")
elif button_flag == "button_3":
buttonshim.set_pixel(0x00, 0xff, 0x00)
button_flag = "null"
showImage("resources/Escher_Trappen.jpeg")
print("press a button to show a different image...")
elif button_flag == "button_4":
buttonshim.set_pixel(0xff, 0xff, 0x00)
button_flag = "null"
showImage("resources/InkywHAT-400x300-bw.png")
print("press a button to show a different image...")
elif button_flag == "button_5":
buttonshim.set_pixel(0xff, 0x00, 0x00)
button_flag = "null"
showImage("resources/pattern-3.jpg")
print("press a button to show a different image...")
except KeyboardInterrupt:
# quit
print ("program terminated")
sys.exit()