Here is the new PDF that contains all the images: Im1.save(r'C:\Users\Ron\Desktop\Test\myImages.pdf',save_all=True, append_images=imagelist)
Putting all the code components together: from PIL import Image I then created a new imagelist (excluding the first image): imagelist = įinally, I used to following syntax to save the PDF (note the ‘im1’ at the beginning): im1.save(r'C:\Users\Ron\Desktop\Test\myImages.pdf',save_all=True, append_images=imagelist) Next, I performed the conversion: im1 = nvert('RGB') Image4 = Image.open(r'C:\Users\Ron\Desktop\Test\image4.png') Image3 = Image.open(r'C:\Users\Ron\Desktop\Test\image3.png') Image2 = Image.open(r'C:\Users\Ron\Desktop\Test\image2.png') What if you have a list of images and you’d like to store all of them in a single PDF file?įor example, I added few more images under the following path:Ĭ:\Users\Ron\Desktop\Test image1 = Image.open(r'C:\Users\Ron\Desktop\Test\image1.png') Image1 = Image.open(r'C:\Users\Ron\Desktop\Test\image1.jpg')Ĭonvert List of Images to PDF using Python In that case, the only thing you’ll need to change is the file extension to ‘jpg’: from PIL import Image The same principles apply if you have JPEG images (rather than png). Run the code (adjusted to your paths), and the new PDF will be created at your specified location: Im1.save(r'C:\Users\Ron\Desktop\Test\myFirstImage.pdf') Image1 = Image.open(r'C:\Users\Ron\Desktop\Test\image1.png') So this is the full Python code that I used to convert the image to PDF (you’ll need to adjust the paths to reflect the location where the files are stored on your computer): from PIL import Image
If you’re a Windows user, you may follow this guide for the instructions to install a package using pip. To start, install the PIL package using the command below: pip install Pillow Steps to Convert Images to PDF using Python Step 1: Install the PIL package
Later, you’ll also see how to convert a list of images to PDF. Im1.save(r'path where the pdf will be stored\new file name.pdf') Image1 = Image.open(r'path where the image is stored\file name.png') To begin, here is a template that you can use to convert a png image to PDF using Python (for JPEG, use the file extension of ‘jpg’): from PIL import Image The PIL package will be used to accomplish this goal. In this short guide, you’ll see how to convert images to PDF using Python.