]> Skullheadx's Git Forge - text-art.git/commitdiff
clean up code
authorSkullheadx <704277@pdsb.net>
Sat, 24 Dec 2022 16:59:48 +0000 (11:59 -0500)
committerSkullheadx <704277@pdsb.net>
Sat, 24 Dec 2022 16:59:48 +0000 (11:59 -0500)
.gitignore
main.py
setup.py
text_art.py
video.py

index 28cc5510db7896aad6dc6c92e68bb24a587b7bc7..3497a0589215867d088647a3fe1bd5f61eb13a31 100644 (file)
@@ -2,11 +2,8 @@
 /shelf/
 /workspace.xml
 .idea/
-frames/
-text_art_frames/
-input/
-output/
 __pycache__/
 *.mp4
 *.jpg
 *.png
+*.txt
diff --git a/main.py b/main.py
index a037e0d60733feab353c26eb41d7a80e4f101eef..af28c271bdc821379e90e0396282feca871c5a99 100644 (file)
--- a/main.py
+++ b/main.py
@@ -1,20 +1,22 @@
-import os.path
-
 from setup import *
+from text_art import text_art_video, text_art_image
 from video import extract_frames, to_video, get_frames
-from text_art import text_art
 
 
-def main() -> None:
-    # file_name = input("Enter name of file: ")
+def main(video=True) -> None:
     file_name = input_filename
     extract_frames(file_name)
+
     frames = get_frames(frames_path)
-    for image in frames:
-        text_art(image)
+    if video:
+        for path in frames:
+            text_art_video(path)
 
-    to_video(text_art_frames_path,get_frames(text_art_frames_path))
+        to_video(text_art_frames_path, get_frames(text_art_frames_path))
+    else:
+        for path in frames:
+            text_art_image(path)
 
 
 if __name__ == '__main__':
-    main()
+    main(video=is_video)
index cf7e86b685cfe768ec463c5cd48ee039e0d2941d..d1c192a96e3812d4665b55fddc485b3427a06868 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -4,17 +4,39 @@ import numpy as np
 import math
 from moviepy.editor import *
 
-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/Rick Astley - Never Gonna Give You Up (Official Music Video).mp4"
+frames_path = "frames/"
+text_art_frames_path = "text_art_frames/"
+text_art_txts_path = "text_art_txts/"
+input_path = "input/"
+output_path = "output/"
 
-BLACK = (0,0,0)
-WHITE = (255,255,255)
+is_video = True
 
+filename = "3x14x9 4x4 Piston Door.mp4"
 
-def get_number(filename:str):
+input_filename = os.path.join(input_path, filename)
+output_filename = os.path.join(output_path, filename)
+output_filename_no_audio = os.path.join(output_path, "no audio" + filename)
+
+
+BLACK = (0, 0, 0)
+WHITE = (255, 255, 255)
+
+# density = "Ñ@#W$9876543210?!abc;:+=-,._       "
+# density = density[::-1]
+density = '     .:-i|=+%O#@'
+density_length = len(density)
+
+font = cv2.FONT_HERSHEY_COMPLEX_SMALL
+scale = 10
+font_size = 1
+thick = 1
+
+
+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
+    return (max_range - min_range + 1) * n + min_range
index 7c0d78e6fd1f04f804423f71578883fa2ca593a9..8cb26e1a5fccc686250c3486f394aae52076cbad 100644 (file)
@@ -1,74 +1,40 @@
 from setup import *
 
 
-def text_art(path):  # 600, 800
-    image = cv2.imread(os.path.join(frames_path, path), 0)
-    """
-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
-
-    width, height = np.array(image).shape
-    new_image = np.zeros((width, height), np.uint8)
-    # print(width, height, new_image.shape)
+def add_text(grayscale: int, x: int, y: int, new_image: np.ndarray) -> None:
+    density_index = math.floor(find_index(grayscale, 0, 255, 0, density_length - 1))
+    text = density[density_index]
+    font_color = (255, 255, 255, int(grayscale))
 
-    # density = "Ñ@#W$9876543210?!abc;:+=-,._       "
-    density = '     .:-i|=+%O#@'
-    # density = density[::-1]
-    density_length = len(density)
+    cv2.putText(new_image, text, (x, y), font, font_size, font_color, thick, cv2.LINE_AA)
 
-    font = cv2.FONT_HERSHEY_COMPLEX_SMALL
 
-    """
-    density = '     .:-i|=+%O#@'
-    scale = 10
-    font_size = 1
-    thick = 1
-    
-    density = '     .:-i|=+%O#@'
-    scale = 20
-    font_size =1
-    thick = 3
-    """
-
-    scale = 10
-    font_size = 1
-    thick = 1
-    # cv2.rectangle(image, (0,0), (height,width), BLACK, -1)
-    # cv2.imwrite(os.path.join(text_art_frames_path, path), image)
+def text_art_video(path: str) -> str:
+    image = cv2.imread(os.path.join(frames_path, path), 0)
+    width, height = np.array(image).shape
+    new_image = np.zeros((width, height), np.uint8)
 
-    # line = ""
     for index, i in np.ndenumerate(image):
         y, x = index
         if (x % scale) or (y % scale):
             continue
-        # if (x+y) % 25 != 0:
-        #     continue
-        # for k in np.nditer(i):
+        add_text(image[index], x, y, new_image)
 
-        density_index = math.floor(find_index(image[index], 0, 255, 0, density_length - 1))
-        text = density[density_index]
-        font_color = (255, 255, 255, int(image[index]))  #
-        # line += text
+    cv2.imwrite(os.path.join(text_art_frames_path, path), new_image)
+    return path
 
-        # (text_width, text_height) = cv2.getTextSize(text, font, font_size, thick)[0]
-        # text_height += y * scale
-        cv2.putText(new_image, text, (x, y), font, font_size, font_color, thick, cv2.LINE_AA)
 
-        # if y == 0 and x > 0:
-        #     line += '\n'
-        #     pass
+def text_art_image(path: str) -> None:
+    image = cv2.imread(os.path.join(frames_path, path), 0)  # grayscale image
+    line = ""
+    for index, i in np.ndenumerate(image):
+        y, x = index
+        if (x % scale) or (y % scale):
+            continue
 
-    # print(line)
+        if y == 0 and x > 0:
+            line += '\n'
 
-    # image[-text_height:,:] = cv2.bitwise_or(image[-text_height:,:], mask)
-    #
-    cv2.imwrite(os.path.join(text_art_frames_path, path), new_image)
-    # cv2.imshow('Grayscale Image', new_image)
-    # cv2.waitKey(0)
-    return path
+    # Write to txt file
+    with open(os.path.join(text_art_txts_path,f"frame{get_number(path)}.txt"), "w") as f:
+        f.write(line)
index f69f8c34a1a211ab4fb04bac6fbd7b1c3f0b199a..e3950f582555e4ab27f4cf689c63496612fc189f 100644 (file)
--- a/video.py
+++ b/video.py
@@ -23,9 +23,16 @@ def extract_frames(path: str) -> None:
 
 
 def to_video(path: str, frames: list) -> None:
+    if len(frames) == 0:
+        raise Exception("No frames were found.")
+    video = cv2.VideoCapture(input_filename)
+    frame_rate = video.get(cv2.CAP_PROP_FPS)
+    video.release()
+    cv2.destroyAllWindows()
+
     height, width, layers = cv2.imread(os.path.join(path, frames[0])).shape
     fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
-    writer = cv2.VideoWriter(output_filename, fourcc, 25, (width, height))
+    writer = cv2.VideoWriter(output_filename_no_audio, fourcc, frame_rate, (width, height))
     for frame in frames:
         writer.write(cv2.imread(os.path.join(path, frame)))
 
@@ -33,12 +40,13 @@ def to_video(path: str, frames: list) -> None:
     writer.release()
 
     # adding audio
-    clip = VideoFileClip(output_filename)
+    clip = VideoFileClip(output_filename_no_audio)
     audio_clip = AudioFileClip(input_filename)
     clip.audio = audio_clip
-    clip.write_videofile("output/output1.mp4")
+    clip.write_videofile(output_filename)
+
+
 def get_frames(path):
-    # return ["frame2.png"]
     frames = []
     for root, dirs, files in os.walk(path):
         for file in files: