使用 ReportLab 添加到 PDF 时 PNG 模糊

PNG fuzzy when added to PDF with ReportLab

我正在尝试使用 ReportLab 在 Python 中创建 PDF。我需要调整 PNG 图像的大小以正确适合页面。当我调整图像大小时,它们在 Microsoft Photos 中查看或拖放到 word 文档中时看起来很好,但是当它们放入 PDF 中时它们非常模糊。

This is the scaled image 看起来很脆。

This is a screen grab of the pdf 看起来很模糊。

这是我目前使用的代码

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()

如果有人对如何获得清晰的图像有任何建议,我们将不胜感激!

调整大小代码来自这里:How do I resize an image using PIL and maintain its aspect ratio?

如果有人偶然发现了这一点,我最终采用的方法是混合使用 Python 和 LaTex,因为 Latex 可以很好地处理 PDF 和图像,从而产生干净、清晰的图像。