from tqdm import tqdm
from PIL import Image
CharList = '''@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,"^`'. '''
Charcount = len(CharList)
PicName = input('Please enter your image file name:')
File = open(PicName, 'rb')
ImageFile = Image.open(File).convert('L')
Scaling = float(input('Please enter the scale you want:'))
Width = int(ImageFile.size[0] * Scaling)
Height = int(ImageFile.size[1] * Scaling)
ImageFile = ImageFile.resize((Width, Height))
print('Char-Image Width:{0}\nChar-Image Height:{1}'.format(Width, Height))
CharPic = open('CharPic.txt', 'w')
Progress = tqdm(total = Width * Height)
PicChar = ''
for h in range(Height):
for w in range(Width):
gray = ImageFile.getpixel((w, h))
PicChar += CharList[int(((Charcount - 1) * gray) / 256)]
Progress.update(1)
CharPic.write(PicChar + '\n')
PicChar = ''
Progress.close()
CharPic.close()
运行结果: