|
1 | | -# thumbhash-python |
2 | | -A Python implementation of the Thumbhash image placeholder generation algorithm. |
| 1 | +<h1 align="center"> |
| 2 | + <br> |
| 3 | + ThumbHash for Python |
| 4 | +</h1> |
| 5 | +<p align="center"> |
| 6 | + <p align="center">Open-source, end-to-end encrypted tool to manage secrets and configs across your team, devices, and infrastructure.</p> |
| 7 | +</p> |
| 8 | + |
| 9 | + |
| 10 | +<p align="center"> |
| 11 | +<a href="https://github.com/Astropilot/thumbhash-python/actions?query=workflow%3ATest+event%3Apush+branch%3Amain" target="_blank"> |
| 12 | + <img src="https://github.com/Astropilot/thumbhash-python/workflows/Test/badge.svg?event=push&branch=main" alt="Test"> |
| 13 | +</a> |
| 14 | +<a href="https://coverage-badge.samuelcolvin.workers.dev/redirect/Astropilot/thumbhash-python" target="_blank"> |
| 15 | + <img src="https://coverage-badge.samuelcolvin.workers.dev/Astropilot/thumbhash-python.svg" alt="Coverage"> |
| 16 | +</a> |
| 17 | +<a href="https://pypi.org/project/thumbhash-python" target="_blank"> |
| 18 | + <img src="https://img.shields.io/pypi/v/thumbhash-python?color=%2334D058&label=pypi%20package" alt="Package version"> |
| 19 | +</a> |
| 20 | +<a href="https://pypi.org/project/thumbhash-python" target="_blank"> |
| 21 | + <img src="https://img.shields.io/pypi/pyversions/thumbhash-python.svg?color=%2334D058" alt="Supported Python versions"> |
| 22 | +</a> |
| 23 | +<a href="https://github.com/Astropilot/thumbhash-python/blob/master/LICENSE"> |
| 24 | + <img src="https://img.shields.io/github/license/Astropilot/thumbhash-python" alt="MIT License"> |
| 25 | +</a> |
| 26 | +</p> |
| 27 | + |
| 28 | +# Introduction |
| 29 | + |
| 30 | +The thumbhash library implements the [Thumbhash](https://evanw.github.io/thumbhash/) image placeholder generation algorithm invented by [Evan Wallace](https://madebyevan.com/) in Python. |
| 31 | + |
| 32 | +A full explanation and interactive example of the algorithm can be found at https://github.com/evanw/thumbhash |
| 33 | + |
| 34 | +# Installation |
| 35 | + |
| 36 | +You need Python 3.7+. |
| 37 | + |
| 38 | +```console |
| 39 | +$ pip install thumbhash-python |
| 40 | +``` |
| 41 | + |
| 42 | +# Usage |
| 43 | + |
| 44 | +Create thumbhash from image file: |
| 45 | +```py |
| 46 | +from thumbhash import image_to_thumbhash |
| 47 | + |
| 48 | +with open('image.jpg', 'rb') as image_file: |
| 49 | + hash = image_to_thumbhash(image_file) |
| 50 | +``` |
| 51 | + |
| 52 | +You can also pass file name as parameter to the function: |
| 53 | +```py |
| 54 | +from thumbhash import image_to_thumbhash |
| 55 | + |
| 56 | +hash = image_to_thumbhash('image.jpg') |
| 57 | +``` |
| 58 | +These functions use the Pillow library to read the image. |
| 59 | + |
| 60 | +If you want to directly convert a rgba array to a thumbhash, you can use the low-level function: |
| 61 | +```py |
| 62 | +from thumbhash.encode import rgba_to_thumbhash |
| 63 | + |
| 64 | +rgba_to_thumbhash(w: int, h: int, rgba: Sequence[int]) -> bytes |
| 65 | +``` |
| 66 | + |
| 67 | +To decode a thumbhash into an image: |
| 68 | +```py |
| 69 | +from thumbhash import thumbhash_to_image |
| 70 | + |
| 71 | +image = thumbhash_to_image("[THUMBHASH]", base_size=128) |
| 72 | + |
| 73 | +image.show() |
| 74 | + |
| 75 | +image.save('path/to/file.png') |
| 76 | +``` |
| 77 | + |
| 78 | +Alternatively you can use the following function to deal directly with the pixels array (without relying on Pillow): |
| 79 | +```py |
| 80 | +from thumbhash.decode import thumbhash_to_rgba |
| 81 | + |
| 82 | +def thumbhash_to_rgba( |
| 83 | + hash: bytes, base_size: int = 32, saturation_boost: float = 1.25 |
| 84 | +) -> Tuple[int, int, List[int]] |
| 85 | +``` |
| 86 | + |
| 87 | +## CLI |
| 88 | + |
| 89 | +You can also use the CLI mode to encode or decode directly via your shell. |
| 90 | + |
| 91 | +**Usage**: |
| 92 | + |
| 93 | +```console |
| 94 | +$ thumbhash [OPTIONS] COMMAND [ARGS]... |
| 95 | +``` |
| 96 | + |
| 97 | +**Options**: |
| 98 | + |
| 99 | +* `--install-completion`: Install completion for the current shell. |
| 100 | +* `--show-completion`: Show completion for the current shell, to copy it or customize the installation. |
| 101 | +* `--help`: Show this message and exit. |
| 102 | + |
| 103 | +**Commands**: |
| 104 | + |
| 105 | +* `decode`: Save thumbnail image from thumbhash |
| 106 | +* `encode`: Get thumbhash from image |
| 107 | + |
| 108 | +### `thumbhash decode` |
| 109 | + |
| 110 | +Save thumbnail image from thumbhash |
| 111 | + |
| 112 | +**Usage**: |
| 113 | + |
| 114 | +```console |
| 115 | +$ thumbhash decode [OPTIONS] IMAGE_PATH HASH |
| 116 | +``` |
| 117 | + |
| 118 | +**Arguments**: |
| 119 | + |
| 120 | +* `IMAGE_PATH`: The path where the image created from the hash will be saved [required] |
| 121 | +* `HASH`: The base64-encoded thumbhash [required] |
| 122 | + |
| 123 | +**Options**: |
| 124 | + |
| 125 | +* `-s, --size INTEGER RANGE`: The base size of the output image [default: 32; x>=1] |
| 126 | +* `--saturation FLOAT`: The saturation boost factor to use [default: 1.25] |
| 127 | +* `--help`: Show this message and exit. |
| 128 | + |
| 129 | +### `thumbhash encode` |
| 130 | + |
| 131 | +Get thumbhash from image |
| 132 | + |
| 133 | +**Usage**: |
| 134 | + |
| 135 | +```console |
| 136 | +$ thumbhash encode [OPTIONS] IMAGE_PATH |
| 137 | +``` |
| 138 | + |
| 139 | +**Arguments**: |
| 140 | + |
| 141 | +* `IMAGE_PATH`: The path of the image to convert [required] |
| 142 | + |
| 143 | +**Options**: |
| 144 | + |
| 145 | +* `--help`: Show this message and exit. |
| 146 | + |
| 147 | + |
| 148 | +## Contributing |
| 149 | + |
| 150 | +See [Contributing documentation](./.github/CONTRIBUTING.md) |
| 151 | + |
| 152 | +## License |
| 153 | + |
| 154 | +`thumbhash-python` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license. |
0 commit comments