【问题标题】:PNG fuzzy when added to PDF with ReportLab使用 ReportLab 添加到 PDF 时 PNG 模糊
【发布时间】:2016-08-02 01:22:16
【问题描述】:

我正在尝试使用 ReportLab 在 Python 中创建 PDF。我需要调整 PNG 图像的大小以正确适合页面。当我调整图像大小时,它们在 Microsoft 照片中查看或拖放到 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?

【问题讨论】:

  • 看起来仍然很清晰,但 PDF 中的图像比您的缩放图像大。使用更高分辨率的图像。
  • 我不太了解 PIL 或 Report Lab - 但想知道是否有办法为此图像设置插值(参见 Pdf Spec 8.9.5.3)

标签: python pdf png reportlab


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-12
    • 2022-01-18
    • 1970-01-01
    • 2019-01-01
    相关资源
    最近更新 更多