Last active
November 4, 2020 00:50
-
-
Save saccadic/3fda3397344f414660bfd4c90f39f58c to your computer and use it in GitHub Desktop.
複数の画像をPDFに統合するスクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import pathlib | |
| from tqdm import tqdm | |
| from PIL import Image | |
| import img2pdf | |
| # !pip install pathlib tqdm Pillow img2pdf | |
| def ConvertImage2Pdf(pdfFileName,imageDirPath,ext="png"): | |
| file = pathlib.Path(imageDirPath) | |
| pathList = list(file.glob("*."+ext)) | |
| with open(pdfFileName, "wb") as files: | |
| fileList = [] | |
| for i in tqdm(range(len(pathList))): | |
| pngPath = str(pathList[i].absolute()) | |
| jpegPath = str(pathList[i].absolute().with_suffix(".jpeg")) | |
| Image.open(pngPath).convert("RGB").save(jpegPath, 'JPEG') | |
| fileList.append(jpegPath) | |
| files.write(img2pdf.convert(fileList)) | |
| def CleanDirectroy(target_dir): | |
| print("Clean up > "+target_dir) | |
| file = pathlib.Path(target_dir) | |
| pathList = list(file.glob("*")) | |
| for i in range(len(pathList)): | |
| path = pathList[i] | |
| os.remove(path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment