Created
December 1, 2022 09:33
-
-
Save sukhitashvili/62c231f07b20c9d28f75de07d5af2749 to your computer and use it in GitHub Desktop.
Image to json and json to image util functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import base64 | |
| import numpy as np | |
| def img_to_json(img): | |
| return { | |
| 'data': base64.b64encode(img.tobytes()).decode('utf8'), | |
| 'shape': list(img.shape) | |
| } | |
| def json_to_img(img_json): | |
| buf = base64.b64decode(img_json['data']) | |
| shape = img_json['shape'] | |
| return np.frombuffer(buf, dtype=np.uint8).reshape(*shape) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment