图片转字符画

  • 2018-06-14
  • 268
  • 0
  • 1
# coding:utf-8
from PIL import Image


def img_to_txt(file_sou, file_dst):
    x = 120
    # str_ = list('$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`\'. ')
    str_ = list('@#+:,`.')
    # str_ = list('$@B%8&WM#* ')
    # str_ = list(' .,\";*=+')
    # 亮暗反转
    # str_.reverse()
    img = Image.open(file_sou).convert('L')
    img = img.resize((x, img.size[1] // ((img.size[0] // x))))
    size = img.size
    dst = ''
    for y in range(size[1]):
        for x in range(size[0]):
            tmp = img.getpixel((x, y))
            dst += str_[(tmp*(len(str_) - 1)) // 255] * 2
        dst += '\n'
    with open(file_dst, 'w') as f:
        f.write(dst)


if __name__ == '__main__':
    import os
    import sys
    filename_s = sys.argv[1]
    filename_d = sys.argv[2]
    if os.path.exists(filename_s):
        img_to_txt(filename_s, filename_d)
    else:
        print("file not found")

评论

还没有任何评论,你来说两句吧

发表评论