]> Skullheadx's Git Forge - text-art.git/commitdiff
text art
authorSkullheadx <94652084+Skullheadx@users.noreply.github.com>
Sat, 24 Dec 2022 03:59:19 +0000 (22:59 -0500)
committerSkullheadx <94652084+Skullheadx@users.noreply.github.com>
Sat, 24 Dec 2022 03:59:19 +0000 (22:59 -0500)
12 files changed:
.gitignore
frames/frame0.png [new file with mode: 0644]
frames/frame1.png [new file with mode: 0644]
frames/frame2.jpg [new file with mode: 0644]
main.py
output/output.mp4 [new file with mode: 0644]
setup.py [new file with mode: 0644]
text_art.py [new file with mode: 0644]
text_art_frames/frame0.png [new file with mode: 0644]
text_art_frames/frame1.png [new file with mode: 0644]
text_art_frames/frame2.jpg [new file with mode: 0644]
video.py [new file with mode: 0644]

index e3aa1528f6ee8376e18c26c7e044e1ffe72e6275..28cc5510db7896aad6dc6c92e68bb24a587b7bc7 100644 (file)
@@ -2,3 +2,11 @@
 /shelf/
 /workspace.xml
 .idea/
+frames/
+text_art_frames/
+input/
+output/
+__pycache__/
+*.mp4
+*.jpg
+*.png
diff --git a/frames/frame0.png b/frames/frame0.png
new file mode 100644 (file)
index 0000000..1ab44eb
Binary files /dev/null and b/frames/frame0.png differ
diff --git a/frames/frame1.png b/frames/frame1.png
new file mode 100644 (file)
index 0000000..2594619
Binary files /dev/null and b/frames/frame1.png differ
diff --git a/frames/frame2.jpg b/frames/frame2.jpg
new file mode 100644 (file)
index 0000000..81c750d
Binary files /dev/null and b/frames/frame2.jpg differ
diff --git a/main.py b/main.py
index 6378f528ca2b7f35eb2dea08dafa210f1cdac723..0bfec3c5d65560c19de4b6ae7f4677989d28caeb 100644 (file)
--- a/main.py
+++ b/main.py
@@ -1,6 +1,21 @@
-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()
diff --git a/output/output.mp4 b/output/output.mp4
new file mode 100644 (file)
index 0000000..da8e2bf
Binary files /dev/null and b/output/output.mp4 differ
diff --git a/setup.py b/setup.py
new file mode 100644 (file)
index 0000000..8b90d52
--- /dev/null
+++ b/setup.py
@@ -0,0 +1,20 @@
+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
diff --git a/text_art.py b/text_art.py
new file mode 100644 (file)
index 0000000..68dcc47
--- /dev/null
@@ -0,0 +1,47 @@
+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
diff --git a/text_art_frames/frame0.png b/text_art_frames/frame0.png
new file mode 100644 (file)
index 0000000..1ba5655
Binary files /dev/null and b/text_art_frames/frame0.png differ
diff --git a/text_art_frames/frame1.png b/text_art_frames/frame1.png
new file mode 100644 (file)
index 0000000..b62c123
Binary files /dev/null and b/text_art_frames/frame1.png differ
diff --git a/text_art_frames/frame2.jpg b/text_art_frames/frame2.jpg
new file mode 100644 (file)
index 0000000..95cf9b0
Binary files /dev/null and b/text_art_frames/frame2.jpg differ
diff --git a/video.py b/video.py
new file mode 100644 (file)
index 0000000..780626e
--- /dev/null
+++ b/video.py
@@ -0,0 +1,41 @@
+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