current position:Home>Picture to character drawing in Python, so easy!

Picture to character drawing in Python, so easy!

2022-06-24 07:50:19AI technology base camp

ba52aa700d58bac36107cc0091536899.gif

author | Parson sauce

source | Python technology

I believe you often see people show all kinds of character paintings on the Internet , For this colorful world , What we see everyday are some high-definition color pictures , Occasionally a rough black-and-white character painting , It's quite relaxing .

Principle of character drawing

The principle of this character painting is also relatively simple , We can think of each character as a large pixel , A character can represent a color , The more characters , The more colors you can reflect , Character painting is more hierarchical .

Gray value and RGB

The gray value refers to the depth of the color at the point of the black-and-white image , Its scope is 0-255, White is 255, Black is 0, Other colors are somewhere in between .

RGB It's red, green and blue , Other colors can be obtained through different superposition .

To convert a picture to a character drawing , You need to define a character set first , Used for mapping with gray values , The image of each pixel RGB Value is converted to a grayscale value , Output the corresponding character to get the character drawing .

RGB To gray value , There is a transformational formula , Source network :

gray = (2126 * r + 7152 * g + 722 * b) / 10000

Code combat

I chose a picture at random :

bfbf509d5ce4e0cefe99c5317a782db6.png

My goal is to transform into the following :

46c0046f74b5f03036ad1eb0e9beeacc.png

According to the above conversion principle , Let's go straight to the code :

from PIL import Image

char = list('M3NB6Q#OC?7>!:–;. ')

def get_char(r, g, b, alpha=256):
    if alpha == 0:
        return ' '
    
    grey = (2126 * r + 7152 * g + 722 * b) / 10000
    
    char_idx = int((grey / (alpha + 1.0)) * len(char))
    return char[char_idx]

def write_file(out_file_name, content):
    with open(out_file_name, 'w') as f:
        f.write(content)

def main(file_name="input.jpg", width=100, height=80, out_file_name='output.txt'):
    text = ''
    im = Image.open(file_name)
    im = im.resize((width, height), Image.NEAREST)
    for i in range(height):
        for j in range(width):
            text += get_char(*im.getpixel((j, i)))
        text += '\n'
    print(text)
    write_file(out_file_name, text)

if __name__ == '__main__':
    main('dance.png')

The idea of procedure :

  • Defines an array of characters , The characters in this array can be written at will .

  • Resolution images , Parse each pixel in the picture into RGB value .

  • According to our above formula , Convert each pixel into a character in the character array .

  • Concatenate the characters corresponding to all pixels , The conversion is complete .

summary

Picture to character , The principle and idea of transformation are relatively simple , The implementation is not complicated , You can try it !

4e5aa795194ac32b1f01cdfd6a0e0364.gif

Looking back

Matplotlib Two methods of drawing torus !

13 individual python Necessary knowledge , Recommended collection !

Artifact , Easy visualization Python Calling process !

Low code out of half a lifetime , Come back or " cancer "!

 Share 
 Point collection 
 A little bit of praise 
 Click to see 

copyright notice
author[AI technology base camp],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/175/202206240348504242.html

Random recommended