程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

So easy, using Python to convert pictures to character drawings

編輯:Python

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 . Like to remember to collect 、 Focus on 、 give the thumbs-up .

notes : At the end of the article, a technical exchange group is provided

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 :

My goal is to transform into the following :

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 !

Recommended articles

  • Li Hongyi 《 machine learning 》 Mandarin Program (2022) coming

  • Some people made Mr. Wu Enda's machine learning and in-depth learning into a Chinese version

  • Addicted to , Recently, a large visual screen has been rolled out for the company ( Source code attached )

  • So elegant ,4 paragraph Python Automatic data analysis artifact is really fragrant

  • Combing for more than half a month , Well prepared 17 A map of knowledge and thinking , Let's talk about statistics this time

  • Year end summary :20 Visual large screen template , Direct application is really fragrant ( The source code is attached at the end of the article )

Technical communication

Welcome to reprint 、 Collection 、 Gain some praise and support ! data 、 The code can be obtained from me

At present, a technical exchange group has been opened , Group friends have exceeded 2000 people , The best way to add notes is : source + Interest direction , Easy to find like-minded friends

  • The way ①、 Send the following picture to wechat , Long press recognition , The background to reply : Add group ;
  • The way ②、 Add microsignals :dkl88191, remarks : come from CSDN
  • The way ③、 WeChat search official account :Python Learning and data mining , The background to reply : Add group


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved