实现效果
原始图片
![这里写图片描述](https://img-blog.csdn.net/20180817162527157?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTIzMTM3NTE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
在文本中打印的字符串
![这里写图片描述](https://img-blog.csdn.net/20180817162745636?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTIzMTM3NTE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
实现代码
学习使用 python 中的 opencv 库
import cv2
ascii_char = list(r"$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. ")
WIDTH = 60
HEIGHT = 46
def picture_letter(x):
if x == 0:
return ' '
length = len(ascii_char)
unit = 257/length
print(x/257*71)
return ascii_char[int(x/unit)]
if __name__ == '__main__':
orgimg = cv2.imread(r"E:\VScode\a_b.jpg")
img = cv2.resize(orgimg, (WIDTH, HEIGHT), interpolation=cv2.INTER_CUBIC)
grayimg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
txt = ""
for i in range(0,HEIGHT):
for j in range(WIDTH):
txt += picture_letter(grayimg[i, j])
txt = txt+ '\n'
with open(r"E:\test\output.txt", 'w') as f:
f.write(txt)