How to convert a base64 image?

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 look at the documentation and discussion of the forum , which says that I should 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 convert to read a base64 image file?

+5
source share
2 answers

Updated answer - now that I understand it better :-)

Basically, you can base64 encode an image using openssl like this:

 openssl enc -base64 -in image.png > image.b64 

However, if you want ImageMagick be able to read it, you first need a small headline to tell ImageMagick what to do. The title should contain:

 data:image/png;base64, 

followed by your base64 encoded data generated using the openssl command above. Thus, depending on what functions your shell has, you can do this with a compound statement in bash :

 { echo "data:image/png;base64,"; openssl enc -base64 -in input.png; } > image.b64 

or like this on Windows:

 echo data:image/png;base64, > image.b64 openssl enc -base64 -in image.png >> image.b64 

Once you have an image in this format, you can proceed to process it using ImageMagick as follows:

 convert inline:image.b64 result.png 

For those using this in css, add the -A flag to output on a single line

 openssl enc -base64 -A -in image.png > image.b64 

Original answer

After many experiments, I can do it !!! :-)

Start by installing Eric (@emcconville):

 # 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 think data:image/png;base64, not in the base64 file created by openssl , so I create a compound statement that sends this, plus the file in stdin from ImageMagick .

+4
source

Updated Answer

From ImageMagick document format ...

The embedded image is similar to inline:data:;base64,/9j/4AAQSk...knrn//2Q== . If the embedded image exceeds 5,000 characters, refer to it from a file (for example, inline:inline.txt ).

This points to two "gotcha" when using the built-in format. First you need to remove any standard base64 space (unix line break) so that all the information is on one line. And secondly, any data exceeding 5000 characters should be read from the file buffer.

 # Copy data to new file, striping line-breaks & adding INLINE header. (Please advise better sed/awk.) cat file.txt | tr -d "\r\n" | awk '{print "data:image/png;base64,"$1}' > file.inline # Read file as expected convert inline:file.inline file.jpg 

Original (not quite correct) answer

The message "corrupt image" tells me that there may be spaces in the base64 file. If yes, then tr .

 # For example convert rose: rose.png # Create base64 file openssl enc -base64 -in rose.png -out rose.txt # Read inline & data from stdin -- after stripping whitespace cat rose.txt | tr -d "\r\n" | convert inline:data:- out.jpg 
+3
source

Source: https://habr.com/ru/post/1231957/


All Articles