Skip to content

Commit 3ad828c

Browse files
committed
⚗️ colorful
1 parent 9c7e508 commit 3ad828c

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

src/badapple/frame2str.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import cv2
2+
import numpy as np
3+
from .util import Font
4+
5+
6+
def get_buffer(
7+
fnt: Font, img: np.ndarray,
8+
x: int, y: int, colorful: bool = False,
9+
contrast: bool = False,
10+
) -> str:
11+
buffer = ''
12+
13+
if colorful:
14+
img = cv2.resize(img, (x, y//2))
15+
# np.ndarray(shape=(y, x, 3), dtype=np.uint8)
16+
# x*y subpixel = x*y pixel = x*y char
17+
for j in range(y//2):
18+
for k in range(x):
19+
buffer += '\x1b[48;2;%d;%d;%dm ' % tuple(img[j, k][-1::-1])
20+
buffer += '\x1b[0m\n'
21+
return buffer
22+
23+
img = cv2.resize(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), (x, y))
24+
# np.ndarray(shape=(y, x), dtype=np.uint8)
25+
# x*y subpixel = x*y/2 pixel = x*y/2 char
26+
27+
if contrast:
28+
max_pixel = np.max(img)
29+
min_pixel = np.min(img)
30+
if max_pixel == min_pixel:
31+
if max_pixel >= 128:
32+
img = np.full((y, x), 0xff, dtype=np.uint8)
33+
else:
34+
img = np.zeros((y, x), dtype=np.uint8)
35+
else:
36+
max_min = max_pixel - min_pixel
37+
img = (((img.astype(dtype=np.uint16) - min_pixel) * 0xff +
38+
max_min // 2) // max_min).astype(dtype=np.uint8)
39+
40+
for j in range(y//2):
41+
for k in range(x):
42+
buffer += fnt.get(img[j*2, k], img[j*2+1, k])
43+
buffer += '\n'
44+
45+
return buffer

0 commit comments

Comments
 (0)