diff options
author | Lorenz Diener <[email protected]> | 2019-05-11 00:55:40 +0200 |
---|---|---|
committer | Lorenz Diener <[email protected]> | 2019-05-11 00:55:40 +0200 |
commit | bf61d4881ec4cb7ab2e73a21da2349c681e371a5 (patch) | |
tree | aa7836863c5f139b55148b91e18651642a92b0fd /mastodon | |
parent | 35c43562dd3d34d6ebf7a0f757c09e8fcccc957c (diff) | |
download | mastodon.py-bf61d4881ec4cb7ab2e73a21da2349c681e371a5.tar.gz |
Add blurhash code
Diffstat (limited to 'mastodon')
-rw-r--r-- | mastodon/Mastodon.py | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 27acbaa..d725dd9 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py | |||
@@ -25,6 +25,7 @@ from cryptography.hazmat.primitives.asymmetric import ec | |||
25 | import http_ece | 25 | import http_ece |
26 | import base64 | 26 | import base64 |
27 | import json | 27 | import json |
28 | import blurhash | ||
28 | 29 | ||
29 | try: | 30 | try: |
30 | from urllib.parse import urlparse | 31 | from urllib.parse import urlparse |
@@ -2315,7 +2316,42 @@ class Mastodon: | |||
2315 | ) | 2316 | ) |
2316 | 2317 | ||
2317 | return json.loads(decrypted.decode('utf-8'), object_hook = Mastodon.__json_hooks) | 2318 | return json.loads(decrypted.decode('utf-8'), object_hook = Mastodon.__json_hooks) |
2318 | 2319 | ||
2320 | ### | ||
2321 | # Blurhash utilities | ||
2322 | ### | ||
2323 | def decode_blurhash(self, media_dict, out_size = (16, 16), size_per_component = True, return_linear = True): | ||
2324 | """ | ||
2325 | Basic media-dict blurhash decoding. | ||
2326 | |||
2327 | out_size is the desired result size in pixels, either absolute or per blurhash | ||
2328 | component (this is the default). | ||
2329 | |||
2330 | By default, this function will return the image as linear RGB, ready for further | ||
2331 | scaling operations. If you want to display the image directly, set return_linear | ||
2332 | to False. | ||
2333 | |||
2334 | Returns the decoded blurhash image as a three-dimensional list: [height][width][3], | ||
2335 | with the last dimension being RGB colours. | ||
2336 | |||
2337 | For further info and tips for advanced usage, refer to the documentation for the | ||
2338 | blurhash module: https://github.com/halcy/blurhash-python | ||
2339 | """ | ||
2340 | # Figure out what size to decode to | ||
2341 | decode_components_x, decode_components_y = blurhash.components(media_dict["blurhash"]) | ||
2342 | if size_per_component == False: | ||
2343 | decode_size_x = out_size[0] | ||
2344 | decode_size_y = out_size[1] | ||
2345 | else: | ||
2346 | decode_size_x = decode_components_x * out_size[0] | ||
2347 | decode_size_y = decode_components_y * out_size[1] | ||
2348 | |||
2349 | # Decode | ||
2350 | decoded_image = blurhash.decode(media_dict["blurhash"], decode_size_x, decode_size_y, linear = return_linear) | ||
2351 | |||
2352 | # And that's pretty much it. | ||
2353 | return decoded_image | ||
2354 | |||
2319 | ### | 2355 | ### |
2320 | # Pagination | 2356 | # Pagination |
2321 | ### | 2357 | ### |