-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
49 lines (40 loc) · 1.31 KB
/
main.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
import os, cv2, sys
from PIL import Image, ImageEnhance
FACTOR = 1.5 # Contrast Factor
GSCALE = "@#+~-` " # 7 Levels of depth, 37 per character
PATH = 'frame.jpg'
def main():
video = cv2.VideoCapture(0)
video.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
video.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
while True:
ret, frame = video.read()
frame = cv2.flip(frame, 1)
cv2.imwrite(PATH, frame)
print(ConvertToASCII())
if sys.platform == 'win32':
os.system('cls')
else:
os.system('clear')
def ConvertToASCII():
# Resize and Convert to greyscale image
img = Image.open(PATH)
img = img.resize((100, 40))
img = img.convert('L')
# Increase image contrast
contrast = ImageEnhance.Contrast(img)
img = contrast.enhance(FACTOR)
# Get image data according greyscale
pixels = img.getdata()
new_pixels = [GSCALE[pixel // 37] for pixel in pixels]
new_pixels = "".join(new_pixels)
# Assign characters to the new pixel values
new_pixels_count = len(new_pixels)
ascii_image = [
new_pixels[index: index + 100]
for index in range(0, new_pixels_count, 100)
]
ascii_image = "\n".join(ascii_image)
# Return the ascii image
return ascii_image
main()