Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output PNG base64 when decode image path is - #47

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
13 changes: 10 additions & 3 deletions thumbhash/cli.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from base64 import b64encode
import io
from pathlib import Path
from typing import Any

import typer
from rich import print
from thumbhash import image_to_thumbhash, thumbhash_to_image
from typing_extensions import Annotated

app = typer.Typer()

Expand Down Expand Up @@ -32,10 +35,9 @@ def encode(
def decode(
image_path: Path = typer.Argument(
...,
help="The path where the image created from the hash will be saved",
help="The path where the image created from the hash will be saved, '-' for stdout (base64)",
file_okay=True,
dir_okay=False,
resolve_path=True,
),
hash: str = typer.Argument(..., help="The base64-encoded thumbhash"),
size: int = typer.Option(
Expand All @@ -48,7 +50,12 @@ def decode(
"""
image = thumbhash_to_image(hash, size, saturation)

image.save(image_path)
if f"{image_path}" == "-":
b = io.BytesIO()
image.save(b, "png")
print(f"PNG (base64): [green]{b64encode(b.getvalue()).decode('utf-8')}[/green]")
else:
image.save(image_path)


def main() -> Any:
Expand Down