Convert images in multiple folders to different pdfs for each folder at once

Introduction

I have a lot of images, but if I try to back them up as they are, the transfer time is slow, probably because it becomes a random wallet. So I decided to make it a sequential file. Even if you try to zip it, the upper limit is 4GB. If you try to make all one file using 7zip, it is too big and difficult to handle Even if you try to convert from a batch file at once, some folders may not be created properly due to bad folder names. Then I searched for a program with the intention of converting it to pdf.

Convert multiple images to PDF file with python I found this, but it was a little difficult to use.

This program is a little easier to use. The basic code remains the same.

Image summary
├── 2015
│   ├── img1.jpg
│   ├── img2.jpg
│   ├── img3.jpg
├── 2016
│   ├── img10.jpg
│   ├── img11.jpg
│   ├── img12.jpg
├── 20170
│   ├── img21.jpg
│   ├── img22.jpg
│   ├── img23.jpg
├── 2018
│   ├── img42.jpg
│   ├── img125.jpg
│   ├── img1000.jpg

When there was such a folder

Image summary
├── 2015.pdf
├── 2016.pdf
├── 2017.pdf
├── 2018.pdf

Use this when you want to convert to pdf at once.

environment

Anaconda python3.7 windows10 Probably does not work on mac, Linux

Library import

pip install img2pdf

code

makingpdf1.1.py


import img2pdf
from pathlib import Path



#Single processing
def ImageToPdf(outputpath, imagepath):
    '''
    outputpath: pathlib.Path()
    imagepath: pathlib.Path()
    '''
    lists = list(imagepath.glob("**/*"))#Search in a single folder
    #Create pdf
    with open(outputpath,"wb") as f:
        #jpg,Combine only png files into pdf
        #Pathlib.WindowsPath()Error if not converted to string type
        f.write(img2pdf.convert([str(i) for i in lists if i.match("*.jpg ") or i.match("*.png ") or i.match("*.jpeg ")]))
    print(outputpath.name + " :Done")

#Loop through multiple folders
while True:
    #Working folder
    base_path = input("Folder d&P")
    base_path = base_path.strip(" \' ")
    #Extract only the directories in the working folder
    glob = Path(base_path).glob("*")
    imagefolderlist = list([item for item in list(glob) if item.is_dir()])
    #Specify the same name as the specified directory in outputpath
    outputpathlist = list([item.with_name(item.name + ".pdf") for item in imagefolderlist])
    #Perform loop processing
    for outputpath,imagepath in zip(outputpathlist,imagefolderlist):
        try:
            ImageToPdf(outputpath,imagepath)
        except:
            pass


change point

Convert multiple images to PDF file with python In this program, only one folder can be specified, and the path must be written in advance, which is difficult to use. Therefore, I dragged and dropped the folder so that it could be converted more and more. Also, since it did not support the .jpeg extension, it was supported.

How to use

Image summary
├── 2015
│   ├── img1.jpg
│   ├── img2.jpg
│   ├── img3.jpg
├── 2016
│   ├── img10.jpg
│   ├── img11.jpg
│   ├── img12.jpg
├── 20170
│   ├── img21.jpg
│   ├── img22.jpg
│   ├── img23.jpg
├── 2018
│   ├── img42.jpg
│   ├── img125.jpg
│   ├── img1000.jpg

When there was such a folder

When you run the code

Folder d&P

Will appear, so drag and drop the folder ___ that contains the folder you want to make into ___pdf and press enter. In the above example, drag and drop the "Image Summary" folder instead of the "2018" folder where you want to convert the contents to pdf. Then, pdf will be created in the "Image Summary" folder.

problem

In the case of png file, it cannot be converted to pdf if it contains an alpha channel.

Summary

Useful when converting images divided by folders to pdf at once.

Recommended Posts

Convert images in multiple folders to different pdfs for each folder at once
Convert PDFs to images in bulk with Python
Use gitlabApi to add members to multiple groups in gitlab at once
Convert memo at once with Python 2to3
I made a CLI tool to convert images in each directory to PDF
Convert multiple CSVs to each Excel sheet
Convert multiple proto files at once with python
Register multiple self-made styles in Word at once
Beginners try to convert Word files to PDF at once
[Linux] Grep multiple gzip files in a directory at once
A simple way to avoid multiple for loops in Python
How to define multiple variables in a python for statement
Batch convert all xlsx files in the folder to CSV files
Convert images passed to Jason Statham-like in Python to ASCII art
Convert Excel file to text in Python for diff purposes