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 - now that I understand it better myself :-)
Basically, you can base64 encode an image using openssl
like this:
openssl enc -base64 -in image.png > image.b64
However, if you want ImageMagick
to be able to read it, you need a small header at the start, to tell ImageMagick
what follows. The header must contain:
data:image/png;base64,
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 in bash
:
{ echo "data:image/png;base64,"; openssl enc -base64 -in input.png; } > image.b64
or like this in Windows:
echo data:image/png;base64, > image.b64
openssl enc -base64 -in image.png >> image.b64
Once you have the image in that format, you can then proceed to process it with ImageMagick
like this:
convert inline:image.b64 result.png
For those who use this in css, add -A
flag to output in one line
openssl enc -base64 -A -in image.png > image.b64
Original Answer
After MUCH experimenting, I can do it!!! :-)
Start with Eric's (@emcconville) setup:
# For example
convert rose: rose.png
# Create base64 file
openssl enc -base64 -in rose.png -out rose.txt
and now add this mess as the last line:
{ echo "data:image/png;base64,"; cat rose.txt; } | convert inline:- out.jpg
I guess the data:image/png;base64,
is not present in the base64 file created by openssl
so I create a compound statement that sends that plus the file to stdin
of ImageMagick
.