Skip to content

Commit ab2d822

Browse files
authored
Merge pull request #27 from not-lain/switch-to-typer
switch to typer
2 parents 995c38e + cc114d7 commit ab2d822

File tree

3 files changed

+54
-41
lines changed

3 files changed

+54
-41
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def get_version() -> str:
4343
extras_require=extras,
4444
include_package_data=True,
4545
classifiers=["Topic :: Utilities", "Programming Language :: Python :: 3.9"],
46-
requires=["setuptools", "wheel", "typing", "pillow", "numpy", "requests", "tqdm"],
46+
requires=["setuptools", "wheel", "typing", "pillow", "numpy", "requests", "tqdm", "typer"],
4747
entry_points={
4848
"console_scripts": [
4949
"loadimg=loadimg.loadimg:main",

src/loadimg/loadimg.py

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,43 @@
1-
import argparse
2-
import sys
1+
import typer
2+
import shutil
3+
from .utils import load_img, resize_image
34

4-
try:
5-
from .utils import load_img
6-
except ImportError:
7-
from utils import load_img
85

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",
1610
"--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",
2215
"--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")
2736

28-
args = parser.parse_args()
29-
if not hasattr(args, "input"):
30-
parser.print_help()
31-
exit(1)
3237

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)
4640

4741

4842
if __name__ == "__main__":
49-
sys.exit(main())
43+
main()

src/loadimg/utils.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,25 @@ def isBase64(sb):
272272
return False
273273

274274

275+
def resize_image(image: Image.Image, max_width: int) -> Image.Image:
276+
"""Resizes a Pillow image to a maximum width maintaining the aspect ratio.
277+
278+
Args:
279+
image: The PIL Image to resize.
280+
max_width: The maximum width allowed.
281+
282+
Returns:
283+
The resized PIL Image.
284+
"""
285+
width, height = image.size
286+
if width <= max_width:
287+
return image
288+
289+
aspect_ratio = height / width
290+
new_height = int(aspect_ratio * max_width)
291+
return image.resize((max_width, new_height))
292+
293+
275294
def image_to_ascii(
276295
image: Image.Image, new_width: int = 100, ascii_chars: str = "@%#*+=-:. "
277296
) -> str:

0 commit comments

Comments
 (0)