Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the background color when rendering a PNG using PythonMagick

Tags:

I'm trying to render an SVG->PNG using PythonMagick, but it seems that the background color is ignored.

Using the ImageMagick command, things work as expected:

tmp$ convert -background none access.svg access.png
tmp$ convert -background red access.svg access2.png

which results in transparent and red backgrounds respectively.

But using PythonMagick, it seems the background color is ignored:

import PythonMagick
svg = PythonMagick.Image('access.svg')
svg.backgroundColor().to_std_string()
'#FFFFFFFFFFFF'
svg.backgroundColor().alpha()
0.0
svg.backgroundColor('none')
svg.backgroundColor().to_std_string()
'#0000000000000000'
svg.backgroundColor().alpha()
1.0
svg.write('access.png')
svg.backgroundColor('red')
svg.backgroundColor().to_std_string()
'#FFFF00000000'
svg.backgroundColor().alpha()
0.0
svg.write('access2.png')

Checking the output shows that both pngs have a white background. Is there another way to set the background color so that it is recognised when writing the image?

like image 997
Michael Nelson Avatar asked Apr 27 '11 15:04

Michael Nelson


People also ask

How can I change the background of a PNG?

Upload your PNG file by clicking the “Choose File” button. Under “Tools”, choose “Painting Tools” > “Brush” and select the color that you want for your PNG file. Next, brush the background of your PNG file with the mouse pointer. Lastly, save your file by clicking the “Download” button.

How do I save a PNG with a white background?

How to save a PNG with a white background. This is simple – just save the PNG as a JPG and your JPG will automatically have a white background. That's because when you save a PNG with a transparent background as a JPG, Photoshop automatically replaces the transparent parts of the image with white. That's it for now.


1 Answers

ImageMagick (and PythonMagick as its python API) has very poor SVG support. Do not even expect that it will render SVG file as it is written in the SVG specification. It converts SVG into internal MVG language and then converts to PNG. See http://www.imagemagick.org/script/magick-vector-graphics.php

ImageMagick is not a tool for manipulating vector graphics.

like image 197
Maksym Polshcha Avatar answered Sep 21 '22 21:09

Maksym Polshcha