Skip to content

Instantly share code, notes, and snippets.

@sukhitashvili
Created December 1, 2022 09:33
Show Gist options
  • Save sukhitashvili/62c231f07b20c9d28f75de07d5af2749 to your computer and use it in GitHub Desktop.
Save sukhitashvili/62c231f07b20c9d28f75de07d5af2749 to your computer and use it in GitHub Desktop.
Image to json and json to image util functions
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