Home  >  Article  >  php教程  >  Use emoji characters to form any Chinese character

Use emoji characters to form any Chinese character

高洛峰
高洛峰Original
2016-11-23 15:51:585228browse

Using emoji expressions to form Chinese characters or simple drawings is widely spread on WeChat and QQ, so I would like to try to make it myself.

For example, the picture below:

Use emoji characters to form any Chinese character

The realization that I thought of at the beginning The solution is: first convert Chinese characters into pictures with black characters on a white background, and then map the pictures into different characters based on grayscale values ​​and print them on the terminal.

The former text-to-picture conversion is very easy to implement using common drawing libraries (such as python's PIL library); the workload is nothing more than slowly adjusting the layout during the implementation process to achieve your desired effect.

The latter is simple It’s just a mapping relationship: map the gray value of the pixels in the picture to a certain character; in this implementation, there are only two mapping relationships for pictures with black background and white characters, black characters correspond to the emoji characters to be replaced, and white background corresponds to blank .

The python implementation code of this part is as follows:

ascii_char = list('1234567890abcd ')  # 任意多个字符,灰度值的映射区间

def select_ascii_char(r, g, b):
    ''' 在灰度图像中,灰度值最高为255,代表白色; 最低为0,代表黑色 '''
    # 把RGB转为灰度值,并且返回该灰度值对应的字符标记
    # 'RGB-灰度值'转换公式如下
    gray = int((19595 * r + 38469 * g + 7472 * b) >> 16)
    # ascii_char中的一个字符所能表示的灰度值区间
    unit = 256.0 / len(ascii_char)
    return ascii_char[int(gray/unit)]

select_ascii_char realizes mapping a pixel into a specific custom character char.

In the implementation of this function, ascii_char is simpler and only requires two characters That is: one is any given, and the other is a space.

So replace ascii_char with [u'❤️ ', u' ']

Due to the need to map the pixels of the image to characters that can be printed on the terminal, the display of the terminal The space is limited, so the image needs to be reduced and adjusted.

zh2emoji code has the corresponding code download on my github. If you are interested, you can fork it to achieve more customized functions you want.

Show

I have N multiple ways of writing the word "fennel":

print image2print(word2image(u'茴'), u'❤️ ')
print image2print(word2image(u'茴'), u'W ')
print image2print(word2image(u'茴'), u'茴', width=40)

The output results are as follows:

Use emoji characters to form any Chinese character

Use emoji characters to form any Chinese character

Use emoji characters to form any Chinese character

More output:

Use emoji characters to form any Chinese character

Use emoji characters to form any Chinese character

Extended

based on z h2emoji,self Implemented an extension that may be useful: demo_show_animation.py.

It can print out a sentence in sequence on the terminal, using emoji or other characters you decide.

For example, if you try to execute python ./demo_show_animation.py Like Just click like, and an animation displaying text will be executed in the terminal. The text displayed is what you just entered.

If you can persist till this point, have you thought of some interesting ways to play? Welcome to participate. . Programming is a kind of fun, and code is a medium for realizing some mental ideas; programmers write code, just like writers write words, it is a habit.

In the future, I may plan to play an animation composed of emojis in the terminal (such as the loading animation of Buka Girl)


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn