-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconvert-video.py
47 lines (42 loc) · 1.51 KB
/
convert-video.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
import cv2
from PIL import Image
import numpy as np
import os
def image_to_emoji(image, width):
img = Image.fromarray(image).convert('L')
#resize based on width
aspect_ratio = img.height / img.width
height = int(width * aspect_ratio)
img = img.resize((width, height))
pixels = np.array(img)
#set the black/white threshold to the mean value of the pixels
threshold = np.mean(pixels)
ascii_image = ""
#this is quite a bit faster than the previous double for loop method
emoji_array = np.where(pixels > threshold, "⬜", "⬛")
ascii_image = '\n'.join([''.join(row) for row in emoji_array])
return ascii_image
def video_to_ascii(video_path, output_folder, width):
os.makedirs(output_folder, exist_ok=True)
video = cv2.VideoCapture(video_path)
frame_count = 0
while True:
success, frame = video.read()
if not success: #if frame fails to load/doesn't exist
break
emoji_frame = image_to_emoji(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB), width)
frame_count += 1
with open(os.path.join(output_folder, f"{frame_count}.txt"), "w", encoding="utf-8") as f:
f.write(emoji_frame)
print(frame_count)
video.release()
print("done")
# Usage
video_path = input("Enter video path: ")
try:
width = int(input("Enter desired width (100 is usually best): "))
except:
print("Invalid width, defaulting to 100")
width = 100
output_folder = "./frames"
video_to_ascii(video_path, output_folder, width)