[PYTHON] Scroll Japanese on the LED of RaspberryPi Sense HAT

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. 写真 2016-11-24 20 49 54.gif

If you want to execute and use it as it is, please do so at your own risk.
Referenced sites, etc.

Recommended Posts

Scroll Japanese on the LED of RaspberryPi Sense HAT
Japanese translation of the e2fsprogs manual
Easy installation of OpenCV on RaspberryPi 3+
Japanese translation of the man-db manual
Japanese translation of the util-linux manual
Japanese translation of the iproute2 manual
Investigate the effect of outliers on correlation
Japanese translation of the LDP man-pages manual
Post the subject of Gmail on twitter
Display the graph of tensorBoard on jupyter
Change the order of PostgreSQL on Heroku
[Python] Japanese localization of matplotlib on Ubuntu
The backslash of the Japanese keyboard is "ro"
Japanese translation: PEP 20 --The Zen of Python
Retry to turn on/off the power of the USB port on RaspberryPi 4 Model B
The behavior of signal () depends on the compile options
[2020July] Check the UDID of the iPad on Linux
At the time of python update on ubuntu
Change the resolution of Ubuntu running on VirtualBox
[Python] Get the day of the week (English & Japanese)
[AWS S3] Confirmation of the existence of folders on S3