ni_hao
ni_hao

Reputation: 426

decode base64 svg data to a svg file

I have a file containing a svg image which is base64 encoded (data-uri). The file starts with

data:image/svg+xml;base64,PHN....

How to decode this to a .svg file in linux ?

Upvotes: 18

Views: 47190

Answers (7)

Iazel
Iazel

Reputation: 2336

You can copy/paste the string (data:image etc. included) in the url bar of a modern browser; it will decrypt it for you. Then you can simply save the page as an svg.

Upvotes: 47

hgiahuyy
hgiahuyy

Reputation: 308

I resolved problem:

let url = 'PHN2ZyBpZD0iTGF5ZXJfMM...'

var svg = decodeURIComponent(escape(window.atob(url)));

Upvotes: 0

Denys Sokolov
Denys Sokolov

Reputation: 83

I use SVG online decoder, fast and simple https://base64.online/decoders/decode-base64-to-svg

Upvotes: 0

Nikita Kurtin
Nikita Kurtin

Reputation: 6237

To address the OP question:

How to decode this to a .svg file in linux ?

Since linux has python by default, I suggest to use python script.

Here is a working example:

import base64 

#change "YOURFILE" with the name of your original file
with open("YOURFILE", "rb") as f: encoded = f.read()

encoded = encoded.replace("data:image/svg+xml;base64,", "")
decoded = base64.b64decode(encoded)

#change "NEWFILE" with the name that you want to give your new svg 
with open("NEWFILE.svg", "wb") as f: f.write(decoded)

If you are new to python, simply copy-paste the code above into a file with .py extension, for example aaabbb.py and then execute it like this:

python aaabbb.py

Upvotes: 7

Dharmesh Patel
Dharmesh Patel

Reputation: 41

or you can use the online tool http://www.hosting4free.info/Base64Decode/Base64-Decode.jsp

Upvotes: 0

Erik Dahlström
Erik Dahlström

Reputation: 60966

You can use e.g base64 --decode < "your base64 data here". And you probably need to strip off the data:image/svg+xml;base64, part before passing it in.

Upvotes: 1

Paul LeBeau
Paul LeBeau

Reputation: 101820

You could use an online base64 decoder, such as http://www.base64decode.org/

Upvotes: 7

Related Questions