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
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)
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)))
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: