Long story short: my Ubercorn is showing text mirrored. I can’t find what’s the problem, PIL has a mirror function but I don’t see it anywhere in my script. Anyone has an idea what could cause this?
#!/usr/bin/env python
import time
import sys
from colorsys import hsv_to_rgb
from PIL import Image, ImageDraw, ImageFont
import unicornhathd
# The text we want to display. You should probably keep this line and replace it below
# That way you'll have a guide as to what characters are supported!
# text = "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789 #@&!?{}<>[]();:.,'%*=+-=$_\\/ :-)"
text = "hang deze in de winkel met andere tekst"
rotation = 0
# if len(sys.argv) > 1:
# try:
# rotation = int(sys.argv[1])
# except ValueError:
# print("Usage: {} <rotation>".format(sys.argv[0]))
# sys.exit(1)
unicornhathd.rotation(rotation)
display_width, display_height = unicornhathd.get_shape()
print("{}x{}".format(display_width, display_height))
# Do not look at unicornhatmini with remaining eye
unicornhathd.brightness(1.0)
font = ImageFont.truetype('/usr/share/fonts/truetype/freefont/FreeSans.ttf', 14)
# Measure the size of our text, we only really care about the width for the moment
# but we could do line-by-line scroll if we used the height
text_width, text_height = font.getsize(text)
# Create a new PIL image big enough to fit the text
image = Image.new('P', (text_width + display_width + display_width, display_height), 0)
draw = ImageDraw.Draw(image)
# Draw the text into the image
draw.text((display_width, -1), text, font=font, fill=255)
# war 0
offset_x = 0
while True:
for y in range(display_height):
for x in range(display_width):
hue = (time.time() / 10.0) + (x / float(display_width * 2))
r, g, b = [int(c * 255) for c in hsv_to_rgb(hue, 1.0, 1.0)]
if image.getpixel((x + offset_x, y)) == 255:
unicornhathd.set_pixel(x, y, r, g, b)
else:
unicornhathd.set_pixel(x, y, 0, 0, 0)
offset_x += 1
if offset_x + display_width > image.size[0]:
offset_x = 0
unicornhathd.show()
time.sleep(0.05)
I’m pretty sure it’s something in the
for y in range(display_height): etc
block of code.
I had that happen to me on my Unicorn Mini, and Unicorn HD when I was messing with that block of code.
Give this a try
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]
unicornhathd.set_pixel(width - 1 - x, y, r, g, b)
It’s what worked for me on my Unicorn HD to scroll text. You’ll likely have to edit the inicornhathd.set.pixel for your ubercron
I tried a bit around, but the thing that does the trick is indeed to change the unicornhathd.set_pixel
, I put now unicornhathd.set_pixel(display_width - 1 -x, y, r, g, b)
but now the text is morphing in a weird way, but at least not mirrored.
I had to really mess around to get a font I liked and get it to use the full height.
The mirrored text happened when I was trying get control over what color the text was.
This a piece of my file I run to show date time and weather sensor data. It’s just the date time bit.
FONT = ('/usr/share/fonts/truetype/freefont/FreeSans.ttf', 16)
then in the while true part
text = time.strftime("It's %A %B %-d %-I:%M:%p")
width, height = unicornhathd.get_shape()
text_x = width
text_y = -2
font_file, font_size = FONT
font = ImageFont.truetype(font_file, font_size)
text_width, text_height = width, 0
text = time.strftime("It's %A %B %-d %-I:%M:%p")
w, h = font.getsize(text)
text_width += w + width
text_height = max(text_height, h)
image = Image.new('RGB', (text_width, max(16, text_height)), (0, 0, 0))
draw = ImageDraw.Draw(image)
r, g, b =(0, 255, 255)
offset_left = 0
draw.text((text_x + offset_left, text_y), text, font=font, fill=(r, g, b))
offset_left += font.getsize(text)[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]
unicornhathd.set_pixel(width - 1 - x, y, r, g, b)
unicornhathd.show()
time.sleep(0.01)
Your snippet didn’t work for me but after playing around with all kind of values I found out that:
if image.getpixel((x + offset_x, y)) == 255:
unicornhathd.set_pixel(x, y, r, g, b)
else:
unicornhathd.set_pixel(x, y, 0, 0, 0)
Has to be changed to:
if image.getpixel((x + offset_x, y)) == 255:
unicornhathd.set_pixel(display_width - 1 - x, y, r, g, b)
else:
unicornhathd.set_pixel(display_width - 1 - x, y, 0, 0, 0)
So I also had to change the part of the else, which I didn’t do before. So problem is solved, but to be honest I don’t know why exactly.
Yeah, it gets fun when your trying to run code for one device on a very similar, but slightly different device. Unicorn pHat, Unicorn Hat, Unicorn Hat Mini, Unicorn Hat HD, and for you Ubercorn.
For me, I wanted scrolling text, but all the scroll pHats etc are just single color. I also want multi color, so that meant getting a Unicorn. That made, for me anyway, it harder to do what I wanted.
Plus I’m using code, whats posted above for example, that I have no idea how it does what it does? I know what the end result is, but I have no idea how it gets there, lol.
Anyway, now that you have something that works you can tinker with it until you break it again, or get what you want. Just make sure to keep a backup of the last working file. ;)
Yes I always make backups, even by only bookmarking these threads to my browser. Now it’s time for another project :)
I’ll be tinkering with my Breakout Garden stuff today I think. More messing with code I don’t understand, lol. =)