问题
I have a black n white image which i would first like to convert into a png with a transparent background such that only the black part of the image remains. Using this output image i want to convert it to svg. All of this through code which can be connected to a back-end of a server. How can i achieve this?
回答1:
I am not certain this will always work, but it may get you started. I suggest ImageMagick
and potrace
both of which are free and available for OS X, Linux and Windows. You can integrate them into PHP and run them as library functions, but for now, I am just using the terminal/Commandline.
Let's start by making a chessboard image with ImageMagick
convert -size 200x100 pattern:checkerboard chess.jpg
We can now threshold that to make it pure black and white, like this:
convert chess.jpg -threshold 50% chessbw.jpg
Then we want to send it to potrace
to make an svg
, but that needs a pbm
format file, so we convert it to pbm
like this:
convert chess.jpg -threshold 50% chessbw.pbm
Now we tell potrace
to convert that into an svg
potrace -b svg chessbw.pbm -o result.svg
but that is not very easy to see, so we get ImageMagick
to convert result.svg
into a JPEG
but with a red background so we can see the transparent areas:
convert -background red result.svg result.jpg
Ok, that was more of an explanation than a simple answer, so I will simplify all that down to one command that does the whole lot:
convert chess.jpg -threshold 50% pbm:- | potrace -b svg - -o result.svg
I hope that helps.
来源:https://stackoverflow.com/questions/31488102/how-to-convert-a-jpeg-image-to-an-svg-in-octave