# Creating a PNG-image of a VegaLite-diagram directly in memory?

**URL:** https://discourse.julialang.org/t/creating-a-png-image-of-a-vegalite-diagram-directly-in-memory/104570
**Category:** General Usage
**Tags:** images, vegalite, streaming
**Created:** [October 4, 2023, 12:23pm UTC](https://discourse.julialang.org/t/creating-a-png-image-of-a-vegalite-diagram-directly-in-memory/104570 "2023-10-04T12:23:32Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Miroslav-Stimac](https://avatars.discourse-cdn.com/v4/letter/m/c89c15/32.png) [@Miroslav-Stimac](https://discourse.julialang.org/u/Miroslav-Stimac)
#### Post date: [October 4, 2023, 12:23pm UTC](https://discourse.julialang.org/t/creating-a-png-image-of-a-vegalite-diagram-directly-in-memory/104570/1 "2023-10-04T12:23:32Z")

</div>

Hello,

I created the diagram fig with VegaLite.jl and want to send it via e-mail by using SMTPClient.jl without creating a temporary file, but using IOBuffer to do it directly in the memory.  
I tried this here, but it does not work:

```julia
io = IOBuffer()

VegaLite.save(FileIO.Stream{format"PNG"}(io), fig, "png")
png_data = take!(io)
image_data = base64encode(png_data)

html_e-mail = html_e-mail * "<img src=\"data:image/png;base64,$image_data\">"

```

Error message:

```julia
ERROR: LoadError: UndefVarError: @format_str not defined

```

Does somebody have an idea what is wrong with  
`VegaLite.save(FileIO.Stream{format"PNG"}(io), fig, "png")`  
or knows another method how to send a VegaLite.jl diagram via e-mail without creating a file?

Thanks for any advices in advance!

Kind regards,  
Miroslav Stimac

---

<div class="post-metadata">

### Author: ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)
#### Post date: [October 5, 2023, 8:59am UTC](https://discourse.julialang.org/t/creating-a-png-image-of-a-vegalite-diagram-directly-in-memory/104570/2 "2023-10-05T08:59:11Z")

</div>

I succeeded to get the Base64-encoded string of a PlotlyJS plot (which is similar, I think, with VegaLite) as follows:

```julia
using PlotlyJS, FileIO, Base64

#Create a plot:

n=255
I = [i for i in 0:n, j in 0:n]
width=400; height=375
plt=Plot(heatmap(z=mod.(xor.(I, I'), 53), colorscale=colors.algae, showscale=false ),
                 Layout(width=width, height=height, yaxis_autorange="reverse"))       

io = IOBuffer()
PlotlyJS.savefig(io, plt; width=width, height=height, scale=1, format="png") 
bytes = take!(io)
bs64_string= "data:image/png;base64,$(stringmime(MIME("image/png"), bytes))"

#Display image from the bs64 string
Plot(PlotlyJS.image(; source=bs64_string, hoverinfo="none"), 
     Layout(xaxis_visible=false, yaxis_visible=false))

```

and here is the [notebook](https://nbviewer.org/gist/empet/4a024815bbc28fd1fd7c0832d702f133) illustrating the initial plot, and its image displayed from the base64-string.
