0

I only found JavaScript code for doing this through browser console.

For example if I copy image address from the first images from Google images search results grid I get a Data URL.

Commands that I tried and errors:

$ curl 'data:stuff' -o filename 
curl: (3) URL using bad/illegal format or missing URL
$ curl 'data:stuff' > filename
curl: (3) URL using bad/illegal format or missing URL
$ wget 'data:stuff' -O filename
Resolving data (data)... failed: Name or service not known.
wget: unable to resolve host address ‘data’

How can I download Data URL through command line?

0

2 Answers 2

0

The data: scheme is local in the context of a webpage while both wget and curl work with remote data. Perhaps what you're really looking for is base64 -d which allows to decode such data.

Lastly, the data URI scheme can be a lot longer/larger than your shell allows, see: https://stackoverflow.com/a/41755526 and I doubt you want to keep that much random data in your shell history.

0

Adding a bit to the answer of Artem.

You can download xclip through your package manager and use a shell script (You should copy the Data URL):

#! /usr/bin/bash

data=$(xclip -out -selection clipboard)

ext=${data#*/}
ext=${ext%%;*}
ext=${ext/jpeg/jpg}

if [ $1 ]; then
        filename=$1
else
        filename=file
fi

echo "${data#*,}" | base64 -d > $filename.$ext

With no arguments filename is saved as file, otherwise it is your first argument.

Edit: Improved script -- thanks to Freddy's great comment.

1
  • There's no need for a temporary file: echo "${data#*,}" | base64 -d > filename. You could also extract the image type and use it as suffix for the output filename, e.g. ext=${data#*/}; ext=${ext%%;*}; ext=.${ext/jpeg/jpg}.
    – Freddy
    Nov 27, 2020 at 19:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.