Search code examples
pythonpdfpngreportlab

PNG fuzzy when added to PDF with ReportLab


I'm trying to create PDF's in Python using ReportLab. I need to resize the PNG image to correctly fit on the page. When I resize the image they look fine when viewed in Microsoft Photos or dragged and dropped onto a word document, but when they are put in the PDF they are very fuzzy.

This is the scaled image which appears crisp.

This is a screen grab of the pdf which appears fuzzy.

This is the code I'm using so far

def resizeImage():
    basewidth = 500
    img = PIL.Image.open('test.png')
    wpercent = (basewidth/float(img.size[0]))
    hsize = int((float(img.size[1])*float(wpercent)))
    img = img.resize((basewidth,hsize), PIL.Image.ANTIALIAS)
    img.save('sompic.png')

def generatePDF():
    c = canvas.Canvas('template.pdf', pagesize=portrait(letter))
    #Header text
    c.setFont('Helvetica', 48, leading=None)
    c.drawCentredString(200, 300, "This is a pdf" )
    test = 'sompic.png'
    c.drawImage(test, 50,350, width=None, height=None)
    c.showPage()
    c.save()

resizeImage()
generatePDF()

If anyone has any suggestions on how to get a crisp image it would be greatly appreciated!

The resize code came from here: How do I resize an image using PIL and maintain its aspect ratio?


Solution

  • If anyone stumbles upon this, the route I ended up going with was using a mixture of Python and LaTex as Latex can deal very well with PDF's and images resulting in a clean, crisp image.