-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshow-image.py
executable file
·455 lines (379 loc) · 12.6 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
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#!/usr/bin/env python
import signal
import time
from sys import exit
import sys, getopt
from PIL import Image
import numpy as np
try:
from PIL import Image
except ImportError:
exit("This script requires the pillow module\nInstall with: sudo pip install pillow")
try:
import unicornhathd
except ImportError:
print("no unicorn hat hd detected - using simulator")
try:
from unicorn_hat_sim import unicornhathd
except ImportError:
print("no unicorn hat hd simulator found - exiting now")
print("You need to install at least a Simulator (unicorn_hat_sim) or the Libraries (unicornhathd)")
# default values for animations
pulseamount = 3
blinkamount = 6
spinamount = 3
zoomloop = 1
animationamount = 1
animation_sleep = 0.05
rotation = 0
brightness = 1
# initialization of booleans
myfile = False
blink = False
spin = False
pulse = False
fun = False
zoomin = False
zoomout = False
zoom = False
loop = False
colorize = False
demo = False
filechoosen = False
true_array = ['true', '1', 't', 'y', 'yes', 'yeah', 'yup', 'certainly', 'uh-huh']
help_text = """This is a Python Image Suite for Pimoroni Unicorn HD
This is based on the original show-png.py example from Pimoroni
It can be used to display images on the Unicorn HD. If the Image is to big it will be automatically resized to 16x16 pixel, but it won't take care of aspect ratio.
So for best results make sure you have already adjusted your Image to 16x16 pixels. You can choose between multiple effects, if you don't choose a effect you will get a static picture!
GIF animations will be played automatically, but will stop after one turn.
Usage:
./show-png.py --file /path/file.png
--blink True Let the image blink
--spin False Spin the Image
--pulse False Let the image pulse
--zoomin False Zoom into the Image
--zoomout False Zoom out of the Image
--zoom False Zoom In and Out of the Image
--fun False Move the outter border od the Image
--loop Loops the animation (Blink/Pulse/Spin/Zoom)
If you provide gif animation it will be played in a loop
--colorize 255,0,0 Colorizes the image before showing
--animationsleep 0.1 Set the sleep time between animation frames
--demo False Shows all animations with attached palm.png"""
demo_text = "To get a demo use the '--demo True' parameter"
try:
try:
argv = sys.argv[1:]
opts, args = getopt.getopt(argv,"hi:o:",["file=","blink=","spin=","pulse=","zoomin=","fun=","zoomout=","zoom=","demo=","loop=","colorize=","animationsleep=","rotation="])
except getopt.GetoptError, e2:
print help_text
print demo_text
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print help_text
sys.exit()
if opt in ("-f", "--file"):
filechoosen = True
myfile = str(arg)
if opt in ("-b", "--blink"):
blink = str(arg).lower() in true_array
if opt in ("-l", "--loop"):
loop = str(arg).lower() in true_array
if opt in ("-s", "--spin"):
spin = str(arg).lower() in true_array
if opt in ("-p", "--pulse"):
pulse = str(arg).lower() in true_array
if opt in ("-ff", "--fun"):
fun = str(arg).lower() in true_array
if opt in ("-zi", "--zoomin"):
zoomin = str(arg).lower() in true_array
if opt in ("-zo", "--zoomout"):
zoomout = str(arg).lower() in true_array
if opt in ("-z", "--zoom"):
zoom = str(arg).lower() in true_array
if opt in ("-c", "--colorize"):
colorize = str(arg).lower()
if opt in ("-a", "--animationsleep"):
animation_sleep = float(arg)
if opt in ("-r", "--rotation"):
rotation = int(arg)
if opt in ("-d", "--demo"):
demo = str(arg).lower() in true_array
except Exception, e:
print e
unicornhathd.rotation(rotation)
unicornhathd.brightness(brightness)
if demo == True:
filechoosen = True
myfile = "./palm.png"
blink = True
spin = True
pulse = True
fun = True
zoomin = True
zoomout = True
zoom = True
if not filechoosen:
print "Error: You need to select a file via --file"
print ""
print help_text
print demo_text
sys.exit(2)
width, height = unicornhathd.get_shape()
img = Image.open(myfile)
mypalette = img.getpalette()
pwidth, pheight = img.size
# automatically resize image to 16x16 pixel as more makes no sense and does not work
if pwidth > 16:
img = img.resize((16,16))
# try to convert image
try:
img = img.convert('RGBA')
except Exception,e:
print e
pass
def showit(theimg = img, newr = 0, newg = 0, newb = 0):
try:
for x in range(width):
for y in range(height):
pixel = theimg.getpixel((y,x))
r,g,b,a = pixel[0],pixel[1],pixel[2],pixel[3]
try:
if int(a) == 0:
r,g,b = 0,0,0
except Exception, e:
pass
if (newr != 0) or (newg != 0) or (newb != 0):
try:
if int(a) != 0:
r,g,b = newr,newg,newb
except Exception, e:
pass
try:
if r or g or b:
valid = True
unicornhathd.set_pixel(x, y, r, g, b)
except Exception, e:
pass
print ""
unicornhathd.show()
except Exception,e:
pass
newr = 0
newg = 0
newb = 0
if colorize != False:
newr = int(colorize.split(",")[0].strip())
newg = int(colorize.split(",")[1].strip())
newb = int(colorize.split(",")[2].strip())
def deleteit():
if blink == True:
time.sleep(0.2)
for x in range(0,width):
for y in range(0,height):
r, g, b = 0, 0, 0
unicornhathd.set_pixel(x, y, r, g, b)
unicornhathd.set_pixel(x, y, r, g, b)
unicornhathd.show()
time.sleep(0.2)
def blank():
for x in range(0,width):
for y in range(0,height):
r, g, b = 0, 0, 0
unicornhathd.set_pixel(x, y, r, g, b)
unicornhathd.set_pixel(x, y, r, g, b)
def analyseImage(path):
im = Image.open(path)
results = {
'size': im.size,
'mode': 'full',
}
try:
while True:
im.seek(im.tell() + 1)
except EOFError:
pass
return results
showed = False
try:
if zoomin == True:
unicornhathd.brightness(brightness)
blank()
showed = True
original_image = img
for i in range(1,16):
blank()
layer = original_image.resize((i,i))
img_w, img_h = layer.size
background = Image.new('RGBA', (16, 16), (0, 0, 0, 0))
bg_w, bg_h = background.size
offset = ((bg_w - img_w) // 2, (bg_h - img_h) // 2)
background.paste(layer, offset)
img = background
showit(img, newr, newg, newb)
img = original_image
blank()
showit(img, newr, newg, newb)
if blink == True:
unicornhathd.brightness(brightness)
blank()
showed = True
myi = 0
while ((myi <= blinkamount) or loop):
showit(img, newr, newg, newb)
time.sleep(0.2)
deleteit()
myi += 1
showit(img, newr, newg, newb)
time.sleep(0.3)
if pulse == True:
blank()
showed = True
myi = 0
while ((myi <= pulseamount) or loop):
time.sleep(0.05)
for i in range (10,2,-1):
value = "0."+str(i)
if value == "0.10":
value = "1"
value = float (value)
unicornhathd.brightness(value)
showit(img, newr, newg, newb)
time.sleep(0.04)
time.sleep(0.05)
for i in range (2,10,1):
value = "0."+str(i)
if value == "0.10":
value = "1"
value = float (value)
unicornhathd.brightness(value)
showit(img, newr, newg, newb)
time.sleep(0.04)
myi += 1
if spin == True:
unicornhathd.brightness(brightness)
blank()
amount = 0
showed = True
while ((amount <= spinamount) or loop):
for i in range(0,360,12):
blank()
newimg = Image.open(myfile)
img = newimg.rotate(i, Image.NEAREST)
try:
img = img.convert('RGBA')
except:
pass
showit(img, newr, newg, newb)
amount += 1
blank()
newimg = Image.open(myfile)
img = newimg
try:
img = img.convert('RGBA')
except:
pass
showit(img, newr, newg, newb)
if fun == True:
unicornhathd.brightness(brightness)
blank()
showed = True
original_image = img
for i in range(0,480,5):
# Calls the function to rotate the image by given angle
img = img.rotate(5)
blank()
showit(img, newr, newg, newb)
time.sleep(0.02)
blank()
img = original_image
showit(img, newr, newg, newb)
if zoomout == True:
unicornhathd.brightness(brightness)
blank()
showed = True
original_image = img
for i in range(16,0,-1):
blank()
layer = original_image.resize((i,i))
img_w, img_h = layer.size
background = Image.new('RGBA', (16, 16), (0, 0, 0, 0))
bg_w, bg_h = background.size
offset = ((bg_w - img_w) // 2, (bg_h - img_h) // 2)
background.paste(layer, offset)
img = background
showit(img, newr, newg, newb)
blank()
background = Image.new('RGBA', (16, 16), (0, 0, 0, 0))
img = background
showit(img, newr, newg, newb)
img = original_image
if zoom == True:
myloop = 0
while ((myloop <= spinamount) or loop):
unicornhathd.brightness(brightness)
blank()
showed = True
original_image = img
for i in range(1,16):
blank()
layer = original_image.resize((i,i))
img_w, img_h = layer.size
background = Image.new('RGBA', (16, 16), (0, 0, 0, 0))
bg_w, bg_h = background.size
offset = ((bg_w - img_w) // 2, (bg_h - img_h) // 2)
background.paste(layer, offset)
img = background
showit(img, newr, newg, newb)
img = original_image
blank()
showit(img, newr, newg, newb)
time.sleep(0.2)
unicornhathd.brightness(brightness)
showed = True
for i in range(16,0,-1):
blank()
layer = original_image.resize((i,i))
img_w, img_h = layer.size
background = Image.new('RGBA', (16, 16), (0, 0, 0, 0))
bg_w, bg_h = background.size
offset = ((bg_w - img_w) // 2, (bg_h - img_h) // 2)
background.paste(layer, offset)
img = background
showit(img, newr, newg, newb)
myloop += 1
blank()
background = Image.new('RGBA', (16, 16), (0, 0, 0, 0))
img = background
showit(img, newr, newg, newb)
img = original_image
if showed == False:
blank()
showed = True
animated = True
mode = analyseImage(myfile)['mode']
im = Image.open(myfile)
i = 0
p = im.getpalette()
last_frame = im.convert('RGBA')
myloop = 1
while ((myloop <= animationamount) or loop):
try:
im.seek(0)
myloop += 1
while True:
new_frame = Image.new('RGBA', (16, 16), (0, 0, 0, 0))
new_frame.paste(im, (0,0), im.convert('RGBA'))
blank()
showit(new_frame, newr, newg, newb)
i += 1
last_frame = new_frame
time.sleep(animation_sleep)
im.seek(im.tell() + 1)
except Exception, e3:
print e3
pass
showit(new_frame, newr, newg, newb)
except KeyboardInterrupt:
unicornhathd.off()