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

How to use Python to transform picture into character picture

編輯:Python

Catalog

Preface :

example 1

example 2

Preface :

Character painting : Character painting is a combination of a series of characters , We can think of characters as larger pixels , A character can represent a color , The more types of characters , The more colors you can show , The picture will also be more layered .

If we want to draw character drawings by hand , First of all, we must have a solid foundation in fine arts , Secondly, it takes a lot of time and energy . But we can use Python, Just a few lines of code , You can easily convert a picture into a character picture .

example 1

design sketch :

The theory is simple , Is to operate the picture , I need to use PIL Of python package , There is a very useful image processing function .

Open the picture first , Resize the image :

img = Image.open(picPath)img = img.resize((picW, picH))

Then read the gray value , Then the gray value and the character corresponding to the line .

from PIL import ImagelstChars = list("[email protected]%8&WM#*oahkbdpqwmZO0QLaCJUYXzczjhdhsdavunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'.") def oneChars(r, g, b, alpha = 256): global lstChars length = len(lstChars) gray = int(0.2126 * r + 0.7152 * g + 0.722 * b) index =length*gray return lstChars[index]picPath = "C:\Users\Administrator\Desktop\\aaaaa\\aa.png"picH = 40picW = 80img = Image.open(picPath)img = img.resize((picW, picH))txt = ""for y in range(picH): for x in range(picW): txt += oneChars(img.getpixel((x, y))) txt += '\n'print txt example 2

Here is the source code :

# -*- coding=utf-8 -*-from PIL import ImageIMG = 'IMG.jpg'  # Set picture file WIDTH = 150  # Set the width of the character drawing HEIGHT = 80  # Set the height of the character drawing OUTPUT = 'T.txt'  # Set the text file for storing character drawings ascii_char = list("[email protected]%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. ")  # Set the character set to be displayed # take 256 Grayscale maps to 70 On characters def get_char(r, g, b, alpha=256):   # alpha For transparency    # Judge alpha value , by 0 It means full transparency    if alpha == 0:       return ' '   # Gets the length of the character set , Here for 70   length = len(ascii_char)   # take RGB Value to grayscale value gray, The gray value range is 0-255   gray = int(0.2126 * r + 0.7152 * g + 0.0722 * b)   # The gray value range is 0-255, The character set has only 70   # The following processing is required to map the gray value to the specified character    # Prevent when the gray value is 255 when , The output of the 70 Characters out of list index , So we need to (255+1)   unit = (255.0 + 1) / length   # Returns the character corresponding to the gray value    return ascii_char[int(gray / unit)]if __name__ == '__main__':   # Open and adjust the width and height of the picture    im = Image.open(IMG)   im = im.resize((WIDTH, HEIGHT), Image.NEAREST)   # Initialize the output string    txt = ""   # Traverse every line in the picture    for i in range(HEIGHT):       # Traverse each column in the row        for j in range(WIDTH):           # take (j,i) Coordinate RGB Pixels are converted to characters and added to txt character string            txt += get_char(*im.getpixel((j, i)))       # After traversing a line, you need to add a newline character        txt += '\n'   # Output to the screen    print(txt)   with open(OUTPUT, 'w') as f:       f.write(txt)

Just paste it directly , Just import a picture in the folder and rename it as IMG.jpg. The running result will generate a file named T.txt The file of .

Original picture :

Generate file graph :

This is about how to use python This is the end of the article on converting pictures into character pictures , More about python Please search the previous articles of the software development network or continue to browse the relevant articles below. I hope you will support the software development network in the future !



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