Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
74 changes: 34 additions & 40 deletions src/loadimg/loadimg.py
Original file line number Diff line number Diff line change
@@ -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()
19 changes: 19 additions & 0 deletions src/loadimg/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading