以下均为本人亲自所写,不得未经允许或未经备注使用!
PNG 到文字转换机
from PIL import Image
import numpy as np
img = Image.open('img.png')
thresh = 170
fn = lambda x : 255 if x > thresh else 0
r = img.convert('L').point(fn, mode='1')
r.save('foo.png')
img = Image.open('foo.png')
arr = np.array(img)
f = open("output.txt", "w")
x, y = 7, 5
for i in range(0, len(arr), x):
str = ""
for j in range(0, len(arr[i]), y):
if arr[i][j]:
str += ' '
else:
str += '.'
str += '\n'
f.write(str)
f.close()