python字符画

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from PIL import Image
import argparse

#命令行输入参数处理
parser = argparse.ArgumentParser()

parser.add_argument('file') #输入文件
parser.add_argument('-o', '--output') #输出文件
parser.add_argument('--width', type = int, default = 80) #输出字符画宽
parser.add_argument('--height', type = int, default = 80) #输出字符画高

#获取参数
args = parser.parse_args()

IMG = args.file
WIDTH = args.width
HEIGHT = args.height
OUTPUT = args.output

def strB2Q(ustring):
"""半角转全角"""
rstring = ""
for uchar in ustring:
inside_code=ord(uchar)
if inside_code == 32: #半角空格直接转化
inside_code = 12288
elif inside_code >= 32 and inside_code <= 126: #半角字符(除空格)根据关系转化
inside_code += 65248

rstring += chr(inside_code)
return rstring

ascii_char = list("$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. ")


# 将256灰度映射到70个字符上
def get_char(r,b,g,alpha = 256):
# 白色返回空
if alpha == 0:
return ' '
length = len(ascii_char)
#灰度值公式
gray = int(0.2126 * r + 0.7152 * g + 0.0722 * b)
#因为70个字符的数组下标范围是从0到69,如果不加1,灰度为256时会取下标为70,此时超出数组范围
unit = (256.0 + 1)/length
return ascii_char[int(gray/unit)]

if __name__ == '__main__':
#载入IMG的文件
im = Image.open(IMG)
#重置或默认值
HEIGHT=int(WIDTH*im.size[1]/im.size[0]*0.55)
im = im.resize((WIDTH,HEIGHT), Image.NEAREST)

txt = ""

for i in range(HEIGHT):
for j in range(WIDTH):
#返回指定位置的像素
txt += get_char(*im.getpixel((j,i)))
txt += '\n'

print(txt)

#字符画输出到文件
if OUTPUT:
with open(OUTPUT,'w') as f:
f.write(txt)
else:
with open("output.txt",'w') as f:
f.write(txt)

参考链接: 图片转字符画 https://gitee.com/wangyufu/python-ASCII-Generator/blob/master/ascii.py
Python 图片转字符画 https://www.cnblogs.com/wangyufu/p/5602848.html

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from PIL import Image
import numpy as np

# 定义字符集,从暗到亮
ASCII_CHARS = "@%#*+=-:. "

def resize_image(image, new_width=100):
(original_width, original_height) = image.size
aspect_ratio = original_height / float(original_width)
new_height = int(aspect_ratio * new_width * 0.55) # 0.55是为了补偿字符高度
resized_image = image.resize((new_width, new_height))
return resized_image

def grayify(image):
return image.convert("L") # 转换为灰度图

def pixels_to_ascii(image):
pixels = np.array(image)
ascii_str = ""
for pixel_value in pixels:
for pv in pixel_value.tolist():
ascii_str += ASCII_CHARS[pv // 25] # 将像素值映射到字符集
return ascii_str

def convert_image_to_ascii(image_path, new_width=100):
try:
image = Image.open(image_path)
except Exception as e:
print(e)
return

image = resize_image(image, new_width)
image = grayify(image)

ascii_str = pixels_to_ascii(image)

img_width = image.width
ascii_str_len = len(ascii_str)
ascii_img = ""

# 将长字符串分割为行
for i in range(0, ascii_str_len, img_width):
ascii_img += ascii_str[i:i + img_width] + "\n"

return ascii_img

# 使用示例
if __name__ == "__main__":
image_path = "test.jpg" # 替换为你的图片路径
ascii_image = convert_image_to_ascii(image_path, new_width=100)
print(ascii_image)

# 如果需要将结果保存到文件中
with open("ascii_image.txt", "w") as f:
f.write(ascii_image)