-
Notifications
You must be signed in to change notification settings - Fork 0
/
Text.py
69 lines (45 loc) · 1.81 KB
/
Text.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
import colorsys
import time
from sys import exit
try:
from PIL import Image, ImageDraw, ImageFont
except ImportError:
exit('This script requires the pillow module\nInstall with: sudo pip install pillow')
try:
import unicornhathd as unicorn
print("unicorn hat hd detected")
except ImportError:
from unicorn_hat_sim import unicornhathd as unicorn
class Text():
def __init__(self, lines) -> None:
self.lines = lines
def display(self):
colours = [tuple([int(n * 255) for n in colorsys.hsv_to_rgb(x / float(len(self.lines)), 1.0, 1.0)]) for x in range(len(self.lines))]
ANIMATION_TIME = 0.005
unicorn.rotation(0)
unicorn.brightness(0.6)
width, height = unicorn.get_shape()
text_x = width
text_y = 2
#font = ImageFont.truetype(font_file, font_size)
font = ImageFont.load_default()
text_width, text_height = width, 0
for line in self.lines:
w, h = font.getsize(line)
text_width += w + width
text_height = max(text_height, h)
text_width += width + text_x + 1
image = Image.new('RGB', (text_width, max(16, text_height)), (0, 0, 0))
draw = ImageDraw.Draw(image)
offset_left = 0
for index, line in enumerate(self.lines):
draw.text((text_x + offset_left, text_y), line, colours[index], font=font)
offset_left += font.getsize(line)[0] + width
for scroll in range(text_width - width):
for x in range(width):
for y in range(height):
pixel = image.getpixel((x + scroll, y))
r, g, b = [int(n) for n in pixel]
unicorn.set_pixel(width - 1 - x, y, r, g, b)
unicorn.show()
time.sleep(ANIMATION_TIME)