Python image to character drawing

 
 

PIL installation is very troublesome, it is recommended to download exe and install it directly

PIL official website: http://pythonware.com/products/pil/

But now the download link can't be accessed, I uploaded the 32-bit and 64-bit versions to the blog garden for download

PILwin32:http://files.cnblogs.com/files/pcat/PILwin32.zip

PILwin64:http://files.cnblogs.com/files/pcat/PILwin64.zip



#
coding=utf-8 from PIL.Image import Image import argparse #Command line input parameter processing parser= argparse.ArgumentParser() parser.add_argument( ' file ' ) #input file parser.add_argument( ' -o ' , ' --output ' ) parser.add_argument('--width',type=int,default=80) parser.add_argument('--heigth',type=int,default=80) args=parser.parse_args() IMG=args.file WIDTH=args.width HEIGHT=args.heigth OUTPUT=args.output ascii_char = list("$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. ") #map 256 grayscale to 70 characters def get_char(r,g,b,alpha=256): if alpha==0: return ' ' length=len(ascii_char) gray=int(0.2126 * r + 0.7152 * g + 0.0722 * b) unit=(256.0+1)/length return ascii_char[int(gray/unit)] im = Image.open(IMG) im = im.resize((WIDTH,HEIGHT), Image.NEAREST) txt="" for i in range(HEIGHT): for j in range(WIDTH): txt+=get_char(*im.getpixel((j,i))) print txt #Character drawing output file if OUTPUT: with open(OUTPUT,'w') as f: f.write(txt) else: with open("output.txt",'w') as f: f.write(txt)

链接:https://www.shiyanlou.com/courses/370/labs/1191/document

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325073745&siteId=291194637