-
Notifications
You must be signed in to change notification settings - Fork 0
/
ascii_webcam_renderer.py
executable file
·47 lines (35 loc) · 1.13 KB
/
ascii_webcam_renderer.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
#!/usr/bin/python3
from PIL import Image
import cv2
import sys
def main():
cam_id = int(sys.argv[1])
if len(sys.argv) == 3:
width = int(sys.argv[2])
height = int(sys.argv[3])
else:
width = 200
height = 200
capture = cv2.VideoCapture(cam_id)
while(True):
ret, cv_image = capture.read()
cv_image = cv2.cvtColor(cv_image, cv2.COLOR_BGR2RGB)
image = Image.fromarray(cv_image)
image.convert(mode="P", matrix=None, dither=Image.FLOYDSTEINBERG, palette=Image.WEB, colors=256)
pixels = image.load()
color = 'MNHQ$OC?7>!;:-. '
color = list(reversed(color))
out_str = ""
for ix in range(height):
if ix % 2 == 0 and ix % 4 == 0:
for jx in range(width):
rgba = pixels[200 + jx, 125 + ix]
rgb = rgba[:3]
out_str += color[int(sum(rgb) / 3.0 / 256.0 * 16.0)]
out_str += '\n'
out_str += '\033[0;0H'
sys.stdout.write(out_str)
sys.stdout.flush()
capture.release()
if __name__ == '__main__':
main()