-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ascii.py
117 lines (97 loc) · 2.96 KB
/
Ascii.py
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import AsciiDico
import random
import imageio as imio
from PIL import Image
#img.itemset((10,10,2),100) : 0 b, 1 g, 2 r
def Average(img, x, y, xTo, yTo, step, isGrayScale):
myFunc = None
if isGrayScale :
myFunc = GetAverageGrayScale
else :
myFunc = GetAverageRGB
PixelNumber = 0
PixelSum = 0
for i in range(x, xTo, step):
for j in range(y, yTo, step):
PixelNumber += 1
PixelSum += myFunc(img, i, j)
return PixelSum / PixelNumber
def FindLetter(average, Arr) :
for mini, maxi, letter in Arr :
if (mini <= average and average < maxi) :
return letter
#for tuple in Arr :
# if (tuple[0] <= average and average < tuple[1]) :
# return tuple[2]
def GetAverageGrayScale(img, i, j) :
p = img.getpixel((i, j))
if type(p) == int:
p = (p, p, p)
r, g, b = p
return r
def GetAverageRGB(img, i, j) :
p = img.getpixel((i, j))
if type(p) == int:
p = (p, p, p)
r, g, b = p
#r = img[j, i, 0]
#g = img[j, i, 1]
#b = img[j, i, 2]
return (21 * r) / 100 + (72 * g) / 100 + (7 * b) / 100
def ToAscii(img, nbchar, path, inverted, precision, step, propxy = 2, Arr = AsciiDico.ArrBlocNote):
isGrayScale = True
if not IsGrayScale(img, precision):
# img = ToGrayScale(img)
isGrayScale = False
width, height = img.size
lines = 0
columns = 0
file = open(path, "w")
dx = int (width / nbchar)
dy = propxy * dx
j = 0
while j < height :
lines += 1
jTo = j + dy
if jTo >= height:
jTo = height - 1
i = 0
c = 0
while i < width :
c += 1
iTo = i + dx
if iTo >= width:
iTo = width - 1
average = Average(img, int(i), int(j), int(iTo), int(jTo), step, isGrayScale)
if inverted:
average = 255 - average
file.write(FindLetter(int(average), Arr))
i += dx
columns = c
file.write("\n")
j += dy
file.close()
return (lines, columns)
def IsGrayScale(img, precision) :
width, height = img.size
draw = img.load()
for i in range(precision) :
(x, y) = (random.randrange(width), random.randrange(height))
if draw[x, y][0] != draw[x, y][1] or draw[x, y][0] != draw[x, y][2] :
return False
return True
def ToGrayScale(img):
width, height = img.size
draw = img.load()
for x in range(width):
for y in range(height):
r = draw[x, y][0]
g = draw[x, y][1]
b = draw[x, y][2]
average = (21 * r) / 100 + (72 * g) / 100 + (7 * b) / 100
# img[x, y] = (average, average, average, 255)
draw[x, y] = (int(average), int(average), int(average))
# draw.itemset((x, y, 0), average)
# draw.itemset((x, y, 1), average)
# draw.itemset((x, y, 2), average)
return img