您可以为此目的使用reportlab。 drawImage可用于Pdf中的图像绘制。
drawImage 参数。
drawImage(image, x, y, width=None, height=None, mask=None, preserveAspectRatio=False, anchor='c')
- “image”可以是图像文件名或 ImageReader 对象。
- x 和 y 定义您要绘制的图像的左下角
- 如果给定宽度和高度,图像将被拉伸以填充以 (x, y, x+width, y-height) 为边界的给定矩形。
示例程序:
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()
此程序应生成一个 pdf test.pdf,其中包含对应坐标中的两个图像。现在您可以从 coords 字典中解析坐标和图像。
让:
coords_dict = {'coords': [u'7,12,152,85'], 'filepath': 2.jpg}
那么drawImage 你的字典应该是
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])
更多信息请check