【发布时间】: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)