將圖片轉化為txt文字顯示

原圖:
這裡寫圖片描述

效果顯示:(txt字型大小為1)

這裡寫圖片描述

效果顯示:(txt字型大小為2)

這裡寫圖片描述

python原始碼

#匯入cv模組
import cv2 as cv
import random
import numpy as np
# # =================================存取影象=====================================
# 60號宋體字母佔用字元比較
xiangsi={
"I":198,"V":229,"L":230,"U":235,"C":236,"Y":236,"T":245,"J":248,"A":262,"X":265,"Z":270,"N":276,"G":281,"F":284,"P":303,"O":306,"E":322,"K":339,"D":357,"W":373,"R":380,"H":384,"Q":391,"B":391,"S":292,"M":432
}
I_value=180   # I對映為不同的值表示亮度,
M_value=50   # M對映為50
x=(M_value-I_value)/(xiangsi['M']-xiangsi['I'])
b=I_value-xiangsi['I']*x
#歸一化,後陣列遞減排列
for zimu in xiangsi:
xiangsi[zimu]=int(x*xiangsi[zimu] b)   # 將字母表示為對應的黑度值。
print(xiangsi)
# # 直接讀取單通道灰度圖
gray_img = cv.imread('hui.jpg', cv.IMREAD_GRAYSCALE)
gray_img = cv.resize(gray_img,(int(gray_img.shape[1]),int(gray_img.shape[0]/1.7)),interpolation=cv.INTER_CUBIC)
# cv.imshow('iker', gray_img)
# cv.waitKey(0)
print(gray_img.shape)   # 先是高度, 再是寬度
file = open('img.txt',mode='w')
for row in range(gray_img.shape[0]):
for col in range(gray_img.shape[1]):
pzimu=" "
cansee=False
for zimu in xiangsi:
if (gray_img[row][col] > xiangsi[zimu]):
file.write(pzimu)
cansee=True
break
else:
pzimu=zimu
if(not cansee):file.write(pzimu)
file.write('\n')
file.close()