from PIL import Image
#简陋的字符画 by la_deng
def main(IMG):
wh = input('请输入生成图片的宽&高(元组)[可省略]: ')
if wh == '':
WIDTH=80
HEIGHT=80
else:
WIDTH,HEIGHT=eval(wh)
lst=list("$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,"^`'. ")
def get_char(r,g,b,alpha=256):
if alpha==0:
return ' '
lenght=len(lst)
gray=int(0.2126 * r + 0.7152 * g + 0.0722 * b)
unit=256/lenght
return lst[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):
a=im.getpixel((j,i))
txt += get_char(*a)
txt += '\n'
print(txt)
r=input('是否保存? Y/N : ')
if r == 'Y' or r == 'y':
with open('output.txt','w') as f:
f.write(txt)
while 1:
try:
main(IMG=input('请输入图片地址 :'))
except:
print('这不是有效输入!!!')