|
1 | | -import argparse |
2 | | -import sys |
| 1 | +import typer |
| 2 | +import shutil |
| 3 | +from .utils import load_img, resize_image |
3 | 4 |
|
4 | | -try: |
5 | | - from .utils import load_img |
6 | | -except ImportError: |
7 | | - from utils import load_img |
8 | 5 |
|
9 | | - |
10 | | -def main(): |
11 | | - parser = argparse.ArgumentParser( |
12 | | - prog="loadimg", description="Load and convert images from various sources" |
13 | | - ) |
14 | | - parser.add_argument("input", help="Input image (file path, URL, or base64 string)") |
15 | | - parser.add_argument( |
| 6 | +def loadimg_cli( |
| 7 | + img: str = typer.Argument(..., help="Input image (file path, URL, or base64 string)"), |
| 8 | + output_type: str = typer.Option( |
| 9 | + "ansi", |
16 | 10 | "--output-type", |
17 | | - choices=["pil", "numpy", "str", "base64", "ascii", "ansi", "url"], |
18 | | - default="ansi", |
19 | | - help="Output format (default: ansi)", |
20 | | - ) |
21 | | - parser.add_argument( |
| 11 | + help="Output format (pil, numpy, str, base64, ascii, ansi, url)", |
| 12 | + ), |
| 13 | + input_type: str = typer.Option( |
| 14 | + "auto", |
22 | 15 | "--input-type", |
23 | | - choices=["auto", "base64", "file", "url", "numpy", "pil"], |
24 | | - default="auto", |
25 | | - help="Input type (default: auto)", |
26 | | - ) |
| 16 | + help="Input type (auto, base64, file, url, numpy, pil)", |
| 17 | + ), |
| 18 | + fit: bool = typer.Option( |
| 19 | + True, "--fit", "-f", help="Fit the image to the terminal width" |
| 20 | + ), |
| 21 | +): |
| 22 | + """ |
| 23 | + Load and convert images from various sources. |
| 24 | + """ |
| 25 | + if fit: |
| 26 | + max_width = shutil.get_terminal_size().columns |
| 27 | + result = load_img(img=img, output_type="pil", input_type=input_type) |
| 28 | + result = resize_image(result, max_width) |
| 29 | + result = load_img(result, output_type=output_type) |
| 30 | + else : |
| 31 | + result = load_img(img,output_type=output_type,input_type=input_type) |
| 32 | + if isinstance(result, str): |
| 33 | + print(result) |
| 34 | + else: |
| 35 | + print(f"Image converted successfully to {output_type} format") |
27 | 36 |
|
28 | | - args = parser.parse_args() |
29 | | - if not hasattr(args, "input"): |
30 | | - parser.print_help() |
31 | | - exit(1) |
32 | 37 |
|
33 | | - try: |
34 | | - result = load_img( |
35 | | - args.input, output_type=args.output_type, input_type=args.input_type |
36 | | - ) |
37 | | - if isinstance(result, str): |
38 | | - print(result) |
39 | | - else: |
40 | | - print(f"Image converted successfully to {args.output_type} format") |
41 | | - except Exception as e: |
42 | | - print(f"Error: {e}") |
43 | | - return 1 |
44 | | - |
45 | | - return 0 |
| 38 | +def main(): |
| 39 | + typer.run(loadimg_cli) |
46 | 40 |
|
47 | 41 |
|
48 | 42 | if __name__ == "__main__": |
49 | | - sys.exit(main()) |
| 43 | + main() |
0 commit comments