Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

The realization principle of python character drawing

Shulou Source: shulou.com Published: 2022-06-02 05:06:17 09月30日 Update

This article mainly explains "the realization principle of python character painting". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the realization principle of python character painting".

1. Let's see the effect first.

The original picture is

Character painting

two。 Realization principle

In fact, the principle is very simple, first of all, to prepare a character set

Char_set =''$@ B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\ | () 1 {} []?-_ + ~ ionomlis,\ "^'.''

Secondly, to convert the picture into a grayscale image, the so-called grayscale image is a black-and-white photo, and in this process, the picture should be reduced, and the proportion of each picture is different, which should be decided according to the actual situation of the picture. in this way, you get a scaled-down black-and-white photo.

Im = Image.open ('qq.png')

Im = im.resize (80,50), Image.ANTIALIAS)

Im = im.convert ('L') # is converted to black and white, each pixel has a grayscale value, from 0 to 255,0 is black, 255is white

Im.save ('t.jpeg') # saves pictures only to demonstrate black-and-white photos

It looks a little ugly. This black-and-white photo has a total of 80 to 50 pixels, and each pixel can get a grayscale value by using the im.getpixel method, which ranges from 0 to 255. 0 is black, 255 is white, and the middle is gray from black to white.

The next thing to do is to convert 4000 gray values into characters.

Def get_char (gray):

If gray > = 240:

Return''

Else:

Return char_ set [int (gray/ ((256.0 + 1) / len (char_set)]

If the grayscale value is greater than 240, I convert it to an empty string so that it looks comfortable, and the rest is mapped proportionally to the character set.

To emphasize that the generated txt file do not open to view, so that you can not see a complete character painting, open the character painting in the browser, so that you can see the complete, if you zoom out a little more, the resulting character painting will lose a lot of details, in general, the width of 80 on it, height depends on the situation adjustment.

3. Complete code

From PIL import Image

Char_set =''$@ B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\ | () 1 {} []?-_ + ~ ionomlis,\ "^'.''

Im = Image.open ('qq.png')

Im = im.resize (80,50), Image.ANTIALIAS)

Im = im.convert ('L') # is converted to black and white, each pixel has a grayscale value, from 0 to 255,0 is black, 255is white

Im.save ('t.jpeg')

Def get_char (gray):

If gray > = 240:

Return''

Else:

Return char_ set [int (gray/ ((256.0 + 1) / len (char_set)]

Text =''

For i in range (im.height):

For j in range (im.width):

Gray = im.getpixel ((j, I)) # the return value may be an int or a triple

If isinstance (gray, tuple):

Gray = int (0.2126 * gray [0] + 0.7152 * gray [1] + 0.0722 * gray [2])

Text + = get_char (gray)

Text + ='\ n'

With open ('pic.txt', 'w') as f:

F.write (text)

Thank you for your reading, the above is the content of "the realization principle of python character painting". After the study of this article, I believe you have a deeper understanding of the realization principle of python character painting, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Characters grayscale principle black and white pictures pixels photos that is situation scale black learning content character set white generated same comfortable ugly next Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Redmi Shulou Technology Huawei Shulou Tech Info