Super safe, Python synthesizes multiple pictures to PDF format

In daily life, we often encounter the need to submit proof materials on the front and back of the ID card, and most of these websites only accept pdf format. At this time, we need to synthesize two pictures on the front and back of the ID card into a pdf file.

Under the macOS system, the preview software can easily do this. At the same time, open the picture to a preview window and click export PDF to export it successfully. However, Windows system does not have such convenient software to achieve this. There are many websites that synthesize PDF on the Internet, but these websites all need to upload PDF for synthesis, which I personally think is very unsafe.

Therefore, the safest way is to write a Python script to realize the synthesis function.

1. Preparation

Before you start, make sure that Python and pip have been successfully installed on your computer. If not, install them.

(optional 1) if you use Python for data analysis, you can directly install Anaconda: anaconda, a good helper of Python data analysis and mining, which has built-in Python and pip

(optional 2) in addition, it is recommended to use the VSCode editor, which has many advantages: the best partner of Python Programming - VSCode detailed guide.

Please choose one of the following ways to enter the command to install dependencies:

  1. Windows environment opens Cmd (start run CMD).
  2. Open terminal in MacOS environment (Command + space, enter Terminal).
  3. If you use the VSCode editor or pychart, you can directly use the Terminal at the bottom of the interface
pip install pillow

2. Synthesis principle

The pilot module, namely PIL: Python Imaging Library, is already the de facto image processing standard library of the python platform. PIL is very powerful, but the API is very simple and easy to use. Through it, we can easily manipulate images and export them to different formats.

Let's start with a simple example. We will open a picture and save it in pdf format:

from PIL import Image
import os


def convert_img_pdf(filepath, output_path):
    """
Convert picture to pdf format

Args:
    filepath (str): File path
    output_path (str): Output path
"""
output = Image.open(filepath)
output.save(output_path, "pdf", save_all=True)

if name == "main":
convert_img_pdf("1.jpeg", "./test.pdf")

Use a random picture to test:

After running the code, it is successfully converted to PDF file:

This conversion is completed in a few lines of code, which is much safer than those websites that upload photos to the cloud.

3. Synthesis of multiple photos into PDF

output.save(pdfFilePath, "pdf", save_all=True, append_images=sources)

With the basic knowledge of transforming photos into PDF, it is very simple to understand the following code of multi image synthesis PDF.

In fact, it is used A special save parameter of APP end_ images:

output.save(pdfFilePath, "pdf", save_all=True, append_images=sources)

By storing all the images in a "sources" array, we can easily synthesize these images into PDF.

from PIL import Image import os

def combine_imgs_pdf(folder_path, pdf_file_path):
    """
All pictures in the composition folder are pdf

Args:
    folder_path (str): Source folder
    pdf_file_path (str): Output path
"""
files = os.listdir(folder_path)
png_files = []
sources = []
for file in files:
    if 'png' in file or 'jpg' in file:
        png_files.append(folder_path + file)
png_files.sort()

output = Image.open(png_files[0])
png_files.pop(0)
for file in png_files:
    png_file = Image.open(file)
    if png_file.mode == "RGB":
        png_file = png_file.convert("RGB")
    sources.append(png_file)
output.save(pdf_file_path, "pdf", save_all=True, append_images=sources)
if __name__ == "__main__":
    folder = r"G:\certificates\\"
    pdfFile = r"G:\certificates\ID.pdf"
    combine_imgs_pdf(folder, pdfFile)

In this way, as long as you put your certificate photos in a folder and run this Python code, it can automatically synthesize these certificates into a PDF and output them to the path you specify. Very simple and convenient.

Keywords: Python

Added by Shad on Wed, 02 Mar 2022 12:39:33 +0200