libvips / pyvips

python binding for libvips using cffi
MIT License
649 stars 50 forks source link

change default background color #501

Open sunwoo76 opened 2 months ago

sunwoo76 commented 2 months ago

I have a simple code as below:

image = pyvips.Image.new_from_file("output22.svg")
image.write_to_file("output22.png")

The result always have RGB white colored background.

how can I save the svg file with transparent rgba background in png format?

Thanks!

jcupitt commented 2 months ago

Hello @sunwoo76,

The best way is to use an SVG file with a transparent background, of course. With this SVG:

lion

I can run:

vips copy lion.svg x.png

To make:

x

If your SVG file has a solid background, you can write some code to search for background pixels and make them transparent, but the quality may not be so good. It's hard to get the edges exactly right, and hard to always detect all the background.

Do you have a sample SVG? What quality level do you need?