Created
September 17, 2018 13:22
-
-
Save atuyosi/677071b54b3a54f2586666059113dbad to your computer and use it in GitHub Desktop.
連番のPNG画像をまとめてPDF化するスクリプト(A5)
This file contains 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
#! /usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
from reportlab.pdfgen import canvas | |
from reportlab.pdfbase import pdfmetrics | |
# from reportlab.rl_config import defaultPageSize | |
from reportlab.lib.pagesizes import A5, landscape | |
from reportlab.pdfbase.cidfonts import UnicodeCIDFont | |
from reportlab.lib.units import cm | |
# Pillow | |
from PIL import Image | |
from pathlib import Path | |
class PdfConfig: | |
# ほとんど不要 | |
title = "No title" | |
author = "" | |
subject = "" | |
default_fontname = "HeiseiKakuGo-W5" | |
image_resolution = 300 | |
jpeg_quality = 95 | |
font_adjustment = 0.85 | |
image_embeded = True | |
def __init__(self): | |
pass | |
if __name__ == '__main__' : | |
output_filename = 'output_new.pdf' #出力ファイル名固定 | |
config = PdfConfig() | |
newPdfPage = canvas.Canvas(output_filename) | |
newPdfPage.setPageSize(A5) | |
newPdfPage.saveState() # 念の為 | |
newPdfPage.setAuthor(config.author) | |
newPdfPage.setTitle(config.title) | |
newPdfPage.setSubject(config.subject) | |
# 同じディレクトリのoutputディレクトリにPNG形式の画像があり、ファイル名が連番という前提(image-001.png...) | |
pl = Path('./output').glob('image-*.png') | |
# tiff file, PDF | |
for i, p in enumerate(sorted(pl)): | |
# 画像のサイズを確認するために必要 | |
with p.open('rb') as file : | |
image = Image.open(file) | |
imagefilename = str(p) | |
print(f"start page: {i}") | |
print("image size: {}".format(image.size)) | |
image_width , image_height = image.size | |
page_size = {} | |
newPdfPage.setPageSize(A5) | |
page_size['width'], page_size['height'] = A5 | |
image_offset_x = 0 | |
image_offset_y = 0 | |
print("page size: {0}, {1}".format(page_size['width'], page_size['height'])) | |
newPdfPage.drawImage(imagefilename, 0+image_offset_x, 0+image_offset_y, width=page_size['width'], | |
height=page_size['height'], preserveAspectRatio=True, anchor='c') | |
newPdfPage.showPage() | |
newPdfPage.save() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment