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

Dinu Gherman gherman at darwin.in-berlin.de
Sun Aug 26 12:56:09 EDT 2012


a few months back I asked the question below, which, sadly, hasn't attrac-
ted any feedback, yet. I'm still interested to know if there is a way to
do this inside reportlab or if I absolutely have to use pure PIL for over-
laying existing bitmap images (not exactly what the code below does).



On March, 11th, 2012 I wrote:

> 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