-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
43 lines (40 loc) · 1.19 KB
/
utils.py
File metadata and controls
43 lines (40 loc) · 1.19 KB
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
36
37
38
39
40
41
42
43
#!/usr/bin/env python
import matplotlib.pyplot as plt
import numpy as np
from io import BytesIO
import base64
import os
def fig_to_uri(in_fig, close_all=True, **save_args):
"""
Save a figure as a URI
:param in_fig:
:return:
"""
out_img = BytesIO()
in_fig.savefig(out_img, format='png', **save_args)
if close_all:
in_fig.clf()
plt.close('all')
out_img.seek(0) # rewind file
encoded = base64.b64encode(out_img.read()).decode("ascii").replace("\n", "")
return "data:image/png;base64,{}".format(encoded)
def figfile_to_uri(in_fig, close_all=True, **save_args):
"""
Save a figure as a URI
:param in_fig:
:return:
"""
out_img = BytesIO()
in_fig.savefig(out_img, format='png', **save_args)
if close_all:
in_fig.clf()
plt.close('all')
out_img.seek(0) # rewind file
encoded = base64.b64encode(out_img.read()).decode("ascii").replace("\n", "")
return "data:image/png;base64,{}".format(encoded)
def get_img_src(f):
if not os.path.exists(f):
return ''
encoded_image = base64.b64encode(open(f, 'rb').read())
src = 'data:image/png;base64,{}'.format(encoded_image.decode())
return src