Search code examples
pythonreportlab

python: reportlab, how to remove black borders from image


I am trying to generate a pdf file, using python reportlab, but is seems image is displayed with strange black border in pdf.

Here is the code:

# Standalone script to generate pdf lessons

from reportlab.pdfgen import canvas
def hello(c):
    c.drawImage("./media/files/1.png", 0, 600, 350, 350)


c = canvas.Canvas("hello.pdf")
hello(c)
c.showPage()
c.save()

The image I am trying to add is hereenter image description here

Can somebody advice why the black line on the left have appeared, and how to fix it?


Solution

  • The problem isn't with a border, rather your chessboard has transparent pixels on the right and bottom sides, and reportlab isn't recognizing the alpha channel and is painting the transparent section as black:

    enter image description here

    Using mask='auto' tells drawImage to use the alpha channel in your PNG, so the background shows through:

    c.drawImage("./media/files/1.png", 0, 600, 350, 350, mask='auto')