From 13cc3fe5c296e4ec11f9b0b856ca499fef30e364 Mon Sep 17 00:00:00 2001 From: VEERT00X Date: Tue, 22 Aug 2023 23:02:38 +0200 Subject: [PATCH] v2.0.0 --- .vscode/settings.json | 6 +++ _main_.py | 90 ++++++++++++++++++++++--------------------- requirements.txt | 6 +-- 3 files changed, 56 insertions(+), 46 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..ba77eac --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "[python]": { + "editor.defaultFormatter": "ms-python.python" + }, + "python.formatting.provider": "none" +} \ No newline at end of file diff --git a/_main_.py b/_main_.py index 439fd72..8a3daf8 100644 --- a/_main_.py +++ b/_main_.py @@ -1,5 +1,3 @@ -# Importing the libraries - import os import sys import cv2 @@ -7,49 +5,55 @@ # Configuration -def get_file_path(): - try: - file_path = sys.argv[1] - except IndexError: - file_path = input("Enter file path: ") - print("File path loaded: " + file_path) - return file_path - -FILE_PATH = get_file_path() -ASCII = ["@", "#", "S", "%", "?", "*", "+", ";", ":", ",", ".","!","^","&","~","-","_"] +ASCII = ["@", "#", "S", "%", "?", "*", "+", ";", ":", ",", ".", "!", "^", "&", "~", "-", "_"] # Functions -def resized_gray_image(image ,new_width=70): - width,height = image.size - aspect_ratio = height/width - new_height = int(aspect_ratio * new_width) - resized_gray_image = image.resize((new_width,new_height)).convert('L') - return resized_gray_image - -def pixEl(image): - pixels = image.getdata() - characters = "".join([ASCII[pixel//25] for pixel in pixels]) - return characters - -def generate(image,new_width=70): - new_image_data = pixEl(resized_gray_image(image)) - - total_pixels = len(new_image_data) - - ascii_image = "\n".join([new_image_data[index:(index+new_width)] for index in range(0, total_pixels, new_width)]) - - sys.stdout.write(ascii_image) - os.system('cls' if os.name == 'nt' else 'clear') -cap = cv2.VideoCapture(FILE_PATH) - -# Main - -if __name__ == "__main__": +def resized_gray_image(image, new_width=70): + width, height = image.size + aspect_ratio = height / width + new_height = int(aspect_ratio * new_width) + resized_gray_image = image.resize((new_width, new_height)).convert('L') + return resized_gray_image + +def pixelate(image, new_width=70): + new_image_data = resized_gray_image(image, new_width).getdata() + characters = [ASCII[pixel // 25] for pixel in new_image_data] + return ''.join(characters) + +def generate(image, new_width=70): + ascii_image = pixelate(image, new_width) + total_pixels = len(ascii_image) + rows = [ascii_image[index:(index + new_width)] for index in range(0, total_pixels, new_width)] + return '\n'.join(rows) + +def main(): + # Handle command-line arguments + display_frame = "--no-frame" not in sys.argv + file_path = sys.argv[1] if len(sys.argv) > 1 else input("Enter file path: ") + cap = cv2.VideoCapture(file_path) + while True: + ret, frame = cap.read() + + if not ret: + break + + frame_image = Image.fromarray(frame) + ascii_output = generate(frame_image) + + os.system('cls' if os.name == 'nt' else 'clear') + + if display_frame: + cv2.imshow("Original Frame", frame) + + print(ascii_output) + + if cv2.waitKey(1) & 0xFF == ord('q'): + break + + cap.release() + cv2.destroyAllWindows() - ret,frame = cap.read() - cv2.imshow("frame",frame) - generate(Image.fromarray(frame)) - cv2.waitKey(1) - +if __name__ == "__main__": + main() diff --git a/requirements.txt b/requirements.txt index 6d668cc..78fe29d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ -numpy==1.21.6 -opencv-contrib-python==4.4.0.42 -Pillow==9.3.0 \ No newline at end of file +numpy +opencv-contrib-python +Pillow \ No newline at end of file