-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplay_class.py
More file actions
139 lines (124 loc) · 6.66 KB
/
Copy pathdisplay_class.py
File metadata and controls
139 lines (124 loc) · 6.66 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
#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-
import sys
import glob
from rgbmatrix import RGBMatrix
from PIL import ImageFont, ImageDraw, Image
class display_class:
global AnimationCanvas
def __init__(self,rows,chain,parallel,pwmBits,brightness,luminanceCorrect):
self.matrix = RGBMatrix(rows,chain,parallel)
self.matrix.pwmBits = pwmBits
self.matrix.brightness = brightness
self.matrix.luminanceCorrect = luminanceCorrect
# The canvas to work on without chanching the Screen
self.offsetCanvas = self.matrix.CreateFrameCanvas()
# Kill the screen at programm start
self.offsetCanvas.Fill(0,0,0)
self.offsetCanvas = self.matrix.SwapOnVSync(self.offsetCanvas)
def setBrightness(self,brightness):
self.matrix.brightness = brightness
# The canvas to work on without chanching the Screen
self.offsetCanvas = self.matrix.CreateFrameCanvas()
self.offsetCanvas.Fill(0,0,0)
self.offsetCanvas = self.matrix.SwapOnVSync(self.offsetCanvas)
#print 'Brightsness is set to: ' + str(brightness)
#print 'With type: ' + str(type(brightness))
def setpwmBits(self,pwmBits):
self.matrix.pwmBits = pwmBits
def setluminaceCorrect(self,luminanceCorrect):
self.matrix.luminanceCorrect = luminanceCorrect
def updateScreen(self):
# This function is to show the canvas and update the screen
self.offsetCanvas = self.matrix.SwapOnVSync(self.offsetCanvas)
def clearoffsetScreen(self):
# This is the normal way to clear the canvas
self.offsetCanvas.Fill(0,0,0)
# Display image on the given coordinates
def showimage(self,x,y,imagepath):
# Imports an Image from the given path, accepted are .jpg .gif .png
self.im = Image.open(imagepath)
self.xy = [0,0]
self.xymax = self.im.size
self.alpha = 255
if (not self.im.mode == 'RGB') or (not self.im.mode == 'RGBA'):
self.im = self.im.convert('RGBA')
if self.im.mode == 'RGB':
for self.xy[0] in range(0,self.xymax[0]):
for self.xy[1] in range(0,self.xymax[1]):
self.tmp = (self.xy[0],self.xy[1])
self.red, self.green, self.blue = self.im.getpixel(self.tmp)
self.offsetCanvas.SetPixel(x + self.xy[0],y + self.xy[1],self.red,self.green,self.blue)
elif self.im.mode == 'RGBA':
for self.xy[0] in range(0,self.xymax[0]):
for self.xy[1] in range(0,self.xymax[1]):
self.tmp = (self.xy[0],self.xy[1])
self.red, self.green, self.blue, self.alpha = self.im.getpixel(self.tmp)
if self.alpha > 50:
self.offsetCanvas.SetPixel(x + self.xy[0],y + self.xy[1],self.red,self.green,self.blue)
# Textrenderer to display Text
def drawtext(self,x,y,text,fonttype,red,green,blue,clear):
self.fonthight = fonttype[fonttype.find('x')+1:len(fonttype)]
if not self.fonthight.isdigit():
self.fonthight = self.fonthight[0:len(self.fonthight)-1]
self.fontwidth = fonttype[0:fonttype.find('x')]
self.im = Image.new("I", (len(text)*int(self.fontwidth),int(self.fonthight)-1))
self.draw = ImageDraw.Draw(self.im)
# all .bdf files are converted to .pil for easy access
self.font = ImageFont.load("/programs/rpi-rgb-led-matrix-master/fonts/" + fonttype + ".pil")
if isinstance(text, str):
self.draw.text((0, -1), text.decode("utf8").encode("iso-8859-1"), font=self.font)
elif isinstance(text, unicode):
self.draw.text((0, -1), text.encode("iso-8859-1"), font=self.font)
self.xy = [0,0]
self.xymax = self.im.size
for self.xy[0] in range(0,self.xymax[0]):
for self.xy[1] in range(0,self.xymax[1]):
self.tmp = (self.xy[0],self.xy[1])
if self.im.getpixel(self.tmp) > 0:
self.offsetCanvas.SetPixel(x + self.xy[0],y + self.xy[1],red,green,blue)
elif clear == True:
self.offsetCanvas.SetPixel(x + self.xy[0],y + self.xy[1],0,0,0)
def load_animations(self,path):
# load and preprocess animations to speed up things
global AnimationCanvas
self.animation_length = [0]
self.frame_delay = [0]
self.anim_counter = 0
AnimationCanvas = []
path = path + '/*.gif'
# go through all . gif files in the given folder
for self.infile in glob.glob(path):
AnimationCanvas.append([])
if self.anim_counter > 0:
self.animation_length.append(1)
self.frame_delay.append(1)
self.animation_length[self.anim_counter] = self.anim_counter + 1
self.im = Image.open(self.infile)
self.frame_delay[self.anim_counter] = self.im.info['duration']
self.frame_counter = 0
try: #go through all frames in the gif file, at the end the loop will fail
# for each frame a canvas is created in an 2D array which can be accessed with the animation and frame number
while 1:
AnimationCanvas[self.anim_counter].append(0)
AnimationCanvas[self.anim_counter][self.frame_counter] = self.matrix.CreateFrameCanvas()
self.xy = [0,0]
self.xymax = self.im.size
self.image = self.im.convert('RGB')
for self.xy[0] in range(0,self.xymax[0]):
for self.xy[1] in range(0,self.xymax[1]):
self.tmp = (self.xy[0],self.xy[1])
self.red, self.green, self.blue = self.image.getpixel(self.tmp)
AnimationCanvas[self.anim_counter][self.frame_counter].SetPixel(self.xy[0],self.xy[1],
self.red,self.green,self.blue)
self.im.seek(self.im.tell()+1)
self.frame_counter = self.im.tell()
except:
pass
self.animation_length[self.anim_counter] = self.frame_counter
self.anim_counter += 1
# after all gif files put back the animation length and the frame numbers to the program
return self.animation_length, self.frame_delay
def show_animation_image(self,animation_number,frame_number):
global AnimationCanvas
self.matrix.SwapOnVSync(AnimationCanvas[animation_number][frame_number])