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..52775fb 100644 --- a/src/loadimg/loadimg.py +++ b/src/loadimg/loadimg.py @@ -1,49 +1,43 @@ -import argparse -import sys +import typer +import shutil +from .utils import load_img, resize_image -try: - from .utils import load_img -except ImportError: - 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( + img: 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)", - ) + 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. + """ + 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") - args = parser.parse_args() - if not hasattr(args, "input"): - parser.print_help() - exit(1) - try: - result = load_img( - args.input, output_type=args.output_type, input_type=args.input_type - ) - if isinstance(result, str): - print(result) - else: - print(f"Image converted successfully to {args.output_type} format") - except Exception as e: - print(f"Error: {e}") - return 1 - - return 0 +def main(): + typer.run(loadimg_cli) if __name__ == "__main__": - sys.exit(main()) + 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: