根据image.coordinates - Python在单个PDF中插入多个图像

-2 python pdf image reportlab

我有一个图像路径和相应的坐标作为字典.例如:{'coords':[u'530,88,592,99'],'filepath':1.jpg},{'coords':[u'7,12,152,85'],'filepath':2.jpg },{'coords':[u'448,12,594,86'],'filepath':3.jpg}我想根据图像的相对坐标生成包含所有图像的单个PDF.我想,有可能使用reportlab,但没有最初的运气.提前致谢.

itz*_*nTV 6

你可以用它reportlab来达到这个目的.drawImage可用于图像绘制Pdf.

drawImage 参数.

drawImage(image, x, y, width=None, height=None, mask=None, preserveAspectRatio=False, anchor='c')
Run Code Online (Sandbox Code Playgroud)
  • "image"可以是图像文件名或ImageReader对象.
  • x和y定义要绘制的图像的左下角
  • 如果给出宽度和高度,则将拉伸图像以填充由(x,y,x +宽度,y高度)限定的给定矩形.

示例程序:

from reportlab.pdfgen import canvas
from reportlab.lib.units import inch

c = canvas.Canvas("test.pdf")
# move the origin up and to the left
c.translate(inch,inch)
c.setFillColorRGB(1,0,1)
c.drawImage("image1.jpg", 10, 10, 50, 50)
c.drawImage("image2.jpg", 60, 60, 50, 50)

c.showPage()
c.save()
Run Code Online (Sandbox Code Playgroud)

该程序应生成一个pdf test.pdf,其中包含相应坐标中的两个图像.现在您可以解析coords字典中的坐标和图像.

让:

 coords_dict = {'coords': [u'7,12,152,85'], 'filepath': 2.jpg}
Run Code Online (Sandbox Code Playgroud)

然后drawImage你的dict应该是

 c.drawImage(
    coords_dict["filepath"], 
    coords_dict["coords"][0], 
    coords_dict["coords"][1],
    coords_dict["coords"][3]-coords_dict["coords"][0],
    coords_dict["coords"][4]-coords_dict["coords"][2])
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请检查

  • 如果OP没有投票,至少我这样做......向那些投入如此少的努力的人展示一个有效的例子,表明积极的态度;). (2认同)