When working and processing pictures in daily work, we often need to merge multiple pictures into a PDF file for ease of archiving, sharing or printing. Python provides a powerful image processing library that can easily achieve this task. This article will introduce in detail how to use Python to batch convert images into PDFs and include corresponding sample code to help you master this technique.
Preparation
Before you start, you need to make sure that you have installed the required Python libraries. The Pillow library will be used to process the images and the PyPDF2 library will be used to generate the PDF file.
Install Pillow and PyPDF2
You can use the pip command to install these libraries:
pip install Pillow PyPDF2
Convert images to PDF using Pillow
Pillow is a branch of the Python Imaging Library (PIL) and is a very powerful image processing library. It supports opening, manipulating, and saving images in multiple formats.
Convert single image to PDF
First, let’s see how to convert a single image to a PDF.
from PIL import Image def image_to_pdf(image_path, output_path): image = (image_path) pdf_path = output_path ('RGB').save(pdf_path) print(f"Images have been removed {image_path} Convert to PDF document {pdf_path}") # Exampleimage_to_pdf('', '')
In this example, an image is opened and converted to RGB mode, and then saved as a PDF file.
Convert multiple images to PDF
Next, merge multiple images into a PDF file.
from PIL import Image def images_to_pdf(image_paths, output_path): images = [(image).convert('RGB') for image in image_paths] images[0].save(output_path, save_all=True, append_images=images[1:]) print(f"Multiple images have been merged into PDF document {output_path}") # Exampleimage_paths = ['', '', ''] images_to_pdf(image_paths, 'merged_output.pdf')
In this example, first convert all the pictures to RGB mode and then save them as a PDF file using the save_all=True option.
Batch processing of pictures in folders
In practical applications, it may be necessary to batch convert all images in a folder into PDF. You can use the os library to iterate through all pictures in the folder.
import os from PIL import Image def folder_to_pdf(folder_path, output_path): image_paths = [] for file_name in (folder_path): if file_name.endswith(('jpg', 'jpeg', 'png')): image_paths.append((folder_path, file_name)) if image_paths: images = [(image).convert('RGB') for image in sorted(image_paths)] images[0].save(output_path, save_all=True, append_images=images[1:]) print(f"Folder has been added {folder_path} The pictures in PDF document {output_path}") else: print("No image file found in the folder") # Examplefolder_to_pdf('images_folder', 'output_folder.pdf')
In this example, iterate through all the pictures in the specified folder and merge them into a PDF file.
Process pictures in different formats
Sometimes, it is necessary to process pictures in different formats, such as PNG, BMP, etc. Pillow supports a variety of image formats, making it easy to process these images.
import os from PIL import Image def folder_to_pdf(folder_path, output_path): image_paths = [] for file_name in (folder_path): if file_name.endswith(('jpg', 'jpeg', 'png', 'bmp', 'tiff')): image_paths.append((folder_path, file_name)) if image_paths: images = [(image).convert('RGB') for image in sorted(image_paths)] images[0].save(output_path, save_all=True, append_images=images[1:]) print(f"Folder has been added {folder_path} The pictures in PDF document {output_path}") else: print("No image file found in the folder") # Examplefolder_to_pdf('images_folder', 'output_folder.pdf')
Add image compression and adjustments
When working with large numbers of images, the images may need to be compressed or resized to reduce the size of the PDF file.
import os from PIL import Image def resize_image(image, max_size): ratio = min(max_size / , max_size / ) new_width = int( * ratio) new_height = int( * ratio) return ((new_width, new_height), ) def folder_to_pdf(folder_path, output_path, max_size=1000): image_paths = [] for file_name in (folder_path): if file_name.endswith(('jpg', 'jpeg', 'png', 'bmp', 'tiff')): image_paths.append((folder_path, file_name)) if image_paths: images = [resize_image((image).convert('RGB'), max_size) for image in sorted(image_paths)] images[0].save(output_path, save_all=True, append_images=images[1:]) print(f"Folder has been added {folder_path} The pictures in PDF document {output_path}") else: print("No image file found in the folder") # Examplefolder_to_pdf('images_folder', 'output_folder.pdf')
In this example, a resize_image function is added to resize the image to a specified size.
Complete example
Below is a complete sample code that combines all the steps to realize the function of batch converting images into PDFs.
import os from PIL import Image def resize_image(image, max_size): ratio = min(max_size / , max_size / ) new_width = int( * ratio) new_height = int( * ratio) return ((new_width, new_height), ) def folder_to_pdf(folder_path, output_path, max_size=1000): image_paths = [] for file_name in (folder_path): if file_name.endswith(('jpg', 'jpeg', 'png', 'bmp', 'tiff')): image_paths.append((folder_path, file_name)) if image_paths: images = [resize_image((image).convert('RGB'), max_size) for image in sorted(image_paths)] images[0].save(output_path, save_all=True, append_images=images[1:]) print(f"Folder has been added {folder_path} The pictures in PDF document {output_path}") else: print("No image file found in the folder") # Examplefolder_to_pdf('images_folder', 'output_folder.pdf')
Summarize
This article details how to convert images into PDF files in batches using Python. With Pillow and PyPDF2 libraries, we can easily achieve this task. The article first introduces how to convert a single image into a PDF, and then shows how to merge multiple images into a PDF file. Next, we discussed how to batch process all pictures in the folder and process pictures in different formats, such as JPEG, PNG, BMP, etc. In addition, the article also provides methods to compress and resize images to reduce the size of the generated PDF files. Finally, combining these steps provides a complete sample code to help you quickly implement image-to-PDF conversion.
The above is the detailed content of Python's implementation of batch conversion of pictures into PDF. For more information about Python's pictures to PDF, please pay attention to my other related articles!