代码
from __future__ import print_function
from PIL import Image
import argparse
codeLib = '''$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`' '''
count = len(codeLib)
print(count)
#彩色图变黑白图,映射
def transform1(image_file):
image_file = image_file.convert("L")
codePic = ''
for h in range(0,image_file.size[1]):
for w in range(0,image_file.size[0]):
gray = image_file.getpixel((w,h))
print(gray)
codePic = codePic + codeLib[int(((count)*gray)/256)]
codePic = codePic + '\r\n' #结尾自动换行
return codePic
fp = open(u'./image./zhu.jpg','rb')
image_file = Image.open(fp)
image_file = image_file.resize((int(image_file.size[0]*0.25),int(image_file.size[1]*0.13)))
tmp = open('zhu.txt','w')
tmp.write(transform1(image_file))
tmp.close
字符
$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'