I couldn't scroll Japanese with the official API of SenseHAT, so I tried to use Python's PIL and Misaki font. This is an image in which the text is made into a single image and cut out in order from the edge to an 8 * 8 image.
python
# -*- encoding:utf8 -*-
import unicodedata
import time
import sys
from PIL import Image, ImageDraw, ImageFont
from sense_hat import SenseHat
#Get the total number of bytes in a string
def count_byte(s):
n = 0
for c in s:
wide_chars = u"WFA"
eaw = unicodedata.east_asian_width(c)
if wide_chars.find(eaw) > -1:
n += 2
else:
n += 1
return n
#Scroll display on Sense HAT
def show_scroll_string(
disp_str, fore_color = "#ffffff", back_color = "#000000", interval = 0.1):
'''
disp_str =Character string to be displayed
fore_color =Text color (optional)
back_color =Background color (optional)
interval =Scroll display interval (optional)
'''
matrix_size = 8
img_width = (count_byte(disp_str) * 4) + (matrix_size * 2)
sense = SenseHat()
font = ImageFont.truetype("/misaki_gothic.ttf", #Specify the font path
8, encoding="unic")
img_moto = Image.new("RGB", (img_width, matrix_size), back_color)
draw = ImageDraw.Draw(img_moto)
draw.text((matrix_size, 1), disp_str, font = font, fill = fore_color)
img_moto.save("./img.png ", "PNG")
for num in range(0, img_width - matrix_size):
img = img_moto.crop((num, 0, matrix_size + num, matrix_size))
img.save("./img_.png ", "PNG")
sense.load_image("./img_.png ")
time.sleep(interval)
sense.clear()
# main
show_scroll_string(u"String String String.", fore_color="#ffff00", interval = 0.07)
If you specify the font path and put the output character string in the argument of show_scroll_string and call it, it should be scrolled.
If you want to execute and use it as it is, please do so at your own risk.
Referenced sites, etc.
Recommended Posts