-
Notifications
You must be signed in to change notification settings - Fork 0
/
img2colorized_ascii.py
107 lines (76 loc) · 2.51 KB
/
img2colorized_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
import sys
import cv2
import numpy as np
from PIL import Image
from asciify import do
from util.color_utils import colorizeString, genANSI_TextTrueColorCode, genANSI_BackTrueColorCode
def main():
# img = Image.open('./img2.png')
# printImageASCII(img)
pathToVideo = './vid/watch_dogs_intro.mp4'
vidcap = cv2.VideoCapture(pathToVideo)
success, frame = vidcap.read()
while (success):
# Convert frame to PIL Image
imgPIL = convertFrame(frame)
s = render(imgPIL)
printImageASCII(s)
success, frame = vidcap.read()
vidcap.release()
def convertFrame(frame) -> Image:
img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
imgPIL = Image.fromarray(img)
return imgPIL
def render(img):
"""
Takes a input of PIL Image \n
Returns rendered colorized ASCII art of the input image
"""
# Fixed size
newSize = (100, 56)
img = img.resize(newSize)
# img.show()
rgb = img.convert("RGB")
# print(f'Size = {rgb.size}')
W = rgb.size[0]
H = rgb.size[1]
# [[0 for x in range(cols_count)] for x in range(rows_count)]
imgColors = [[0 for x in range(newSize[0])] for x in range(newSize[1])]
# When accessing:
# imgColors[row][col] <- row = h | col = w
# Save the color of each pixel
for h in range(0, H):
for w in range(0, W):
pixel = rgb.getpixel((w, h))
imgColors[h][w] = pixel
# Converts the frame (Image object) into ASCII art
asciiImg = do(img)
lines = asciiImg.split("\n")
# bgColor = (0, 0, 0)
# backColorCode = genANSI_BackTrueColorCode(bgColor[0], bgColor[1], bgColor[2])
s = ""
for l in range(0, len(lines)):
line = lines[l]
for i in range(0, len(line)):
color = imgColors[l][i]
char = line[i]
colorCode = genANSI_TextTrueColorCode(color[0], color[1], color[2])
c = colorizeString(
char,
colorCode
# backColorCode
)
# print(c, end='')
s += c
# print("")
s += "\n"
return s
def printImageASCII(s: str):
linesAmount = len(s.split("\n")) + 1
print(s)
# Erase the previous output
# From: https://www.daniweb.com/programming/software-development/threads/428136/clear-the-screen-without-os-library
for _ in range(linesAmount):
sys.stdout.write("\x1b[1A\x1b[2K") # move up cursor and delete whole line
if (__name__ == '__main__'):
main()