diff options
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 181b0e7..174e798 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 |
@@ -2346,7 +2347,42 @@ class Mastodon: | |||
2346 | ) | 2347 | ) |
2347 | 2348 | ||
2348 | return json.loads(decrypted.decode('utf-8'), object_hook = Mastodon.__json_hooks) | 2349 | return json.loads(decrypted.decode('utf-8'), object_hook = Mastodon.__json_hooks) |
2349 | 2350 | ||
2351 | ### | ||
2352 | # Blurhash utilities | ||
2353 | ### | ||
2354 | def decode_blurhash(self, media_dict, out_size = (16, 16), size_per_component = True, return_linear = True): | ||
2355 | """ | ||
2356 | Basic media-dict blurhash decoding. | ||
2357 | |||
2358 | out_size is the desired result size in pixels, either absolute or per blurhash | ||
2359 | component (this is the default). | ||
2360 | |||
2361 | By default, this function will return the image as linear RGB, ready for further | ||
2362 | scaling operations. If you want to display the image directly, set return_linear | ||
2363 | to False. | ||
2364 | |||
2365 | Returns the decoded blurhash image as a three-dimensional list: [height][width][3], | ||
2366 | with the last dimension being RGB colours. | ||
2367 | |||
2368 | For further info and tips for advanced usage, refer to the documentation for the | ||
2369 | blurhash module: https://github.com/halcy/blurhash-python | ||
2370 | """ | ||
2371 | # Figure out what size to decode to | ||
2372 | decode_components_x, decode_components_y = blurhash.components(media_dict["blurhash"]) | ||
2373 | if size_per_component == False: | ||
2374 | decode_size_x = out_size[0] | ||
2375 | decode_size_y = out_size[1] | ||
2376 | else: | ||
2377 | decode_size_x = decode_components_x * out_size[0] | ||
2378 | decode_size_y = decode_components_y * out_size[1] | ||
2379 | |||
2380 | # Decode | ||
2381 | decoded_image = blurhash.decode(media_dict["blurhash"], decode_size_x, decode_size_y, linear = return_linear) | ||
2382 | |||
2383 | # And that's pretty much it. | ||
2384 | return decoded_image | ||
2385 | |||
2350 | ### | 2386 | ### |
2351 | # Pagination | 2387 | # Pagination |
2352 | ### | 2388 | ### |