python实现图片转字符串

学习pillow库 argparse库

原理

我们需要把一张彩色的图片对应到单色的字符画上。

灰度值:指黑白图像中点的颜色深度,范围一般从0到255,白色为255,黑色为0,故黑白图片也称为灰度图像

公式:

gray = int(0.2126*r+0.7152*g+0.0722*b)

安装pillow库

pip install pillow

获取图片

wget http://labfile.oss.aliyuncs.com/courses/370/ascii_dora.png

字符集

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

代码

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
# -*- coding: utf-8 -*-
# !/usr/bin/env python
from PIL import Image
import argparse
import numpy as np
def get_char(r,g,b,alpha=256):
if alpha == 0:
return ' '
length = len(ascii_char)
gray = int(0.2126*r+0.7152*g+0.0722*b)
unit = (256.0 + 1)/length
return ascii_char[int(gray/unit)]
def get_args():
#命令行输入参数处理
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
OUTPUT = args.output
HEIGHT = args.height
return IMG,WIDTH,OUTPUT,HEIGHT
if __name__ == '__main__':
IMG,WIDTH,OUTPUT,HEIGHT = get_args()
im = Image.open(IMG)
im = im.resize((WIDTH,HEIGHT),Image.NEAREST)
ascii_char = list("$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\()1{}[]?-_+~i!lI;:,\"^`'.")
txt = ""
for i in range(HEIGHT):
for j in range(WIDTH):
txt += get_char(*im.getpixel((j,i)))
txt += '\n'
#字符串输出到文件
if OUTPUT:
with open(OUTPUT,'w') as f:
f.write(txt)
else:
with open("dora.txt",'w') as f:
f.write(txt)

运行

python pic.py ascii_dora.png

结果

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
72
73
74
75
76
$$$$
$$bZuZo#cuuZYM$$
$WXuuuuW..."hu1.."hb$
$puuuuuuC....."X.....&ub$
$#uuuuuuuXh......M......Xuc$
$cuuuuuuuuh~....d"]..h1..MuuuB
$Zuuuuuuuuuu#....M.v."h.IM.vuuuu$
$Uuuuuuuuuuuu#....I..1L.....]pXuuu$
$UuuuuuuuuuuupM/......"].....Y..]oXX$
$uuuuuuuuuuXM/Id....../.LZL/.h....dc$
$uuuuuuuuuch...."~.....v1d.Xh8"....."8$$$
wuuuuuuuuh"......~L..rr.&t..hh....1M~M...$
$uuuuuuuuM.]Ld1..........*hthhh~.L/......."$
XuuuuuuuW.......1Yh/.....@hhhh*.~..........$
$uuuuuuup.............1h...%hh*/............d
wuuuuuuu~....................]....Mdvr]]]]I.d
$uuuuuuc&...~vv]]]I...........Z..............$
$uuuuuuM"..............]~.....d.............I
Wuuuuuu~......................v]...ZhvI.....$
puuuuuZ.....1&%%%dZv1I..//....]v........1Zh$
CuuuuuM...."hhhhhhho8%#oW%dv].]v........1LI$
Cuuuuud....]hhhhhWB%ohhhhhh*WBhd1I....~YM..$
Xuuuuu1....vh#%*hhhhhhhhhhhhhhhhhhhhhhhh"..$
Cuuuuu.....vhhhhhhhhhhhhhhhhhhhhhhhhhhh%...$
Cuuuuu.....Ihhhhhhhhhhhhhhhhhhhhhhhhhhh]...
puuuuu......ohhhhhhhhhhhhhhhhhhhhhhhhhB...r
Wuuuuu......8hhhoW%8Mhhhhhhhhhhhhhhhhh"...8
$uuuuu......Ih&Urrrrrr0BhhhhhhhhhhhhhZ...Y$
$uuuuu/......$rrrrrrrrrru@hhhhhhhhhhB....$
wuuuuM......"crrrrrrrrrrrkhhhhhhhhM....I
$uuuuU.......vrrrrrrrrrrrrWhhhhhho"....8
cuuuur.......hrrrrrrrrrrrrWhhhha~....I
puuuuM......."0rrrrrrrrrrr%hhhhW.....Z
$uuuuXd......."krrrrrrrrrrXhhh8....."$
buuuuX.........oqrrrrrrrrra%L......$
$uuuuu&..........L0rrrrrr0Y.......8
$uuuuu1...........~hkdo/........8
#uuuuU"......]/Z%$1..IBhhhhhhh*B
ZuuuuZ.~Z%#hhhhB......hMx~lxMhh$
$Cuuw%ahhhh*WW&".......Lqx(xOL/$
$$*hhh#8Quuuub........h?llllW.$
$Whh%puuuuuuu#........ZllYklY.#
$wuuuuuuuuuup........all{qlM.Y
XuuuuuuuuuuuI.......clllfx..]
Cuuuuuuuuuuud......rhlll?h..~
huuuuuuuuuuuZd....v"..]/.....
$uuuuuuuuuCh..~d%MdhdLvvvLddI$ $$$
$uuuuuucWXu..dr.............d$ $1....v$
$uuuC#buuuZ..v..............v$8.......r
uuuuuuuuu#..d..............Y$.........$
Cuuuuuuuu#..Z..............h".........h
huuuuuuuu#..."............"%..........Z
Buuuuuuuuw...d............dI..........d
$$$ $uuuuuuuuuI...h........."Z1...........8
Mhhh$$uuuuuuuuu8...."hv].]]YZ..$...........$
$hhhh%$uuuuuuuuuC......]vdvv]..rh...........$
$hhhhM$uuuuuuuuuuh............LuL..........1
ohhh$$uuuuuuuuuuup~.........&uuY..........$
$$$$ Buuuuuuuuuuuuu81....rhXuuuh.........I
puuuuuuuuuuuuuuuuCUuuuuuuuw.........$
CuuuuuuuuuuuuuuuuuuuuuuuCb#/.......h
$$uuuuuuuuuuuuuuuuuuuCW$$ $].....8
h".uuuuuuuuuuuuuuuuX@$ $dd$$
$...cuuuuuuuuuuuuuZ$
Z...#uuuuuuuuuuuC$
~...~uuuuuuuuuc$
.....Luuuuuuch$
......#uuuuu8$
......./oXC$
]..........8
Z...........
$...........$
Z..........$
$".......v
$#I....d$
$$$