3

i'm writing a program which takes all the pictures in a given folder and aggregates them into a pdf. The problem I have is that when the images are drawn, they are bigger in size and are rotated to the left oddly. I've searched everywhere, havent found anything even in the reportlab documentation.

Here's the code:

import os
from PIL import Image
from PyPDF2 import PdfFileWriter, PdfFileReader
from reportlab.pdfgen import canvas
from reportlab.lib.units import cm
from StringIO import StringIO


def main():
    images = image_search()
    output = PdfFileWriter()
    for image in images:
        Image_file = Image.open(image)     # need to convert the image to the specific size first.
    width, height = Image_file.size
    im_width = 1 * cm
    # Using ReportLab to insert image into PDF
    watermark_str = "watermark" + str(images.index(image)) + '.pdf'
    imgDoc = canvas.Canvas(watermark_str)

    # Draw image on Canvas and save PDF in buffer
    # define the aspect ratio first
    aspect = height / float(width)

    ## Drawing the image
    imgDoc.drawImage(image, 0,0, width = im_width, height = (im_width * aspect))    ## at (399,760) with size 160x160
    imgDoc.showPage()
    imgDoc.save()
    # Get the watermark file just created
    watermark = PdfFileReader(open(watermark_str, "rb"))

    #Get our files ready

    pdf1File = open('sample.pdf', 'rb')
    page = PdfFileReader(pdf1File).getPage(0)
    page.mergePage(watermark.getPage(0))


    #Save the result

    output.addPage(page)
    output.write(file("output.pdf","wb"))

#The function which searches the current directory for image files.
def image_search():
    found_images = []
    for doc in os.listdir(os.curdir):
        image_ext = ['.jpg', '.png', '.PNG', '.jpeg', '.JPG']
        for ext in image_ext:
            if doc.endswith(ext):
                found_images.append(doc)
    return found_images

main()

I also tried scaling and specifying the aspect ratio using the im_width variable, which gave the same output.

B8vrede
  • 4,432
  • 3
  • 27
  • 40
opeonikute
  • 494
  • 1
  • 4
  • 15
  • I just tried running your code and it seems to do the job just fine. The output is my `sample.pdf` with the image I have in my folder drawn in the bottom left corner with a width of 1cm. I tried this with both a reportlab generated pdf as well as a random PDF I pulled of the internet... So the issue might has to do with your reportlab or PyPDF2 versions or the image/pdf. – B8vrede Mar 10 '16 at 08:19
  • It's working but the image size of 1962x2362 isn't merged onto the pdf properly. It's turned to one side and only a part of it is showing. @B8vrede – opeonikute Mar 10 '16 at 09:14
  • That's weird, I just tried it with a image of that exact dimensions and again it worked just fine. Did you try merging it over a different PDF? There is a small chance that the PDF your are merging the images on to is actually the one that is rotated. – B8vrede Mar 10 '16 at 10:09
  • I noticed you said your output is sample.pdf, my output is output.pdf. Is there a chance that's the reason we aren't getting the same results? @B8vrede – opeonikute Mar 10 '16 at 10:15
  • Oh no that was just a mix up, I was referring to the fact that `output.pdf` looks exactly like `sample.pdf` with the only difference that there is a small image in the bottom left corner in `output.pdf`. But could check something does your `watermark0.pdf`(and every other number after that) contain the images in the correct way? – B8vrede Mar 10 '16 at 10:19
  • No they don't. The problem is with the way the images are drawn on the canvas. I want the image to start from the top left and be scaled down to fit the A4 paper size – opeonikute Mar 10 '16 at 10:23
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/105884/discussion-between-onikute-opeyemi-o-and-b8vrede). – opeonikute Mar 10 '16 at 10:27

1 Answers1

4

After a little bit of confusion about your goal I figured out that the goal is to make a PDF overview of the images in the current folder. To do so we actual don't need PyPDF2 as Reportlab offers everything we need for this.

See the code below with the comments as guidelines:

def main():
    output_file_loc = "overview.pdf"
    imgDoc = canvas.Canvas(output_file_loc)
    imgDoc.setPageSize(A4) # This is actually the default page size
    document_width, document_height = A4

    images = image_search()
    for image in images:
        # Open the image file to get image dimensions
        Image_file = Image.open(image)
        image_width, image_height = Image_file.size
        image_aspect = image_height / float(image_width)

        # Determine the dimensions of the image in the overview
        print_width = document_width
        print_height = document_width * image_aspect

        # Draw the image on the current page
        # Note: As reportlab uses bottom left as (0,0) we need to determine the start position by subtracting the
        #       dimensions of the image from those of the document
        imgDoc.drawImage(image, document_width - print_width, document_height - print_height, width=print_width,
                         height=print_height)

        # Inform Reportlab that we want a new page
        imgDoc.showPage()

    # Save the document
    imgDoc.save()
B8vrede
  • 4,432
  • 3
  • 27
  • 40