2

I am wondering if there is an easy way to combine multiple png images into a single pdf in python. I want each image to be a single page in the pdf. Is pypdf the best library to use for this? Any help would be greatly appreciated.

Thanks!

1
  • In my experience, ImageMagick has always provided me what I needed with a few lines of code. Commented May 10, 2015 at 18:15

5 Answers 5

5
from PIL import Image

image1 = Image.open(r'C:/Users/uidg3972/Desktop/New folder/Gopi24.jpg')
image2 = Image.open(r'C:/Users/uidg3972/Desktop/New folder/Gopi-1.jpg')
image3 = Image.open(r'C:/Users/uidg3972/Desktop/New folder/SNP_1291.jpg')

im1 = image1.convert('RGB')
im2 = image2.convert('RGB')
im3 = image3.convert('RGB')

imagelist = [im1,im2,im3]

im1.save(r'C:/Users/uidg3972/Desktop/New folder/mergedImages.pdf',save_all=True, 
append_images=imagelist)
Sign up to request clarification or add additional context in comments.

3 Comments

Do try the above code, i just tried it. It worked well..... Path of the image should be '/' not '\' symbol. Usually we make a mistake in backslash button try with forward slash..! (Same as URL links in any of web-pages kind of)
! pip install pillow and ! pip install image and finally you need .... ! pip in stall img2pdf to run the above code smoothly....! let me if any questions or doubts..!! This is my first contribution, please support and motivate. Thanks ....
should be imagelist = [im2,im3] otherwise im1 is duplicated
3

I've got the same problem.

So I've created python function to unite multiple pictures in one pdf. Code is available at my github page on https://github.com/wizard1989/Unite-multiple-pictures-into-pdf. It uses "reportlab".

Code is based on answers from the following links:
Create PDF from a list of images
Combining multiple pngs in a single pdf in python
png images to one pdf in python
How can I convert all JPG files in a folder to PDFs and combine them? https://www.blog.pythonlibrary.org/2012/01/07/reportlab-converting-hundreds-of-images-into-pdfs/

Here is example of how to unite images into pdf.

We have folder "D:\pictures" with pictures of types png and jpg. We want to create file pdf_with_pictures.pdf out of them and save it in the same folder.

outputPdfName = "pdf_with_pictures"
pathToSavePdfTo = "D:\\pictures"
pathToPictures = "D:\\pictures"
splitType = "none"
numberOfEntitiesInOnePdf = 1
listWithImagesExtensions = ["png", "jpg"]
picturesAreInRootFolder = True
nameOfPart = "volume"

unite_pictures_into_pdf(outputPdfName, pathToSavePdfTo, pathToPictures, splitType, numberOfEntitiesInOnePdf, listWithImagesExtensions, picturesAreInRootFolder, nameOfPart)

Comments

2

I've recently found img2pdf, very useful and easy to use... the code is so simply as following (and you can complicate it with some others additional parameters):

img2pdf *png -o output_all_pngs.pdf

I hope it helps you!

Comments

1

There's a python port to WKHtmlToPdf:

https://pypi.python.org/pypi/wkhtmltopdf/0.1

Easy to create page breaks between img tags in an html doc using css which you can pass to this lib.

http://wkhtmltopdf.org/usage/wkhtmltopdf.txt

Comments

0

This python code converts multiple png images to single pdf in A4 format:

i = 0
image_list = []
for x in range(0,5):
    image = Image.open(f'/content/image{i}.png')
    image = image.resize((1250,800))
    im = Image.new('RGB',
              (1240 , 1754), 
              (255, 255, 255))
    im.paste(image,image.getbbox())
    image_list.append(im)
    i+=1

image_list[0].save(r'/content/pdfs.pdf', 'PDF', quality=100, 
save_all=True, append_images=image_list[1:])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.