简体   繁体   English

使用ReportLab添加到PDF时,PNG模糊

[英]PNG fuzzy when added to PDF with ReportLab

I'm trying to create PDF's in Python using ReportLab. 我正在尝试使用ReportLab在Python中创建PDF。 I need to resize the PNG image to correctly fit on the page. 我需要调整PNG图像的大小以正确适合页面。 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. 当我调整图像大小时,在Microsoft照片中查看或拖放到word文档时它们看起来很好,但是当它们放入PDF时它们非常模糊。

This is the scaled image which appears crisp. 这是缩放的图像 ,看起来很清晰。

This is a screen grab of the pdf which appears fuzzy. 这是一个看似模糊的PDF格式的屏幕抓取

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? 调整大小代码来自此处: 如何使用PIL调整图像大小并保持其宽高比?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM