The Lumber Room

"Consign them to dust and damp by way of preserving them"

Converting a data URL (aka data URI) to an image on the commandline (Mac OS X)

with 2 comments

This is trivial, but was awfully hard to find via Google Search. Eventually had to give up and actually think about it. :-)

So, a data-URI looks something like the following:

data:image/png;base64,[and a stream of base64 characters here]

The part after the comma is literally the contents of the file (image or whatever), encoded in base64, so all you need to do is run base64 --decode on that part.

For example, with the whole data URL copied to the clipboard, I can do:

pbpaste | sed -e 's#data:image/png;base64,##' | base64 --decode > out.png

to get it into a png file.

Advertisement

Written by S

Sun, 2015-09-27 at 19:58:17

Posted in compknow

2 Responses

Subscribe to comments with RSS.

  1. Also useful: http://www.imagemagick.org/Usage/files/#inline

    Given example becomes: convert inline:$(pbpaste) out.png

    (I didn’t know about pbpaste, nice to know!)

    Kshitij

    Thu, 2018-02-08 at 19:31:10


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: