from PIL import Image
str = """@%&#*?-_+~!;:,"^`'.""" #从左至右,字符密度递减
img = Image.open(r'D:/Python35/mypy/onpiece.jpg')
img = img.resize((int(img.size[0]*0.5), int(img.size[1]*0.3)))#缩小(width,height)
img = img.convert('L') #转灰度
text = ""
for h in range(0,img.size[1]):
for w in range(0,img.size[0]):
gray = img.getpixel((w,h))
text = text + str[int(((len(str)-1)*gray)/256)]
text = text + "\r\n"
textfile = open('D:/Python35/mypy/aa.txt','w')
textfile.write(text)
#img.show()
图片显示太大,文字缩小了好几倍: