# How to base64 encode a rasterized image for embedding it into SVG?

**URL:** https://discourse.julialang.org/t/how-to-base64-encode-a-rasterized-image-for-embedding-it-into-svg/90848
**Category:** Signal and Image Processing
**Tags:** images, base64, svg
**Created:** [November 26, 2022, 1:07pm UTC](https://discourse.julialang.org/t/how-to-base64-encode-a-rasterized-image-for-embedding-it-into-svg/90848 "2022-11-26T13:07:41Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)
#### Post date: [November 26, 2022, 1:07pm UTC](https://discourse.julialang.org/t/how-to-base64-encode-a-rasterized-image-for-embedding-it-into-svg/90848/1 "2022-11-26T13:07:41Z")

</div>

Below I try to do this using just the `Base64` module.

MWE

```julia
using Base64
using Downloads
using FileIO

path = Downloads.download("https://github.com/MakieOrg/Makie.jl/raw/master/assets/doge.png")
img = load(path)

# reduce quality a bit, otherwise firefox complains
img = img[1:4:end,1:4:end]
w, h = size(img)

encoded_img = base64encode(img)

svg = """
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<image width="$w" height="$h" xlink:href="data:image/png;base64,$encoded_img"/>
</svg>
"""

open("/tmp/mwe.svg", "w") do f
  write(f, svg)
 end

```

Unfortunately, my Firefox says that this image contains errors.  
I also tried converting the image to `RGBf` first and then encode it, but without success.  
I also looked into `Images.jl`’s docs but nowhere is something mentioned about base64 encoding.

Does anyone know how to do this?

PS: The svg’s xml structure itself seems to be correct, because when I copy the encoded string from e.g. this svg here [File:Cabezo de Alcalá aerial view-fr.svg - Wikimedia Commons](https://commons.wikimedia.org/wiki/File:Cabezo_de_Alcal%C3%A1_aerial_view-fr.svg) then it renders fine in Firefox.

---

<div class="post-metadata">

### Author: ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)
#### Post date: [November 26, 2022, 2:10pm UTC](https://discourse.julialang.org/t/how-to-base64-encode-a-rasterized-image-for-embedding-it-into-svg/90848/2 "2022-11-26T14:10:57Z")

</div>

> [@fatteneder](#):
>
> `encoded_img`

But is `encoded_img` still a PNG ‘document’, or an array of RGBs?

---

<div class="post-metadata">

### Author: ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)
#### Post date: [November 26, 2022, 2:31pm UTC](https://discourse.julialang.org/t/how-to-base64-encode-a-rasterized-image-for-embedding-it-into-svg/90848/3 "2022-11-26T14:31:38Z")

</div>

Yeah, good question.

`img` itself is an array of `RGAf`s, `encoded_img` is then a string of gibberish.

Maybe I need to add something like a header in front of the gibberish?  
Also tried `image/jpeg` but that did not work either …

---

<div class="post-metadata">

### Author: ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)
#### Post date: [November 26, 2022, 2:34pm UTC](https://discourse.julialang.org/t/how-to-base64-encode-a-rasterized-image-for-embedding-it-into-svg/90848/4 "2022-11-26T14:34:05Z")

</div>

i think the data string must be encoded PNG or JPEG file data, rather than nice arrays of plain RGB.

---

<div class="post-metadata">

### Author: ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)
#### Post date: [November 26, 2022, 2:43pm UTC](https://discourse.julialang.org/t/how-to-base64-encode-a-rasterized-image-for-embedding-it-into-svg/90848/5 "2022-11-26T14:43:58Z")

</div>

You were right. Just encoding the raw content of the file is all that it needed:

```julia
using Base64
using Downloads
using FileIO

path = Downloads.download("https://github.com/MakieOrg/Makie.jl/raw/master/assets/doge.png")
img = read(path)
encoded_img = base64encode(img)

w, h = 100, 100

svg = """
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<image width="$w" height="$h" xlink:href="data:image/png;base64,$encoded_img"/>
</svg>
"""

write("/tmp/mwe.svg", svg)

```

Thank you!

---

<div class="post-metadata">

### Author: ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)
#### Post date: [November 26, 2022, 3:01pm UTC](https://discourse.julialang.org/t/how-to-base64-encode-a-rasterized-image-for-embedding-it-into-svg/90848/6 "2022-11-26T15:01:25Z")

</div>

Cool, glad it’s working.

Another approach, just for fun:

```julia
using Luxor
using Downloads
using FileIO
path = Downloads.download("https://github.com/MakieOrg/Makie.jl/raw/master/assets/doge.png")
img = load(path)
img = img[1:3:end, end:-3:1]
Drawing(200, 200, "/tmp/doge.svg")
origin()
placeimage(img, boxtopleft())
fontsize(20)
sethue("black")
text("One Doge", O + (0, 40), halign=:center)
finish()
preview()

```

 ![Screenshot 2022-11-26 at 15.00.04](https://global.discourse-cdn.com/julialang/original/3X/5/2/521b16b8ac252082f04dcd06160fca6c76c9e3ba.png)

---

<div class="post-metadata">

### Author: ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)
#### Post date: [November 26, 2022, 3:13pm UTC](https://discourse.julialang.org/t/how-to-base64-encode-a-rasterized-image-for-embedding-it-into-svg/90848/7 "2022-11-26T15:13:04Z")

</div>

Oh nice. Really need to check out `Luxor.jl` at some point.

One thing: Your picture shows some rendering artifacts similar to what we observed with `CairoMakie.jl` recently: [Artificial colored edges in CairoMakie when saving an image to PDF. · Issue #2162 · MakieOrg/Makie.jl · GitHub](https://github.com/MakieOrg/Makie.jl/issues/2162)  
Here is the fix: [use premultiplied alpha for cairo argb surfaces by jkrumbiegel · Pull Request #2304 · MakieOrg/Makie.jl · GitHub](https://github.com/MakieOrg/Makie.jl/pull/2304)  
Maybe that could be applied to `Luxor` too?

---

<div class="post-metadata">

### Author: ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)
#### Post date: [November 26, 2022, 3:15pm UTC](https://discourse.julialang.org/t/how-to-base64-encode-a-rasterized-image-for-embedding-it-into-svg/90848/8 "2022-11-26T15:15:30Z")

</div>

Oh, I thought that was just the resampling … 😂

I remember some encounters with premultiplied alpha a few years ago. I’ll investigate, on some rainy day soon…
