本文主要是介绍Python 将图片转换字符串输出,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
from PIL import Image
char = list('M3NB6Q#OC?7>!:–;. ')
def get_char(r, g, b, alpha=256):
if alpha == 0:
return ' '
grey = (2126 * r + 7152 * g + 722 * b) / 10000
char_idx = int((grey / (alpha + 1.0)) * len(char))
return char[char_idx]
def write_file(out_file_name, content):
with open(out_file_name, 'w') as f:
f.write(content)
def main(file_name="input.jpg", width=100, height=80, out_file_name='output.txt'):
text = ''
im = Image.open(file_name)
im = im.resize((width, height), Image.NEAREST)
for i in range(height):
for j in range(width):
text += get_char(*im.getpixel((j, i)))
text += '\n'
print(text)
write_file(out_file_name, text)
if __name__ == '__main__':
main('katong.jpg')
这篇关于Python 将图片转换字符串输出的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!