[reportlab-users] How to save drawings with transparent background as PNG?

Dinu Gherman gherman at darwin.in-berlin.de
Sun Mar 11 08:26:02 EDT 2012


Hi,

it seems like I am trying to do an impossible thing: generate a drawing
and save it as PNG with transparent background, see code below. Reading
the source code for renderPM provided little insight, so I wonder if it
can be done and how? (I'm using RL 2.5, BTW.)

Thanks,

Dinu

PS:

from reportlab.lib import colors
from reportlab.graphics.shapes import Drawing, String
from reportlab.graphics import renderPM

d = Drawing(100, 100)
s = String(50, 50, "foo bar",
fontName="Helvetica", fontSize=18,
textAnchor="middle",
fillColor=colors.black)
d.add(s)

renderPM.drawToFile(d, "test.png", fmt="PNG", bg=None)
p = renderPM.drawToPIL(d, bg=0xffffff, configPIL={'transparent': 0xffffff})
p.save("test-pil.png")



More information about the reportlab-users mailing list