4

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?

Community
  • 1
  • 1
Conner Leverett
  • 573
  • 2
  • 7
  • 21
  • Looks pretty crisp still, but the image in the PDF is larger than your scaled image. Use a higher resolution image. – Kenney Apr 11 '16 at 18:19
  • I don't know a great deal about PIL or Report Lab - but wondered if there was a way to set Interpolation on for this image (see Pdf Spec 8.9.5.3) – Jimmy Apr 13 '16 at 17:38

1 Answers1

0

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.

Conner Leverett
  • 573
  • 2
  • 7
  • 21