# Converting Byte Object to numpy array

**URL:** https://community.plotly.com/t/converting-byte-object-to-numpy-array/40189
**Category:** 📊 Plotly Python
**Created:** [May 28, 2020, 6:50am UTC](https://community.plotly.com/t/converting-byte-object-to-numpy-array/40189 "2020-05-28T06:50:25Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![f.batiri](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/f.batiri/32/11069_2.png) [@f.batiri](https://community.plotly.com/u/f.batiri)
#### Post date: [May 28, 2020, 6:50am UTC](https://community.plotly.com/t/converting-byte-object-to-numpy-array/40189/1 "2020-05-28T06:50:25Z")

</div>

I want to convert the byte object exported with `fig.to_image()`, to numpy array so it can be used in Folium as image overlay. (I can not save the image to disk)  
How can this be done?

Here is an example:

```auto
import plotly.graph_objects as go
# Create plot
fig = go.Figure(data =
    go.Contour(
        z=[[10, 10.625, 12.5, 15.625, 20],
           [5.625, 6.25, 8.125, 11.25, 15.625],
           [2.5, 3.125, 5., 8.125, 12.5],
           [0.625, 1.25, 3.125, 6.25, 10.625],
           [0, 0.625, 2.5, 5.625, 10]]
    ))
# Export byte object
img_bytes = fig.to_image(format="png",width=600, height=350)

```

I have tried using PIL:

```auto
from PIL import Image
img = Image.frombytes("RGB", (350,600), img_bytes)

```

Getting `ValueError: not enough image data` .

I have never used byte object before making this process very confusing for me.

* * *
 PS: Any other way to use plotly figure on folium map is also appreciated.

---

<div class="post-metadata">

### Author: ![empet](https://avatars.discourse-cdn.com/v4/letter/e/977dab/32.png) [@empet](https://community.plotly.com/u/empet)
#### Post date: [May 28, 2020, 8:20am UTC](https://community.plotly.com/t/converting-byte-object-to-numpy-array/40189/2 "2020-05-28T08:20:54Z")

</div>

Hi @f.batiri

This is the function that converts Plotly fig image to an array:

```auto
import io 
from PIL import Image

def plotly_fig2array(fig):
    #convert Plotly fig to an array
    fig_bytes = fig.to_image(format="png")
    buf = io.BytesIO(fig_bytes)
    img = Image.open(buf)
    return np.asarray(img)

```

---

<div class="post-metadata">

### Author: ![f.batiri](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/f.batiri/32/11069_2.png) [@f.batiri](https://community.plotly.com/u/f.batiri)
#### Post date: [May 28, 2020, 3:21pm UTC](https://community.plotly.com/t/converting-byte-object-to-numpy-array/40189/3 "2020-05-28T15:21:17Z")

</div>

Works like a charm and saved me a lot of time. Thank you very much.

---

<div class="post-metadata">

### Author: ![empet](https://avatars.discourse-cdn.com/v4/letter/e/977dab/32.png) [@empet](https://community.plotly.com/u/empet)
#### Post date: [May 28, 2020, 3:44pm UTC](https://community.plotly.com/t/converting-byte-object-to-numpy-array/40189/4 "2020-05-28T15:44:18Z")

</div>

I recently learned how to make this conversion. @jmmease answered my question. Thanks go to him.
