0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Pythonで複数の画像を一気に270度回転してまとめてPDFへ出力

Last updated at Posted at 2023-03-27

Pythonで複数の画像を一気に270度回転してまとめてPDFへ出力

複数の画像ファイルを一気に270度回転して、1つのPDFファイルに出力するPythonコードなり。

家庭用スキャナで、ガンガン横向き資料をスキャンしたときに愛用。

回転不要な場合は、rotateImageの呼び出しをコメントアウトするだけ。

import img2pdf
import os
from PIL import Image

def rotateImage(folder, image_file):
    im = Image.open(image_file)
    im_rotate = im.rotate(270, resample=Image.BICUBIC, expand=True)
    filename = os.path.basename(image_file)
    filename = f"{folder}/rotate_{filename}"
    im_rotate.save(filename)
    return filename

if __name__ == '__main__':
    pdf_filename = "d:/images/output.pdf" # 出力先PDFファイル名
    folder = "d:/images/" # 入力元の画像フォルダ
    extension  = ".jpg" # 処理対象の拡張子

    image_files = []
    for filename in os.listdir(folder):
        if filename.lower().endswith(extension):
            image_file = os.path.join(folder, filename)
            image_file = rotateImage(folder, image_file) # 画像を270度回転
            image_files.append(image_file)
    image_files.sort()
    with open(pdf_filename, "wb") as pdf_file:
        pdf_file.write(img2pdf.convert(image_files))
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?