Insert multiple images in a single PDF according to image.coordinates - Python
Question: I have a image path's and corresponding coordinates as a dict. Ex: {'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} I would
like to generate a single PDF with all the images as per the relative-
coordinates of the images. I guess, it might be possible with reportlab, but
no initial luck. Thanks in advance.


Answer: You can use `reportlab` for this purpose. `drawImage` can be used for image
drawing in `Pdf`.

`drawImage` arguments.

    
    
    drawImage(image, x, y, width=None, height=None, mask=None, preserveAspectRatio=False, anchor='c')
    

  * “image” may be an image filename or an ImageReader object.
  * x and y define the lower left corner of the image you wish to draw
  * If width and height are given, the image will be stretched to fill the given rectangle bounded by (x, y, x+width, y-height).

**Example program:**

    
    
    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()
    

This program should generate a pdf `test.pdf` with two images in the
corresponding coordinates.Now you can parse coordinates and images from your
`coords` dictionary.

**Let:**

    
    
     coords_dict = {'coords': [u'7,12,152,85'], 'filepath': 2.jpg}
    

Then `drawImage` for your dict should be

    
    
     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])
    

For more info please
[check](http://www.reportlab.com/apis/reportlab/2.4/pdfgen.html)

