图片转字符画

code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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")
# python

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×