Skip to content

Commit

Permalink
add img_encode utility function
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentsarago committed Mar 9, 2018
1 parent 6b0ba51 commit 0552ed8
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 35 deletions.
63 changes: 31 additions & 32 deletions examples/image-vis-type-example.ipynb

Large diffs are not rendered by default.

25 changes: 24 additions & 1 deletion mapboxgl/utils.py
@@ -1,5 +1,11 @@
import json
import base64
from io import BytesIO

from matplotlib.image import imsave

from .colors import color_ramps
import geojson, json, re
import geojson
from colour import Color


Expand Down Expand Up @@ -147,3 +153,20 @@ def create_color_stops(breaks, colors='RdYlGn', color_ramps=color_ramps):
stops.append([b, ramp[i]])

return stops


def img_encode(arr, **kwargs):
"""Encode ndarray to base64 string image data
Parameters
----------
arr: ndarray (rows, cols, depth)
kwargs: passed directly to matplotlib.image.imsave
"""
sio = BytesIO()
imsave(sio, arr, **kwargs)
sio.seek(0)
img_format = kwargs['format'] if kwargs.get('format') else 'png'
img_str = base64.b64encode(sio.getvalue()).decode()

return 'data:image/{};base64,{}'.format(img_format, img_str)
2 changes: 1 addition & 1 deletion mapboxgl/viz.py
Expand Up @@ -289,7 +289,7 @@ def __init__(self,
**kwargs):
"""Construct a Mapviz object
:param coordinates: property to determine image coordinates.
:param coordinates: property to determine image coordinates (UL, UR, LR, LL).
EX. [[-80.425, 46.437], [-71.516, 46.437], [-71.516, 37.936], [-80.425, 37.936]]
:param image: image url or base64 encoded. EX. "https://www.mapbox.com/mapbox-gl-js/assets/radar.gif"]
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -31,6 +31,6 @@
'mapboxgl': ['templates/*']},
include_package_data=True,
zip_safe=False,
install_requires=['jinja2', 'geojson', 'colour'],
install_requires=['jinja2', 'geojson', 'colour', 'matplotlib'],
extras_require={
'test': ['pytest', 'pytest-cov', 'codecov', 'mock', 'jupyter', 'Sphinx', 'pandas']})

0 comments on commit 0552ed8

Please sign in to comment.