-def main():
- ...
+import os.path
+from setup import *
+from video import extract_frames, to_video, get_frames
+from text_art import text_art
-if __name__ == "__main__":
+
+def main() -> None:
+ # file_name = input("Enter name of file: ")
+ file_name = input_filename
+ # extract_frames(file_name)
+
+ frames = get_frames()
+
+ output_frames = [text_art(image) for image in frames]
+
+ # to_video(output_frames)
+
+
+if __name__ == '__main__':
main()
--- /dev/null
+import cv2
+import os
+import numpy as np
+import math
+
+
+frames_path = "C:/Users/admon/Documents/GitHub/text-art/frames/"
+text_art_frames_path = "C:/Users/admon/Documents/GitHub/text-art/text_art_frames/"
+output_filename = "output/output.mp4"
+input_filename = "input/donut.mp4"
+
+BLACK = (0,0,0)
+WHITE = (255,255,255)
+
+
+def get_number(filename:str):
+ return int(filename[5:-4])
+def find_index(num, min_num, max_num, min_range, max_range):
+ n = num / (max_num - min_num + 1)
+ return (max_range - min_range + 1) * n + min_range
\ No newline at end of file
--- /dev/null
+from setup import *
+
+def text_art(path): # 600, 800
+ image = cv2.imread(os.path.join(frames_path,path))
+ """
+alpha 1 beta 0 --> no change
+0 < alpha < 1 --> lower contrast
+alpha > 1 --> higher contrast
+-127 < beta < +127 --> good range for brightness values
+ """
+ alpha = 2
+ beta = 127
+ image = alpha*image + beta
+ scale = 3
+ density = "Ñ@#W$9876543210?!abc;:+=-,._ "
+ # density = ' .:-i|=+%O#@'
+ # density = density[::-1]
+ density_length = len(density)
+
+ rows,cols,_ = image.shape
+ output = []
+ for i in range(0,rows,scale):
+ line = ""
+ for j in range(0,cols, scale):
+ avg = np.average(image[i,j])
+ avg = min(avg * 2, 255)
+ density_index = math.floor(find_index(avg, 0, 255, 0, density_length - 1))
+ line += density[density_index]
+ output.append(line)
+ cv2.rectangle(image, (0,0), (cols,rows), BLACK, -1)
+ # cv2.imwrite(os.path.join(text_art_frames_path, path), image)
+
+ font = cv2.FONT_HERSHEY_COMPLEX_SMALL
+ font_size = 0.9
+ thick = 1
+ font_color = WHITE
+ for i,text in enumerate(output[::-1]):
+ (text_width, text_height) = cv2.getTextSize(text, font, font_size, thick)[0]
+ text_height += i * scale
+ mask = np.zeros((text_height, text_width), dtype=np.uint8)
+ mask = cv2.putText(mask,text,(10,scale),font,font_size,font_color,thick,cv2.LINE_AA)
+ mask = cv2.resize(mask, (image.shape[1], text_height))
+ mask = cv2.merge((mask, mask, mask))
+ if image[-text_height:, :, :].size == mask.size:
+ image[-text_height:, :, :] = cv2.bitwise_or(image[-text_height:, :, :], mask)
+ cv2.imwrite(os.path.join(text_art_frames_path, path), image)
+ return path
--- /dev/null
+from setup import *
+
+
+def extract_frames(path: str) -> None:
+ # Clear old frames
+ for root, dirs, files in os.walk(frames_path):
+ for file in files:
+ os.remove(os.path.join(root, file))
+
+ # Get input video
+ video = cv2.VideoCapture(path)
+
+ current_frame = 0
+ while True:
+ ret, image = video.read()
+ # Save each frame
+ if ret != 1:
+ break
+ cv2.imwrite(os.path.join(frames_path, f"frame{current_frame}.png"), image)
+ current_frame += 1
+ video.release()
+ cv2.destroyAllWindows()
+
+
+def to_video(frames: list) -> None:
+ height, width, layers = cv2.imread(os.path.join(frames_path,frames[0])).shape
+ fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
+ writer = cv2.VideoWriter(output_filename, fourcc, 33, (width, height))
+ for frame in frames:
+ writer.write(cv2.imread(os.path.join(frames_path,frame)))
+
+ cv2.destroyAllWindows()
+ writer.release()
+
+def get_frames():
+ frames = []
+ for root, dirs, files in os.walk(frames_path):
+ for file in files:
+ frames.append(file)
+ frames.sort(key=get_number)
+ return frames