Running the python code posted below, at seems like random intervals I get the following error. Which I am lost with trying to figure out what is causing it?
Traceback (most recent call last):
File "/home/pi/mini_clock.py", line 64, in <module>
if image.getpixel((x + offset_x, y)) == 255:
File "/usr/lib/python3/dist-packages/PIL/Image.py", line 1333, in getpixel
return self.im.getpixel(xy)
IndexError: image index out of range
The code below is a modified version of the text.py file found in the examples for the Unicorn hat Mini.
#!/usr/bin/env python3
import time, datetime
import sys
from PIL import Image, ImageDraw, ImageFont
from unicornhatmini import UnicornHATMini
unicornhatmini = UnicornHATMini()
rotation = 180
if len(sys.argv) > 1:
try:
rotation = int(sys.argv[1])
except ValueError:
print("Usage: {} <rotation>".format(sys.argv[0]))
sys.exit(1)
unicornhatmini.set_rotation(rotation)
display_width, display_height = unicornhatmini.get_shape()
print("{}x{}".format(display_width, display_height))
unicornhatmini.set_brightness(0.5)
font = ImageFont.truetype("5x7.ttf", 8)
offset_x = 0
while True:
text = time.strftime("%A %B %-d %-I:%M%p")
text_width, text_height = font.getsize(text)
image = Image.new('P', (text_width + display_width + display_width, display_height), 0)
draw = ImageDraw.Draw(image)
draw.text((display_width, -1), text, font=font, fill=255)
for y in range(display_height):
for x in range(display_width):
if image.getpixel((x + offset_x, y)) == 255:
unicornhatmini.set_pixel(x, y, 0, 255, 0)
else:
unicornhatmini.set_pixel(x, y, 0, 0, 0)
offset_x += 1
if offset_x + display_width > image.size[0]:
offset_x = 0
unicornhatmini.show()
time.sleep(0.05)
1 post - 1 participant