How to convert SVG image to 32-bit RGBA with ImageMagick? - svg

How to convert SVG image to 32-bit RGBA with ImageMagick?
Background must be transparent.

Simply try this command:
convert in.svg -transparent white out.png
(Assuming the original SVG uses a transparent or white background. Otherwise, if SVG background is red or blue or '#235689': use -transparent red, -transparent blue or -transparent '#235689'....)
If your version of ImageMagick doesn't do it right, check if convert -list configure | grep svg shows rsvg output in the DELEGATES and CONFIGURE lines.

I found that setting background to none worked for me:
convert -background none in.svg out.png

Related

Convert svg animation to apng sequence

Is there any way to do this?
I tried magick -background none in.svg out.png with ImageMagick 7.1.0-61, it removed the transparency and only kept the first frame of animation.

How to replace one color in a image with another one?

I've this image:
Its background (and only) color is "#687a40" but want to change it for the color "#9ea09a" (a light grey).
I've tried with this command...:
gm convert photo.png -fuzz 0% -fill "#9ea09a" -opaque "#678a40" photoGREY.png
...but I've not seen any effect on the image. I'm sure I'm doing something wrong but GM's documentation is not very friendly.
Your image has an alpha channel, so you need:
gm convert input.png -fill "#9ea09a" -opaque "#687a40ff" result.png

Convert svg to png imagemagick black border around path

I am on macOs Mojave 10.14.3 (18D109)
My imagemagick details (installed with brew)
convert -version
Version: ImageMagick 7.0.8-35 Q16 x86_64 2019-03-25 https://imagemagick.org
Copyright: © 1999-2019 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules OpenMP
Delegates (built-in): bzlib freetype heic jng jp2 jpeg lcms ltdl lzma openexr png tiff webp xml zlib
I have a basic svg (adopted from W3Schools example):
<?xml version="1.0"?>
<svg xmlns="http://www.w3.org/2000/svg" width="52" height="52" viewBox="0 0 52 52">
<path fill="#fff" d="M22 0 L33 52 L12 42 Z">
</path>
</svg>
That looks like this (Gapplin):
Now when i convert it to png like so:
convert -background none -density 9600 -resize 32x "myFile.svg" "myFile.png"
i get a perfect image, except it has a black border:
I tried just about every variation of command options for imagemagick that had to do with color or black and sadly it never comes out with what i want - transparent background, solid white path (as described by fill on the SVG).
Any suggestions?
In ImageMagick, SVG files can be rendered by any one of three tools: Imagemagick's MSVG/XML, RSVG delegate and Inkscape, usually in order of increasing accuracy.
I just tried the following command with Inkscape 0.92.4 and it comes out totally white. If Inkscape is installed Imagemagick will use it automatically. But in this case it is not working well with that file.
magick test.svg test2.png
But it works fine if I added -background none
magick -background none test.svg test2.png
I get a white triangle on a transparent background.
or if I want a black background
magick -background black test.svg test2.png
When I force Imagemagick to use MSVG, I get a white triangle with a simple black outline on a white background. So again not correct by your comments.
magick MSVG:test.svg test2.png
If I add -background none, then I get white triangle with black outline on a transparent background.
magick -background none MSVG:test.svg test2.png
But if I just want a black background, then
magick -background black MSVG:test.svg test2.png
I then get a white triangle on a black background. But I suspect the white triangle is slightly smaller from the black border it had.
When I force the use of RSVG 2.44.12 by creating an entry in the delegates.xml file, I get a white triangle on a transparent background, which I assume is what you want.
magick RSVG:test.svg test3.png
or
magick -background none RSVG:test.svg test3.png
You can tell if your Imagemagick has MSVG/XML or RSVG by
magick -list format
and look for the line starting with SVG. At the end of that line it will either say XML or RSVG. You cannot tell about Inkscape with this method.
Check to see what you are using for your SVG renderer and its version. Also the version of libpng. Mine is 1.6.36
From your version information, I do not see rsvp listed. So I assume you are using MSVG, but it could be Inkscape if you have installed that on your system.
I am on Mac OSX Sierra with Imagemagick 7.0.8.35 Q16.
I had this problem converting SVG to PNG with PHP - black lines around the path. I install Inkscape on the server that was processing the image and that seemed to fix it, no code change necessary.
In my case, switching converters did not help.
Thus I experimented with the SVG itself.
And adding stroke-opacity="0" to the svg (or the individual paths if you dont want everything to have no outline) helped.
I assume that there is some default for stroke values that the converters set that makes them visible and this defines them as invisible on the svg level

How to change picture background color using ImageMagick?

I want to change my picture background color, This is my source http://cdn.mayike.com/emotion/img/attached/1/image/dt/20170916/18/20170916180007_833.png.
If I want to change the background color to another color, what should I do?
If I want to change the background color to transparent, what should I do?
I tried using convert aa.png -background '#0e0e0e' bb.png, but that doesn't work.
I do not think you will get a perfect result due to the fact that your image is not binary. Nevertheless in Imagemagick you have two choices. First you could just change all white to red:
convert 20170916180007_833.png.jpeg -fuzz 25% -fill red -opaque white -flatten result1.png
Or you can do a flood fill to just change the outer area:
convert 20170916180007_833.png.jpeg -fuzz 25% -fill none -draw "matte 0,0 floodfill" -background red -flatten result2.jpg
To apply any background color, first the program should know the edges and background.
The image you use doesn't do that.
Although your command is correct, it doesn't work since edges and background is not distinguished. So we use Masking first
First run this to get edges:
convert aa.png -bordercolor white -border 1x1 \
-alpha set -channel RGBA -fuzz 20% \
-fill none -floodfill +0+0 white \
-shave 1x1 aa.png
Now you'll get the edges saved in aa.png. At this point your background is transparent. Next run the background color change command:
convert aa.png -background blue -flatten bb.png
Now you'll have the expected result.
Output Final Image
Source:http://www.imagemagick.org/Usage/masking/#bg_remove
Usually -opaque, or -transparent, is all that's needed. But because the image is black & white, we need to isolate ROI to effect. I would recommend using the MVG draw commands.
If change to another color, what should I do?
convert 20170916180007_833.png \
-fill '#0e0e0e' -fuzz 20% \
-draw 'color 0,0 floodfill' \
output_color.png
If change to transparent, what should I do?
convert 20170916180007_833.png \
-fill 'transparent' -fuzz 20% \
-draw 'color 0,0 floodfill' \
output_transparent.png

ImageMagic renders image with black background

I'm converting pdf files to images with ImageMagic, everything is ok until I use -resize option, then I get image with black background. I use this command:
convert -density 400 image.pdf -resize 25% image.png
I need to use -resize option otherwise I get really large image. Is there any other option which I can use to resize image or is there option to set background in white.
That's not a Ghostscript command, You will need to find out what convert is sending to Ghostscript. As it stands I can't even be sure that your problem is with Ghostscript ,since it could easily be something that convert is doing.
Solved it by removing alpha from image, it seems imagemagic tries to apply some opacity but since jpeg doesn't allow transperency result was black background. So I found example on imagemagic website and it helped me:
convert a.pdf -background skyblue -alpha remove -alpha off a_remove.jpg

Resources