/
image_view.py
133 lines (107 loc) · 3.38 KB
/
image_view.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
# Used in main loop
from time import sleep
import random
import time
###################################
# Graphics imports, constants and structures
###################################
from rgbmatrix import RGBMatrix, RGBMatrixOptions
from PIL import Image, ImageDraw
# this is the size of ONE of our matrixes.
matrix_rows = 32
matrix_columns = 32
# how many matrixes stacked horizontally and vertically
matrix_horizontal = 5
matrix_vertical = 3
total_rows = matrix_rows * matrix_vertical
total_columns = matrix_columns * matrix_horizontal
options = RGBMatrixOptions()
options.rows = matrix_rows
options.cols = matrix_columns
options.chain_length = matrix_horizontal
options.parallel = matrix_vertical
#options.hardware_mapping = 'adafruit-hat-pwm'
#options.hardware_mapping = 'adafruit-hat' # If you have an Adafruit HAT: 'adafruit-hat'
options.hardware_mapping = 'regular'
options.gpio_slowdown = 2
matrix = RGBMatrix(options = options)
imageSize = 80
image = Image.open("./logos/flyingpig.jpg").convert('RGB')
#image = image.rotate(180)
image = image.resize((imageSize,imageSize))
xMaxRange = total_columns - imageSize
yMaxRange = total_rows - imageSize
imageX= random.randint(0,xMaxRange)
imageY= random.randint(0,yMaxRange)
dirX = random.randint(1,3)
dirY = random.randint(1,3)
start = time.time()
elapsed_time = 0
last_reset = 0
backgroundColor = (255,255,255)
###################################
# Background
###################################
def background():
global backgroundColor
temp_image = Image.new("RGB", (total_columns,total_rows))
temp_draw = ImageDraw.Draw(temp_image)
temp_draw.rectangle((0,0,total_columns,total_rows), fill= backgroundColor)
matrix.SetImage(temp_image,0,0)
###################################
# hideImage
###################################
def hideImage():
global image
global imageX
global imageY
global backgroundColor
temp_image = Image.new("RGB", (imageSize + 2,imageSize + 2))
temp_draw = ImageDraw.Draw(temp_image)
temp_draw.rectangle((-1,-1,imageSize + 2 ,imageSize + 2), fill= backgroundColor)
matrix.SetImage(temp_image,imageX, imageY)
###################################
# newImage
###################################
def newImage():
global image
pickImage = random.randint(1,6)
if pickImage == 1:
image = Image.open("./logos/flyingpig.jpg").convert('RGB')
elif pickImage == 2:
image = Image.open("./logos/elephant.jpg").convert('RGB')
elif pickImage == 3:
image = Image.open("./logos/phish.jpg").convert('RGB')
elif pickImage == 4:
image = Image.open("./logos/unicorn2.jpg").convert('RGB')
elif pickImage == 5:
image = Image.open("./logos/kong.jpg").convert('RGB')
else:
image = Image.open("./logos/mustanglogo3.jpg").convert('RGB')
image = image.resize((imageSize,imageSize))
###################################
# Main loop
###################################
background()
#newImage()
while True:
if (elapsed_time > 5):
newImage()
elapsed_time=0
last_reset=time.time()
elapsed_time = time.time()-last_reset
matrix.SetImage(image, imageX+dirX, imageY+dirY)
sleep(.10)
if ((imageX > xMaxRange - 2) or (imageX < 2)):
dirX = -dirX
if ((imageY > yMaxRange - 2) or (imageY < 2)):
dirY = -dirY
imageX = imageX + dirX
imageY = imageY + dirY
#hideImage()
try:
print("Press CTRL-C to stop")
while True:
sleep(100)
except KeyboardInterrupt:
exit(0)