How do I convert a base64 image? - imagemagick

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.

Updated answer
From ImageMagick format docs...
The inline image look similar to inline:data:;base64,/9j/4AAQSk...knrn//2Q==. If the inline image exceeds 5000 characters, reference it from a file (e.g. inline:inline.txt).
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.
# 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 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.
# 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

Related

ImageMagick converts out of order

I have 100 images named img0.jpg to img99.jpg to be converted to a pdf file. problem is
convert img*.jpg out.pdf
adds pages in the order of 1,11,2,22,etc. how is order defined in imagemagick?
Either number your pages with zero-padded numbers like this so ImageMagick takes them in order:
img000.jpg
img001.jpg
img002.jpg
...
img098.jpg
Then your original command should work.
Or, have bash enumerate the files in order and feed the names into ImageMagick like this:
magick img{0..99}.jpg result.pdf
Or:
for file in img{0..99}.jpg; do echo $file; done | magick #- result.pdf
Or rename your files as per the first example above, but using Perl rename:
rename --dry-run 's/\D//g; $_=sprintf("f-%05d.jpg",$_)' f*jpg
Sample Output
'f0.jpg' would be renamed to 'f-00000.jpg'
'f1.jpg' would be renamed to 'f-00001.jpg'
'f10.jpg' would be renamed to 'f-00010.jpg'
'f11.jpg' would be renamed to 'f-00011.jpg'
'f12.jpg' would be renamed to 'f-00012.jpg'
You may have ls -v available to you, in which case you can try:
magick $(ls -v img*jpg) result.pdf

Funny result with multiple backup tape

I'm testing for lto6 tar encrypted backup
I'm using one G only for the test
tar cMpf - --tape-length=1G --blocking-factor 4096 -X /etc/file.exclude /| openssl enc -e -aes256 -salt -pass file:unixpass -out /dev/st0
The first tape work fine
Ask me for second..I insert press return and...
display content of a file!
"<custom_item>type : SQL_POLICYdescription : "2.11 sqlnet.ora settings - 'Setting for the remote_os_authent parameter'""....
this for thousand of lines,like cat command
Using a file for testing it cat /opt/nessus...
opt/nessus/var/nessus/audits/audit_warehouse.audit01402604000014563
Solution found: must insert tape name,i though was automatic generated

.CDR to .SVG Convert Using ImageMagick

I am on CentOS 6.4 and trying to convert .CDR to .SVG Convert Using ImageMagick using SSH command.
my 1.cdr file is in /var/www/vhosts/website.com/httpdocs/test/1.cdr
once converted to SVG it should be created in the same folder
Tried the following command:
convert /var/www/vhosts/website.com/httpdocs/test/1.cdr image.svg
The Error I am getting is:
sh: mplayer: command not found convert: Delegate failed "mplayer"
"%i" -really-quiet -ao null -vo png:z=3' #
delegate.c/InvokeDelegate/1032. convert: missing an image filename
image.svg' # convert.c/ConvertImageCommand/2800.
Not sure what does that mean ?
In order to convert CDR files you need to install uniconvertor for CDR delegate.
List of all delegates:
convert -list delegate
By default it outputs:
cdr => "uniconvertor" "%i" "%o.svg"; mv "%o.svg" "%o"
Install uniconvertor. For example, on Ubuntu it’s:
sudo apt-get install python-uniconvertor
Then run:
convert image.cdr -flatten -thumbnail '512x512' image.png
Or, with zoom cropping:
convert image.cdr -flatten -thumbnail '512x512^' -gravity center -crop 512x512+0+0 +repage image.png
And you’re done.
I convert to PNG here but you may use your own output format.
python-uniconvertor is part of inkscape.
It does not exist by itself.
Ubuntu/Mint recently removed all the old Python stuff, for Corel Draw I have to fire up the WinXP VM & Corel and export something Linux understands, usually PNG, a favourite
CDR & WMF files are pretty much dead to Linux, ImageMagick can still handle WMF though.

Can't install GhostScript in ImageMagick

Download latest version of ImageMagick. Unpacked it. Installing Ghostscript like this:
$ sudo apt-get install ghostscript
After that try to configure ImageMagick:
$ ./configure --with-gslib
$ make
$ make install
After that i try to conver PDF to jpg
$ sudo /usr/local/bin/convert in.pdf out.jpg
And i see this mistake
convert: no decode delegate for this image format `/tmp/magick-BzHdr4Kp-00000001' # error/constitute.c/ReadImage/544.
convert: Postscript delegate failed `in.PDF': Нет такого файла или каталога # error/pdf.c/ReadPDFImage/678.
convert: no images defined `out.jpg' # error/convert.c/ConvertImageCommand/3044.
What i'm doing wrong?
Try the following convert commands to see more precisely what's possibly going wrong:
convert a.pdf -debug coder a.jpg
convert a.pdf -debug all a.jpg
There will possibly be a lot of output going to stderr. Amongst the lines you may see where IM is looking for Ghostscript. Also, try
convert -list delegate
convert -list delegate | grep --color -E '(eps|pdf)'
to find with which exact commandlines ImageMagick tries to run Ghostscript (it may call gsx instead of gs, or it may look for it in /usr/local/bin/...). If you find any deviations from your real Ghostscript installation, you can possibly fix it by editing delegates.xml.
convert -list configure
will show you how ImageMagick is configured (and if, for example, gs was during compile-time in the list in DELEGATES variables). Here you also find where to look for delegates.xml:
convert -list configure | grep CONFIGURE_PATH
should list the directory where this (as well as some more) *.xml settings files are located which control how convert et al. behave...

How to convert a JPEG image into SVG format using ImageMagick?

How to convert a JPEG image into SVG format using ImageMagick?
you'll need to use potrace and convert to a bitmap first.
$convert input.jpg output.ppm
$potrace -s output.ppm -o svgout.svg
Actually, with a complete installation of a recent version of ImageMagick it should be as easy as:
convert some.jpeg some.svg
Of course, ImageMagick cannot do it all by itself -- it uses delegates (helper programs) to handle SVG input or output. (This has been pointed out by other answers already.)
To see a (partial) list of all delegates (and their respective commands), run
convert -list delegate
To see the config file where all the delegate secrets hide, see
convert -list delegate | grep delegates.xml
To see a (partial) list of SVG handling delegates, run
convert -list delegate | grep -i svg
However, ImageMagick likes to put some of its external helper utilities into 'stealth' mode and doesn't necessarily reveal their presence when using above commands.
Just look into the delegates.xml file itself. On my system it's:
grep -i svg /opt/local/etc/ImageMagick/delegates.xml | grep -i --color stealth
<delegate decode="autotrace" stealth="True" \
command=""/opt/local/bin/convert" "%i" \
"pnm:%u"\n\
"/opt/local/bin/autotrace" \
-input-format pnm \
-output-format svg \
-output-file "%o" "%u""/>
<delegate decode="svg:decode" stealth="True" \
command=""/opt/local/bin/inkscape" "%s" \
--export-png="%s" \
--export-dpi="%s" \
--export-background="%s" \
--export-background-opacity="%s" \
> "%s" 2>&1"/>
As you may see, on my system the ImageMagick installation automatically uses (amongst others)...
...inkscape to convert SVG to PNG;
...autotrace to convert PNM to SVG;
Of course, one could argue the benefits of rather using autotrace directly -- but that would require to manually convert the whatever-input-format to PNM first. So for this preliminary step you'd probably use ImageMagick anyway...
You'll actually need some software or code to vectorize your image in between, as jpg is a raster format, while SVG is a vector format. I don't think imagemagick alone can do that for you.

Resources