Skip to content

Commit 9da9b2d

Browse files
committed
Fixed Image not JSON serializable error
1 parent 1847061 commit 9da9b2d

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

doc/python/performance.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,15 +263,24 @@ coordinates = [[coords_lon[0], coords_lat[0]],
263263

264264
from colorcet import fire
265265
import datashader.transfer_functions as tf
266+
import io
267+
import base64
268+
269+
# Get PIL Image and convert it into a JSON Serializable format
266270
img = tf.shade(agg, cmap=fire)[::-1].to_pil()
271+
buffer = io.BytesIO()
272+
img.save(buffer, format='PNG')
273+
raw_data = buffer.getvalue()
274+
img_base64 = base64.b64encode(raw_data).decode('utf-8')
275+
url = f"data:image/png;base64,{img_base64}"
267276

268277
import plotly.express as px
269278
# Trick to create rapidly a figure with map axes
270279
fig = px.scatter_map(dff[:1], lat='Lat', lon='Lon', zoom=12)
271280
# Add the datashader image as a tile map layer image
272281
fig.update_layout(
273282
map_style="carto-darkmatter",
274-
map_layers=[{"sourcetype": "image", "source": img, "coordinates": coordinates}],
283+
map_layers=[{"sourcetype": "image", "source": url, "coordinates": coordinates}],
275284
)
276285
fig.show()
277286
```

0 commit comments

Comments
 (0)