From 1925f7422db80540ddd4cd7e57e9c76e1f1cb8fc Mon Sep 17 00:00:00 2001 From: not-lain Date: Wed, 18 Feb 2026 06:18:27 +0100 Subject: [PATCH 1/2] typer --- setup.py | 2 +- src/loadimg/loadimg.py | 57 +++++++++++++++++------------------------- 2 files changed, 24 insertions(+), 35 deletions(-) diff --git a/setup.py b/setup.py index dd46db6..1bfa4a7 100644 --- a/setup.py +++ b/setup.py @@ -43,7 +43,7 @@ def get_version() -> str: extras_require=extras, include_package_data=True, classifiers=["Topic :: Utilities", "Programming Language :: Python :: 3.9"], - requires=["setuptools", "wheel", "typing", "pillow", "numpy", "requests", "tqdm"], + requires=["setuptools", "wheel", "typing", "pillow", "numpy", "requests", "tqdm", "typer"], entry_points={ "console_scripts": [ "loadimg=loadimg.loadimg:main", diff --git a/src/loadimg/loadimg.py b/src/loadimg/loadimg.py index 57d592a..0c8cb71 100644 --- a/src/loadimg/loadimg.py +++ b/src/loadimg/loadimg.py @@ -1,49 +1,38 @@ -import argparse -import sys +import typer -try: - from .utils import load_img -except ImportError: - from utils import load_img +from .utils import load_img -def main(): - parser = argparse.ArgumentParser( - prog="loadimg", description="Load and convert images from various sources" - ) - parser.add_argument("input", help="Input image (file path, URL, or base64 string)") - parser.add_argument( +def loadimg_cli( + input: str = typer.Argument(..., help="Input image (file path, URL, or base64 string)"), + output_type: str = typer.Option( + "ansi", "--output-type", - choices=["pil", "numpy", "str", "base64", "ascii", "ansi", "url"], - default="ansi", - help="Output format (default: ansi)", - ) - parser.add_argument( + help="Output format (pil, numpy, str, base64, ascii, ansi, url)", + ), + input_type: str = typer.Option( + "auto", "--input-type", - choices=["auto", "base64", "file", "url", "numpy", "pil"], - default="auto", - help="Input type (default: auto)", - ) - - args = parser.parse_args() - if not hasattr(args, "input"): - parser.print_help() - exit(1) - + help="Input type (auto, base64, file, url, numpy, pil)", + ), +): + """ + Load and convert images from various sources. + """ try: - result = load_img( - args.input, output_type=args.output_type, input_type=args.input_type - ) + result = load_img(input, output_type=output_type, input_type=input_type) if isinstance(result, str): print(result) else: - print(f"Image converted successfully to {args.output_type} format") + print(f"Image converted successfully to {output_type} format") except Exception as e: print(f"Error: {e}") - return 1 + raise typer.Exit(code=1) - return 0 + +def main(): + typer.run(loadimg_cli) if __name__ == "__main__": - sys.exit(main()) + main() From cc114d7bd0f0fc06933d9b643d5d738686bac522 Mon Sep 17 00:00:00 2001 From: not-lain Date: Wed, 18 Feb 2026 06:44:37 +0100 Subject: [PATCH 2/2] add fit parameter --- src/loadimg/loadimg.py | 29 +++++++++++++++++------------ src/loadimg/utils.py | 19 +++++++++++++++++++ 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/src/loadimg/loadimg.py b/src/loadimg/loadimg.py index 0c8cb71..52775fb 100644 --- a/src/loadimg/loadimg.py +++ b/src/loadimg/loadimg.py @@ -1,10 +1,10 @@ import typer - -from .utils import load_img +import shutil +from .utils import load_img, resize_image def loadimg_cli( - input: str = typer.Argument(..., help="Input image (file path, URL, or base64 string)"), + img: str = typer.Argument(..., help="Input image (file path, URL, or base64 string)"), output_type: str = typer.Option( "ansi", "--output-type", @@ -15,19 +15,24 @@ def loadimg_cli( "--input-type", help="Input type (auto, base64, file, url, numpy, pil)", ), + fit: bool = typer.Option( + True, "--fit", "-f", help="Fit the image to the terminal width" + ), ): """ Load and convert images from various sources. """ - try: - result = load_img(input, output_type=output_type, input_type=input_type) - if isinstance(result, str): - print(result) - else: - print(f"Image converted successfully to {output_type} format") - except Exception as e: - print(f"Error: {e}") - raise typer.Exit(code=1) + if fit: + max_width = shutil.get_terminal_size().columns + result = load_img(img=img, output_type="pil", input_type=input_type) + result = resize_image(result, max_width) + result = load_img(result, output_type=output_type) + else : + result = load_img(img,output_type=output_type,input_type=input_type) + if isinstance(result, str): + print(result) + else: + print(f"Image converted successfully to {output_type} format") def main(): diff --git a/src/loadimg/utils.py b/src/loadimg/utils.py index 12bd492..fd64f3d 100644 --- a/src/loadimg/utils.py +++ b/src/loadimg/utils.py @@ -272,6 +272,25 @@ def isBase64(sb): return False +def resize_image(image: Image.Image, max_width: int) -> Image.Image: + """Resizes a Pillow image to a maximum width maintaining the aspect ratio. + + Args: + image: The PIL Image to resize. + max_width: The maximum width allowed. + + Returns: + The resized PIL Image. + """ + width, height = image.size + if width <= max_width: + return image + + aspect_ratio = height / width + new_height = int(aspect_ratio * max_width) + return image.resize((max_width, new_height)) + + def image_to_ascii( image: Image.Image, new_width: int = 100, ascii_chars: str = "@%#*+=-:. " ) -> str: