python's implementation of the dot matrix word "come on in Wuhan"

Dot matrix word

To achieve this effect, first of all, the concept of lattice character: lattice font is to divide each character into 16 × 16 or 24 × 24 points, and then use the virtual reality of each point to represent the outline of the character. Dot matrix fonts are also called bitmap fonts, in which each font is represented by a set of two-dimensional pixel information.

If we print with Python print, it's better to have a ready-made lattice font library, so that we can directly convert it into print characters according to its pixel information.

HZK16 font library

HZK is the acronym of the Chinese character library. The HZK16 character library is a 16 × 16 dot matrix font that conforms to the GB2312 standard. There are 6763 supported Chinese characters. Each Chinese character model needs a total of 256 points to display. Each point is a binary bit, that is, the 256 power data of 2, that is, 32 bytes.

Then the idea is clear. According to the Chinese character code in the string, go to the HZK16 character library to get the lattice information. After getting the information, print different characters according to the data of each point in the 16 * 16 lattice.

Note: when running the code, you need to add the HZK16 file in the folder where the code file is located. Otherwise, you cannot get the lattice data. The HZK16 file can be found in the download link

github Download

Multi word implementation

import binascii
KEYS = [0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01]

def printPlay(textStr,line,background):
    # Initialize the 16 * 16 dot matrix position. Each Chinese character needs 16 * 16 = 256 dots to represent, and 32 bytes to display a Chinese character
    # The reason for 32 bytes: 256 points each point is 0 or 1, then the total is 256 power of 2, and one byte is 8 power of 2
    rect_list = [] * 16
    for i in range(16):
        rect_list.append([] * 16)

    for text in textStr:
        #To obtain the gb2312 code of Chinese, a Chinese character is composed of two byte codes
        gb2312 = text.encode('gb2312')
        #Convert binary encoded data to hexadecimal data
        hex_str = binascii.b2a_hex(gb2312)
        #Convert data to strings by unicode
        result = str(hex_str, encoding='utf-8')

        #The first two digits correspond to the first byte of Chinese character: area code, 94 characters recorded in each area
        area = eval('0x' + result[:2]) - 0xA0
        #The last two bits correspond to the second byte of Chinese character: bit code, which is the position of Chinese character in its area
        index = eval('0x' + result[2:]) - 0xA0
        #The absolute offset position of Chinese characters in HZK16 is multiplied by 32 because each Chinese character in the font needs 32 bytes
        offset = (94 * (area-1) + (index-1)) * 32

        font_rect = None

        #Read HZK16 Chinese character library file
        with open("HZK16", "rb") as f:
            #Find the offset position of the target Chinese character
            f.seek(offset)
            #Read 32 bytes of data from the font data
            font_rect = f.read(32)

        #The length of font'rect is 32, which is equivalent to for k in range(16)
        for k in range(len(font_rect) // 2):
            #Data per row
            row_list = rect_list[k]
            for j in range(2):
                for i in range(8):
                    asc = font_rect[k * 2 + j]
                    #Here & for bitwise and operators in Python
                    flag = asc & KEYS[i]
                    #The data in the data rule acquisition font is added to 16 lines, 16 positions in each line, and each position
                    row_list.append(flag)

    #According to the 16 * 16 lattice information obtained, print it to the console
    for row in rect_list:
        for i in row:
            if i:
                #Foreground character (i.e. the output character used to represent the stroke of Chinese characters)
                print(line, end=' ')
            else:

                # Background character (that is, the output character used to represent the background)
                print(background, end=' ')
        print()

inpt = input("Input:")
lineSign = '■'

backgroundSign = '○'
#backgroundSign = "."
printPlay(inpt,lineSign,backgroundSign)

Keywords: Programming Python github encoding

Added by nadeemshafi9 on Wed, 05 Feb 2020 17:36:30 +0200