forked from Vaarun-C/ASCII-Camera
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASCII.py
More file actions
86 lines (65 loc) · 2.17 KB
/
ASCII.py
File metadata and controls
86 lines (65 loc) · 2.17 KB
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
import cv2 as cv
import math
import os
import sys
import numpy as np
from PIL import Image
from io import BytesIO
os.system("clear")
pixel_map = r"$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/|()1{}[]?-_+~<>i!lI;:,\"^`'. "
pixel_hash = []
cap = None
screen_shot = None
def map_color_value(old_value):
return math.floor(((old_value - 0) / (255 - 0)) * (len(pixel_map) - 1 - 0) + 0)
for i in range(256):
pixel_hash.append(pixel_map[map_color_value(i)])
# Define keybindings and menu instructions
menu = """
=== ASCII Art Camera ===
Instructions:
- Press W to start the webcam.
- Press Q to quit the webcam.
- Press S to take a screenshot.
- Press V to display the last screenshot.
Press Enter to start...
"""
# Print the menu and wait for user input
print(menu)
input()
os.system("clear") # Clear the screen after user input
while True:
key = input("Press a key: ")
if key == 'Q' or key == 'q':
if cap is not None:
cap.release()
cv.destroyAllWindows()
break
if key == 'W' or key == 'w':
cap = cv.VideoCapture(0)
cap.set(cv.CAP_PROP_FRAME_WIDTH, 35)
cap.set(cv.CAP_PROP_FRAME_HEIGHT, 20)
for i in range(10):
ret, image = cap.read()
if image is None:
break
image_gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
for i, row in enumerate(image_gray):
for j, column in enumerate(row):
r, g, b = image[i][j]
print(f"\x1b[38;2;{r};{g};{b}m{pixel_hash[column]}\x1b[0m", end='')
print()
sys.stdout.write(f"\033[{0};{0}H")
sys.stdout.flush()
if key == 'S' or key == 's':
if cap is not None:
ret, image = cap.read()
if image is not None:
screen_shot = np.copy(image)
print("Screenshot taken.")
if key == 'V' or key == 'v':
if screen_shot is not None:
img = Image.fromarray(cv.cvtColor(screen_shot, cv.COLOR_BGR2RGB))
img.show()
else:
print("No screenshot available.")