#图像像素到字符的转换
import numpy as np
from PIL import Image
if
name
== ‘
main
’:
image_file = “girl.jpg”
height = 116
img = Image.open(image_file)
print("img=",img)
img_width,img_height = img.size
width = int(1.5*height*img_width // img_height)
img = img.resize((width,height),Image.ANTIALIAS)
pixels = np.array(img.convert('L'))
chars = "AVMNHQ$OC?7>:;."
N = len(chars)
step = 256 // N
print(N)
result = ''
for i in range(height):
for j in range(width):
result += chars[pixels[i][j] // step]
result += '\n'
with open('text.txt',mode = 'w') as f:
f.write(result)