aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLorenz Diener <[email protected]>2019-05-11 00:55:40 +0200
committerLorenz Diener <[email protected]>2019-05-11 00:55:40 +0200
commitbf61d4881ec4cb7ab2e73a21da2349c681e371a5 (patch)
treeaa7836863c5f139b55148b91e18651642a92b0fd
parent35c43562dd3d34d6ebf7a0f757c09e8fcccc957c (diff)
downloadmastodon.py-bf61d4881ec4cb7ab2e73a21da2349c681e371a5.tar.gz
Add blurhash code
-rw-r--r--mastodon/Mastodon.py38
-rw-r--r--setup.py3
-rw-r--r--tests/test_push.py2
3 files changed, 40 insertions, 3 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
25import http_ece 25import http_ece
26import base64 26import base64
27import json 27import json
28import blurhash
28 29
29try: 30try:
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 ###
diff --git a/setup.py b/setup.py
index 737f6d9..ddb898f 100644
--- a/setup.py
+++ b/setup.py
@@ -17,7 +17,8 @@ setup(name='Mastodon.py',
17 'python-magic', 17 'python-magic',
18 'decorator>=4.0.0', 18 'decorator>=4.0.0',
19 'http_ece>=1.0.5', 19 'http_ece>=1.0.5',
20 'cryptography>=1.6.0' 20 'cryptography>=1.6.0',
21 'blurhash>=1.1.0',
21 ], 22 ],
22 tests_require=test_deps, 23 tests_require=test_deps,
23 extras_require=extras, 24 extras_require=extras,
diff --git a/tests/test_push.py b/tests/test_push.py
index 2ad215e..ad02cd8 100644
--- a/tests/test_push.py
+++ b/tests/test_push.py
@@ -66,4 +66,4 @@ def test_push_delete(api):
66 66
67 api.push_subscription_delete() 67 api.push_subscription_delete()
68 with pytest.raises(MastodonNotFoundError): 68 with pytest.raises(MastodonNotFoundError):
69 api.push_subscription() \ No newline at end of file 69 api.push_subscription()
Powered by cgit v1.2.3 (git 2.41.0)