7

I am using the svgwrite module in my Python code and I would like to set a background color. So far I have not been able to find anything. Is there a way to do it?

I was hoping for something during the initialization:

import svgwrite

canvas = svgwrite.drawing.Drawing(fill="#225566") # or background="#225566", or sth similar
canvas.save('image.png')

Or I could probably draw rectangle all over the place, but that's just weird.

Robert Longson
  • 118,664
  • 26
  • 252
  • 242
quapka
  • 2,799
  • 4
  • 21
  • 35
  • 3
    I can't help you with Python/svgwrite, but in general there is no such thing as a "fill" for the SVG as a whole. You can set the "background-color" CSS style property on the top-most `` (but not nested SVGs) for displaying in a browser, but that probably wouldn't work with an SVG-to-PNG converter. Nothing wrong with drawing a background coloured rectangle as your first element in the graphic. – AmeliaBR Apr 05 '14 at 23:47

1 Answers1

10

It seems that svg itself does not define how to set the background colour. For svgwrite I use this:

svg_size_width = 900
svg_size_height = 4500
dwg = svgwrite.Drawing(name, (svg_size_width, svg_size_height), debug=True)
dwg.add(dwg.rect(insert=(0, 0), size=('100%', '100%'), rx=None, ry=None, fill='rgb(50,50,50)'))
dwg.save()
punchcard
  • 419
  • 3
  • 6