# Display a base64 encoded image on the background of a figure

**URL:** <https://community.plotly.com/t/display-a-base64-encoded-image-on-the-background-of-a-figure/36078>\
**Category:** 📊 Plotly Python\
**Created:** [March 11, 2020, 11:02am UTC](https://community.plotly.com/t/display-a-base64-encoded-image-on-the-background-of-a-figure/36078 "2020-03-11T11:02:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gvallverdu](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/gvallverdu/32/548_2.png) [@gvallverdu](https://community.plotly.com/u/gvallverdu)\
**Post date:** [March 11, 2020, 11:02am UTC](https://community.plotly.com/t/display-a-base64-encoded-image-on-the-background-of-a-figure/36078/1 "2020-03-11T11:02:40Z")

</div>

Hello

I am trying to display a local png picture in the layout of a figure. Looking at [this](https://github.com/plotly/dash/issues/71) or [this](https://community.plotly.com/t/using-local-image-as-background-image/4381) topics, I implemented the following code which runs without any error but the output is a blank figure.

Plotly version is 4.3.0

```auto
import plotly.graph_objects as go
import base64

image_filename = "../Data/Ovaire5/20160920_153049.png"
encoded_image = base64.b64encode(open(image_filename, 'rb').read())
img = 'data:image/png;base64,{}'.format(encoded_image)

layout = go.Layout(
    images=[go.layout.Image(source=img)],
    template="plotly_white"
)
fig = go.Figure(layout=layout)
fig.show()

```

I succeed doing this with `matplotlib` from

```auto
img = plt.imread("../Data/Ovaire5/20160920_153049.png")
plt.imshow(img)

```

But I cannot simply use the output of `imread` to the plotly images argument.

Thanks for advices

---

<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:** [March 11, 2020, 1:43pm UTC](https://community.plotly.com/t/display-a-base64-encoded-image-on-the-background-of-a-figure/36078/2 "2020-03-11T13:43:25Z")

</div>

@gvallverdu

One week ago a user confirmed that the code I gave as answer here [https://community.plotly.com/t/how-do-i-add-a-background-image-with-animation-graph/35725/2](https://community.plotly.com/t/how-do-i-add-a-background-image-with-animation-graph/35725/2) worked.

---

<div class="post-metadata">

**Author:** ![gvallverdu](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/gvallverdu/32/548_2.png) [@gvallverdu](https://community.plotly.com/u/gvallverdu)\
**Post date:** [March 12, 2020, 8:46am UTC](https://community.plotly.com/t/display-a-base64-encoded-image-on-the-background-of-a-figure/36078/3 "2020-03-12T08:46:33Z")

</div>

Thank you.  
I missed the `decode()` step. It works now, with the following code. It seems, that `sizex` and `sizey` were mandatory.

```auto
image_filename = "../Data/Ovaire5/20160920_153049.png"
encoded_image = base64.b64encode(open(image_filename, 'rb').read())
img = 'data:image/png;base64,{}'.format(encoded_image.decode())

layout = go.Layout(
    images=[go.layout.Image(
        source=img, 
        sizex=1, sizey=1, 
        yanchor="bottom"
    )],
    template="plotly_white"
)
fig = go.Figure(layout=layout)
fig.show()

```
