'How to convert PNG/JPEG images to svg with ImageMagick?
It seems that with potrace installed, I can just run magick convert x.png x.svg or magick convert x.jpg x.svg.
However this just returns a deformed image in black and white, and I need the svg image to be in full color. Additionally, I really need the file format to be svg because svg images are small and portable, and I don't have a lot of disk space on my server.
Does anyone know how to convert png/jpeg to svg while retaining color?
Solution 1:[1]
For this conversion you need to install ImageMagick and Potrace. Potrace can convert pbm/pgm/ppm/bmp to SVG. So the process of conversion will be like this:
- first need to convert png/jpg to pbm/pgm/ppm/bmp using imagemagick.
- Then convert it to svg using potrace via command line.
Here's a working example with png. you can use jpg here instead of png file.
$im = new Imagick($_SERVER['DOCUMENT_ROOT'] . '/pngtosvg/image/image.png');
$im->setImageFormat('pbm');
$im->writeImage($_SERVER['DOCUMENT_ROOT'] . '/pngtosvg/image/pbmimage.pbm');
shell_exec('cd image && potrace ' . 'pbmimage.pbm' . ' -b svg');
echo file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/pngtosvg/image/pbmimage.svg');
For further explanation you can do visit this amazing repo. raster_images-to-svg
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mosafir |
