I am trying to use the "convert" command-line tool from ImageMagick. I have a base64 encoded png file and I need to convert it to another format. I am looking at documentation and a forum discussion which suggests that I should be able to use this syntax:
convert inline:file.txt file.jpg
But when I do this, I get this error message:
convert: corrupt image `file.txt' @ error/constitute.c/ReadInlineImage/910.
What am I doing wrong? How do I get convert to read a base64 image file?
Updated answer
From ImageMagick format docs...
This hints at two "gotcha" when using the inline format. First any standard base64 whitespace (unix line break) should be removed such that all information would be on one line. And second, that any data above 5000 characters should be read from a file buffer.
Original (not really correct) answer
The "corrupt image" message tells me that there may be whitespace in the base64 file. If so, the tr utility would work.
Updated Answer - now that I understand it better myself :-)
Basically, you can base64 encode an image using
openssl
like this:However, if you want
ImageMagick
to be able to read it, you need a small header at the start, to tellImageMagick
what follows. The header must contain:followed by your base64 encoded data generated using the
openssl
command above. So, depending on what features your shell has, you could do it like this with a compound statement inbash
:or like this in Windows:
Once you have the image in that format, you can then proceed to process it with
ImageMagick
like this:For those who use this in css, add
-A
flag to output in one lineOriginal Answer
After MUCH experimenting, I can do it!!! :-)
Start with Eric's (@emcconville) setup:
and now add this mess as the last line:
I guess the
data:image/png;base64,
is not present in the base64 file created byopenssl
so I create a compound statement that sends that plus the file tostdin
ofImageMagick
.