Coverage for thumbhash / cli.py: 67%

24 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-28 17:06 +0000

1import io 

2from base64 import b64encode 

3from pathlib import Path 

4from typing import Annotated, Any 

5 

6import typer 

7from rich import print 

8from rich.console import Console 

9 

10from thumbhash import image_to_thumbhash, thumbhash_to_image 

11 

12app = typer.Typer() 

13 

14 

15@app.command() 

16def encode( 

17 image_path: Annotated[ 

18 Path, 

19 typer.Argument( 

20 help="The path of the image to convert", 

21 exists=True, 

22 file_okay=True, 

23 dir_okay=False, 

24 readable=True, 

25 resolve_path=True, 

26 ), 

27 ], 

28) -> None: 

29 """ 

30 Get thumbhash from image 

31 """ 

32 hash = image_to_thumbhash(image_path) 

33 

34 print(f"Thumbhash (base64): [green]{hash}[/green]") 

35 

36 

37@app.command() 

38def decode( 

39 image_path: Annotated[ 

40 Path, 

41 typer.Argument( 

42 help="The path where the image created from the hash will be saved, '-' for stdout (png base64)", 

43 file_okay=True, 

44 dir_okay=False, 

45 resolve_path=True, 

46 allow_dash=True, 

47 ), 

48 ], 

49 hash: Annotated[str, typer.Argument(help="The base64-encoded thumbhash")], 

50 size: Annotated[ 

51 int, 

52 typer.Option("--size", "-s", help="The base size of the output image", min=1), 

53 ] = 32, 

54 saturation: Annotated[ 

55 float, typer.Option(help="The saturation boost factor to use") 

56 ] = 1.25, 

57) -> None: 

58 """ 

59 Save thumbnail image from thumbhash 

60 """ 

61 image = thumbhash_to_image(hash, size, saturation) 

62 

63 if str(image_path) == "-": 

64 b = io.BytesIO() 

65 console = Console(markup=False, no_color=True, highlight=False) 

66 

67 image.save(b, "png") 

68 console.print(b64encode(b.getvalue()).decode("utf-8")) 

69 else: 

70 image.save(image_path) 

71 

72 

73def main() -> Any: 

74 return app()