Today I’m going to share with you a simple and fun Python project called “Picture to character drawing”. Nonsense not to say, the first effect of a map to confuse sentient beings.

Yes, picture to character painting is the process of converting the pictures we usually see into strings according to the RGB value and gray value of pixels.

B: well… That sounds like a lot of brain work. It doesn’t matter, listen to small five for small friends one by one.

Pixel RGB values

What are pixel RGB values?

In fact, as we usually see all the pictures, whether color or black and white, in fact, they are colored, more magical is that almost all the colors we see by the naked eye are Red, Green, Blue after different depths of color.

RGB color mode is a color standard in the industry. It is used to obtain various colors through the changes of red ®, green (G) and blue (B) color channels and their superposition among each other.

Therefore, RGB is the color representing the red, green and blue channels. This standard includes almost all colors that human vision can perceive, and it is one of the most widely used color systems at present.

So we can say that all the colored images we see are made up of these three colors.

For character painting, as the name implies, it is a combination of a series of characters. We can think of characters as relatively large pixels, and a character can represent a color.

Just like the picture we just saw after the character painting, the painting contains a variety of different levels of characters, so the more types of characters, the more colors can be expressed, the picture will also have a sense of layers.

So friends may ask, we are going to convert a color picture, so many colors, how to correspond to only one color and also contains so many characters of the character draw up?

Ha ha, don’t know? Here is the concept of gray value. Come on, come on!

Grayscale calculation

Gray value: refers to the color depth of the point in the black and white image, generally ranging from 0 to 255, white is 255, black is 0, so the black and white picture is also called gray image.

Therefore, we can use the gray value formula to map the RGB value of pixels to the gray value, which is the following formula :(note that this formula is simplified, the real calculation formula will be more complicated)

Gray value calculation formula

Gray = 0.2126 * r + 0.7152 * g + 0.0722 * b

To do this, we can create a non-repeating list of characters and use each character to represent a color, with small (dark) shades represented by the symbol at the beginning of the list and large (light) shades represented by the symbol at the end of the list.

Attention! Here we go! Knock on the blackboard!!

We know that. From black to white, the color depth ranges from 0 to 255, meaning that there are 256 different shades of color. If we wanted to represent each color with a different character, wouldn’t we need 256 different characters?

Ha ha, I think so, to find 256 different characters written in your program to form a string, it’s exciting to think about, but would you do it? I don’t think so?

So we just need to think of a way to make gray values of similar color depth represented by different symbols, so we can define a string of different characters, used to represent different color characters. The string defined here has a length of 70.

Defines characters that represent grayscale

ascii_char = list(“$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/|()1{}[]? -_+~<>i! lI; :,”^ ‘. “) # Set the character set to display

Grayscale mapping character function

Then we just need to define a function that finds the corresponding character for each pixel of the picture we need to convert to character painting and returns it.

This function takes the pixel value at a point on the image and a transparency parameter:

Map 256 grayscale to 70 characters

def get_char(r,g,b,alpha = 256):

#alpha for transparency

# Determine alpha value, 0 indicates full transparency

if alpha == 0:

return ‘ ‘

Get the length of the character set

length = len(ascii_char)

# Change RGB value to gray value, gray value range 0-255

Gray = int(0.2126 * r + 0.7152 * g + 0.0722 * b)

# Grayscale values range from 0 to 255, and the character set is only 70

The grayscale value needs to be mapped to the specified character

# prevent the 70th character from being output beyond the index of the list when the gray value is 255.

Unit = (255.0 + 1)/length

# Return the character corresponding to the grayscale value

return ascii_char[int(gray/unit)]

After we write the function method of the pixel mapping string, we can get different character representations for different pixels.

Picture import and size setting

Now, how do we get the pixels at a certain point in the image? At this point we need to call the Pillow library, which does the basic processing of the image.

Here we need to use the Image class from the Pillow library. Using the resize method under Image, we can scale the Image and set the quality of the output Image.

Take this image for example:

Zooming and output quality Settings

IMG = ‘t01b2a945701805d7f1.jpg’ # set the image file

WIDTH = 150 # Set the WIDTH of the character drawing

HEIGHT = 80 # Sets the HEIGHT of the character drawing

im = Image.open(IMG)

im = im.resize((WIDTH,HEIGHT), Image.NEAREST)

Traverse the image to get the character

You can then call the image.getPixel () method and pass in a coordinate parameter to return the value of the pixel at that coordinate, notably if the pixel at that coordinate has a transparent property.

The alpha transparency parameter is also returned, which is why we pass the transparency parameter in the argument of the mapping string function method and make a judgment.

Convert RGB pixels of (j, I) coordinates to characters and add to TXT string

txt += get_char(*im.getpixel((j,i)))

Now what we need to do is to traverse the horizontal and vertical coordinates of the picture of this size, get the character mapped at each coordinate point, and store it in the list of picture strings we set.

Walk through each line of the picture

for i in range(HEIGHT):

Walk through each column in the row

for j in range(WIDTH):

# Convert RGB pixels of (j, I) coordinates to characters and add to TXT string

txt += get_char(*im.getpixel((j,i)))

Add a newline character after traversing a line

txt += ‘\n’

Character drawing output and import files

Finally, output the character list on the screen or save it in a text file. In order to better see the effect of character painting, the Big bad Wolf suggests saving the character list in a text file.

The import file

OUTPUT = ‘output5.txt

Save to a text file

with open(OUTPUT,’w’) as f:

f.write(txt)

After converting the picture to character painting and saving it in a text file, we can find the photo after character in the text file.

Ok, so at this point we’re basically done with the picture to character drawing.

Remove the comment last less than 25 lines, users just need to modify the program’s top picture path to their own picture!