-
Notifications
You must be signed in to change notification settings - Fork 1
/
ImageProcesser.py
108 lines (76 loc) · 3.34 KB
/
ImageProcesser.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
from cgitb import grey
from math import ceil, floor
from turtle import color
import imageio.v3 as imageio
import numpy as np
from progress.bar import PixelBar
def RgbToGray(rgb):
r = rgb[0]
g = rgb[1]
b = rgb[2]
gray = 0.2989 * r + 0.5870 * g + 0.1140 * b
return gray
greyRamp = "GREYramp."
def GetCharacterFromGreyscale(gs):
return greyRamp[ceil((len(greyRamp) - 1) * gs / 255)]
def colored(r, g, b, text):
return "\033[38;2;{};{};{}m{}\033[38;2;255;255;255m".format(r, g, b, text)
def GenerateAscii(img, chunkWidth, chunkHeight, ProgressCallback):
totalLoops = img.shape[0] * img.shape[1]
currentProgress = 0
# newImage = np.zeros((floor(img.shape[0] / chunkHeight), floor(img.shape[1] / chunkWidth), 3), dtype="uint8")
asciiText = ""
# escapedConsoleText = ""
bar = PixelBar('Processing', max=totalLoops)
for newY in range(0, (floor(img.shape[0] / chunkHeight))):
for newX in range(0, floor(img.shape[1] / chunkWidth)):
imageX = newX * chunkWidth
imageY = newY * chunkHeight
pixels = []
pixelColors = []
# print("imageY + chunkHeight: ", imageY + chunkHeight, ", img.height: ", img.shape[0], ", min: ", min((imageY + chunkHeight), img.shape[0]))
for y in range(imageY, min((imageY + chunkHeight), img.shape[0])):
for x in range(imageX, min((imageX + chunkWidth), img.shape[1])):
pixelColors.append(img[y][x])
pixels.append(RgbToGray(img[y][x]))
bar.next()
currentProgress += 1
exit = ProgressCallback(currentProgress, totalLoops)
if (exit):
return None
colorAverage = [0, 0, 0]
greyscaleAverage = 0
for i in range(len(pixels)):
greyscaleAverage += pixels[i]
colorAverage[0] += pixelColors[i][0]
colorAverage[1] += pixelColors[i][1]
colorAverage[2] += pixelColors[i][2]
if (len(pixels) > 0):
greyscaleAverage = greyscaleAverage / len(pixels)
colorAverage[0] = colorAverage[0] / len(pixelColors)
colorAverage[1] = colorAverage[1] / len(pixelColors)
colorAverage[2] = colorAverage[2] / len(pixelColors)
else:
greyscaleAverage = 0
colorAverage[0] = 0
colorAverage[1] = 0
colorAverage[2] = 0
char = GetCharacterFromGreyscale(greyscaleAverage)
asciiText += char
# escapedConsoleText += colored(colorAverage[0], colorAverage[1], colorAverage[2], char)
# if (colorAverage[0] != colorAverage[1]):
# print("Different!")
# print("color average: ", colorAverage)
# escapedConsoleText += colored(255, 233, 0, char)
# newImage[newY][newX][0] = average
# newImage[newY][newX][1] = average
# newImage[newY][newX][2] = average
asciiText += "\n"
# escapedConsoleText += "\n"
return asciiText
def defaultCallback(a, b):
return False
if __name__ == "__main__":
image = imageio.imread("images/cat.jpg")
newImage = GenerateAscii(image, 4, 10, defaultCallback)
print("\n", newImage)