mliu222

注意:转化成txt后,txt的字体使用“宋体”,不能使用“微软雅黑”,否则图像会变形

import numpy as np
from PIL import Image

if __name__ == \'__main__\':
image_file = \'huarun3.png\'
height = 100

img = Image.open(image_file)
img_width, image_height = img.size
print(\'img_width:\', img_width)
print(\'image_height:\', image_height)
width = int(height / image_height * img_width * 2)
img = img.resize((width, height), Image.ANTIALIAS)
print(img)

img_l = img.convert(\'L\')
pixels = np.array(img_l)
print(img_l)

chars = \'MNHQ$OC?7>!:-;. \'
step = 256 // len(chars)
result = \'\'
for i in range(height):
for j in range(width):
result += chars[pixels[i][j] // step]
result += \'\n\'

with open(image_file + \'.txt\', mode=\'w\') as f:
f.write(result)

分类:

技术点:

相关文章:

  • 2021-11-29
  • 2021-09-25
  • 2021-08-18
  • 2022-01-18
  • 2021-12-13
  • 2021-09-01
  • 2021-12-28
猜你喜欢
  • 2019-08-13
  • 2021-12-28
  • 2021-08-04
  • 2021-11-04
  • 2021-12-13
  • 2021-10-26
相关资源
相似解决方案