-
Notifications
You must be signed in to change notification settings - Fork 3
/
BrightnessToASCII.py
40 lines (31 loc) · 1.03 KB
/
BrightnessToASCII.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
from PIL import Image
# RGB mappings
def Average(RGB):
return (RGB[0]+RGB[1]+RGB[2])/3
def Lightness(RGB):
return (max(RGB)+min(RGB))/2
def Luminosity(RGB):
return 0.21*RGB[0] + 0.72*RGB[1] + 0.07*RGB[2]
# scale down Brightness(0,255) to custom ASCII(0,64)
def toASCII(b):
chars = r'`^\",:;Il!i~+_-?][}{1)(|\\/tfjrxnuvczXYUJCLQ0OZmwqpdbkhao*#MW&8%B@$'
slope = 66/255
return chars[round(slope*b)]
im = Image.open("ascii-pineapple.jpg")
if im != None:
pix = im.load()
width, height = im.size
bright = [None]*(width*height)
for col in range(width):
for row in range(height):
bright[width*row + col] = Average(pix[col,row])
asciiMatrix = [None]*(width*height)
for col in range(width):
for row in range(height):
asciiMatrix[width*row + col] = toASCII(bright[width*row + col])
print("Successfully constructed ASCII matrix!")
print("ASCII matrix size: %d x %d "%(im.size[0],im.size[1]))
print("Iterating through pixel ASCII characters:")
for row in range(height):
for col in range(width):
print(asciiMatrix[width*row + col])