script: translate picture into ascii.text

script: translate picture into ascii.text

from:

  • 《python极客编程项目》

Intention:

  • translate picture into ascii.text

theory:

  • use pillow to translate a color image to black an white image
  • use grey level to get the average of every rectangle in the picture
  • replace the value with the scale:
  • output the list of the image

final 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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import  argparse
import numpy as np
from PIL import Image

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

def getAverageL(image):
#return the average of rectangle grey value
im = np.array(image)
w,h = im.shape
return np.average(im.reshape(w*h))

def covertImageToAscii(filename, cols, scale, moreLevels):
#translating a color image to black and white(mode “L”),
image = Image.open(filename).convert('L')
W, H = image.size[0], image.size[1]
print("input image dims: %d x %d "%(W,H))
w = W/cols
h = w/scale
rows = int(H/h)

print("cols: %d, rows: %d" % (cols, rows))
print("tule dims: %d x %d" % (w, h))

if cols > W or rows > H:
print("Image too small for specified cols!")
exit(0)

aimg = []
for j in range(rows):
y1 = int (j*h)
y2 = int ((j+1)*h)
if j == rows-1:
y2 = H

aimg.append("")
for i in range(cols):
x1 = int(i*w)
x2 = int((i+1)*w)

if i == cols-1:
x2 = W
#cut a rectangle
img = image.crop((x1,y1,x2,y2))
#get the average grey levels of the rectangle
avg = int(getAverageL(img))

if moreLevels:
gsval = gscale1[int((avg*69)/255)]
else:
gsval = gscale2[int((avg*9)/255)]

aimg[j] += gsval

return aimg

def main():

descStr = "This program starts"
parser = argparse.ArgumentParser(description=descStr)

parser.add_argument('--file', dest='imgFile', required=True)
parser.add_argument('--scale', dest='scale', required=False)
parser.add_argument('--out', dest='outFile', required=False)
parser.add_argument('--cols', dest='cols', required=False)
parser.add_argument('--morelevels', dest='moreLevels', action='store_true')

args = parser.parse_args()

imgFile = args.imgFile

outFile = 'out.txt'
if args.outFile:
outFile = args.outFile

#the height of output ascii picture is longer than width
scale = 0.43
if args.scale:
scale = int(args.scale)

cols = 90
if args.cols:
cols = int(args.cols)

print('generating ASCII art..')

aimg = covertImageToAscii(imgFile, cols, scale, args.moreLevels)
with open(outFile, 'w+') as f:
for row in aimg:
f.write(row + '\n')

print('ASCII art written to %s' % outFile)

if __name__ == '__main__':
main()

simple test:

1
python ascii.py --file 1.png --cols 82
文章目录
  1. 1. script: translate picture into ascii.text
|