forked from SoniGarima/Image-stitching-using-openCV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
36 lines (28 loc) · 952 Bytes
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"""Utilities
"""
import re
import base64
import numpy as np
from PIL import Image
from io import BytesIO
def base64_to_pil(img_base64):
"""
Convert base64 image data to PIL image
"""
img_base641=img_base64['avatar']
img_base642=img_base64['avatar2']
img_base641=str(img_base641)
img_base642=str(img_base642)
image_data = re.sub('^data:image/.+;base64,', '', img_base641)
image_data2 = re.sub('^data:image/.+;base64,', '', img_base642)
pil_image = Image.open(BytesIO(base64.b64decode(image_data)))
pil_image2 = Image.open(BytesIO(base64.b64decode(image_data2)))
return (pil_image,pil_image2)
def np_to_base64(img_np):
"""
Convert numpy image (RGB) to base64 string
"""
img = Image.fromarray(img_np.astype('uint8'), 'RGB')
buffered = BytesIO()
img.save(buffered, format="PNG")
return u"data:image/png;base64," + base64.b64encode(buffered.getvalue()).decode("ascii")