From 3bbe524c2c240b26585385f70b820aee303b4682 Mon Sep 17 00:00:00 2001 From: vatsal-afk Date: Wed, 16 Oct 2024 10:56:34 +0530 Subject: [PATCH 1/3] add asciiator --- src/yodapa/plugins/asciiator.py | 61 +++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/yodapa/plugins/asciiator.py diff --git a/src/yodapa/plugins/asciiator.py b/src/yodapa/plugins/asciiator.py new file mode 100644 index 0000000..080a529 --- /dev/null +++ b/src/yodapa/plugins/asciiator.py @@ -0,0 +1,61 @@ +from PIL import Image +import os +import typer + +app = typer.Typer() + +ASCII_CHARS = ["#", "?", "%", ".", "S", "+", ".", "*", ":", ",", "@"] + +def handle_image_conversion(image_filepath): + image = None + try: + image = Image.open(image_filepath) + except Exception as e: + print(f"Unable to open image file {image_filepath}.") + print(e) + return + + new_width = 100 + range_width = 25 + (original_width, original_height) = image.size + aspect_ratio = original_height / float(original_width) + new_height = int(aspect_ratio * new_width) + + # Resize and convert the image to grayscale + image = image.resize((new_width, new_height)).convert("L") + + # Map pixel values to ASCII characters + pixels_to_chars = "".join( + [ + ASCII_CHARS[pixel_value // range_width] + for pixel_value in list(image.getdata()) + ] + ) + + # Create a list of strings for each row of ASCII characters + image_ascii = [ + pixels_to_chars[index: index + new_width] + for index in range(0, len(pixels_to_chars), new_width) + ] + + return "\n".join(image_ascii) + +@app.command() +def process(input_data): + image_file_path = input_data + data = handle_image_conversion(image_file_path) + + if data: + # Get the directory and original file name without extension + directory = os.path.dirname(image_file_path) + base_name = os.path.splitext(os.path.basename(image_file_path))[0] + + # Define the output file path + output_file_path = os.path.join(directory, f"{base_name}_ascii.txt") + + # Save the ASCII art to a text file + with open(output_file_path, 'w') as f: + f.write(data) + + print(f"ASCII art saved to {output_file_path}") + From 2ec97dde3c9488bdc4d460aa594dad4c58bc9605 Mon Sep 17 00:00:00 2001 From: vatsal-afk Date: Wed, 16 Oct 2024 23:05:12 +0530 Subject: [PATCH 2/3] add asciiator feature --- src/yodapa/plugins/asciiator.py | 45 ++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/src/yodapa/plugins/asciiator.py b/src/yodapa/plugins/asciiator.py index 080a529..23ab6cf 100644 --- a/src/yodapa/plugins/asciiator.py +++ b/src/yodapa/plugins/asciiator.py @@ -2,7 +2,16 @@ import os import typer -app = typer.Typer() +app = typer.Typer(help=""" + Asciiator plugin. Convert an image to ASCII art. + + Example: + + $ yoda asciiator process "{path to image file}" -s // save output locally + $ yoda asciiator process "{path to image file}" -t // display output on terminal + $ yoda asciiator process "{path to image file}" -s -t // both the actions + + """) ASCII_CHARS = ["#", "?", "%", ".", "S", "+", ".", "*", ":", ",", "@"] @@ -16,23 +25,22 @@ def handle_image_conversion(image_filepath): return new_width = 100 - range_width = 25 (original_width, original_height) = image.size aspect_ratio = original_height / float(original_width) new_height = int(aspect_ratio * new_width) - # Resize and convert the image to grayscale image = image.resize((new_width, new_height)).convert("L") - # Map pixel values to ASCII characters + # Adjust the range width to scale pixel values correctly + range_width = 256 // len(ASCII_CHARS) + pixels_to_chars = "".join( [ - ASCII_CHARS[pixel_value // range_width] + ASCII_CHARS[min(pixel_value // range_width, len(ASCII_CHARS) - 1)] for pixel_value in list(image.getdata()) ] ) - # Create a list of strings for each row of ASCII characters image_ascii = [ pixels_to_chars[index: index + new_width] for index in range(0, len(pixels_to_chars), new_width) @@ -41,21 +49,34 @@ def handle_image_conversion(image_filepath): return "\n".join(image_ascii) @app.command() -def process(input_data): +def process(input_data: str, save: bool = typer.Option(False, "-s", help="Save output to a file"), display: bool = typer.Option(False, "-t", help="Display output in terminal")): + """ + Process an image and convert it to ASCII. + + Args: + - input_data: Path to the image file. + - save: Save the ASCII output to a text file. + - display: Display the ASCII output in the terminal. + """ image_file_path = input_data data = handle_image_conversion(image_file_path) - if data: - # Get the directory and original file name without extension + if not data: + print("Error converting image.") + return + + if save: directory = os.path.dirname(image_file_path) base_name = os.path.splitext(os.path.basename(image_file_path))[0] - - # Define the output file path output_file_path = os.path.join(directory, f"{base_name}_ascii.txt") - # Save the ASCII art to a text file with open(output_file_path, 'w') as f: f.write(data) print(f"ASCII art saved to {output_file_path}") + + if display: + print(data) +if __name__ == "__main__": + app() From a50d77983389e4ed9be354d4da79f2796aa2391d Mon Sep 17 00:00:00 2001 From: vatsal-afk Date: Sat, 19 Oct 2024 00:53:33 +0530 Subject: [PATCH 3/3] update asciiator script --- src/yodapa/plugins/asciiator.py | 70 ++++++++++++++++++++------------- src/yodapa/plugins/weather.py | 9 ++++- 2 files changed, 51 insertions(+), 28 deletions(-) diff --git a/src/yodapa/plugins/asciiator.py b/src/yodapa/plugins/asciiator.py index 23ab6cf..6dee60f 100644 --- a/src/yodapa/plugins/asciiator.py +++ b/src/yodapa/plugins/asciiator.py @@ -1,16 +1,16 @@ from PIL import Image import os import typer +from rich import print app = typer.Typer(help=""" Asciiator plugin. Convert an image to ASCII art. Example: - $ yoda asciiator process "{path to image file}" -s // save output locally - $ yoda asciiator process "{path to image file}" -t // display output on terminal - $ yoda asciiator process "{path to image file}" -s -t // both the actions - + $ yoda asciiator file "{path to image file}" // save output locally + $ yoda asciiator show "{path to image file}" // display output on terminal + $ yoda asciiator both "{path to image file}" // both the actions """) ASCII_CHARS = ["#", "?", "%", ".", "S", "+", ".", "*", ":", ",", "@"] @@ -22,7 +22,7 @@ def handle_image_conversion(image_filepath): except Exception as e: print(f"Unable to open image file {image_filepath}.") print(e) - return + return None new_width = 100 (original_width, original_height) = image.size @@ -31,7 +31,6 @@ def handle_image_conversion(image_filepath): image = image.resize((new_width, new_height)).convert("L") - # Adjust the range width to scale pixel values correctly range_width = 256 // len(ASCII_CHARS) pixels_to_chars = "".join( @@ -48,35 +47,52 @@ def handle_image_conversion(image_filepath): return "\n".join(image_ascii) +def save_ascii_art(image_ascii, image_file_path): + directory = os.path.dirname(image_file_path) + base_name = os.path.splitext(os.path.basename(image_file_path))[0] + output_file_path = os.path.join(directory, f"{base_name}_ascii.txt") + + with open(output_file_path, 'w') as f: + f.write(image_ascii) + + print(f"ASCII art saved to {output_file_path}") + @app.command() -def process(input_data: str, save: bool = typer.Option(False, "-s", help="Save output to a file"), display: bool = typer.Option(False, "-t", help="Display output in terminal")): +def file(image_path: str): """ - Process an image and convert it to ASCII. - - Args: - - input_data: Path to the image file. - - save: Save the ASCII output to a text file. - - display: Display the ASCII output in the terminal. + Save the ASCII art to a text file. """ - image_file_path = input_data - data = handle_image_conversion(image_file_path) + data = handle_image_conversion(image_path) - if not data: + if data: + save_ascii_art(data, image_path) + else: print("Error converting image.") - return + +@app.command() +def show(image_path: str): + """ + Display the ASCII art in the terminal. + """ + data = handle_image_conversion(image_path) - if save: - directory = os.path.dirname(image_file_path) - base_name = os.path.splitext(os.path.basename(image_file_path))[0] - output_file_path = os.path.join(directory, f"{base_name}_ascii.txt") - - with open(output_file_path, 'w') as f: - f.write(data) - - print(f"ASCII art saved to {output_file_path}") + if data: + print(data) + else: + print("Error converting image.") + +@app.command() +def both(image_path: str): + """ + Save the ASCII art to a file and display it in the terminal. + """ + data = handle_image_conversion(image_path) - if display: + if data: + save_ascii_art(data, image_path) print(data) + else: + print("Error converting image.") if __name__ == "__main__": app() diff --git a/src/yodapa/plugins/weather.py b/src/yodapa/plugins/weather.py index 8cbc10c..fbc51da 100644 --- a/src/yodapa/plugins/weather.py +++ b/src/yodapa/plugins/weather.py @@ -2,7 +2,14 @@ import click import typer -app = typer.Typer() +app = typer.Typer(help=""" + weather plugin. Get the weather for a location. + + Example: + + $ yoda weather get_weather "New York" // New York is an example, replace with desired location + + """) @app.command() def get_weather(location):