LoginSignup
8

More than 5 years have passed since last update.

PythonでKindle用自炊PDFを作ろうと実験

Posted at

スキャナとかで取り込んだjpgファイルをPythonを使って何とか出来ないかと。
まずは必要なモジュールをインポート。

$ pip install reportlab

あとPILも必要なのでインストール。

$ brew install libjpeg

http://www.pythonware.com/products/pil/
上記URLからPILをダウンロード。
解凍後、cdでディレクトリを移動。

$ cd Imaging-1.1.7
$ sudo python setup.py install

とりあえず現時点で思いつきで出来たスクリプトが以下。

pdf.py

#!/user/bin/env python
# -*- coding: utf-8 -*-
from reportlab.pdfgen import canvas
from reportlab.rl_config import defaultPageSize

canvas = canvas.Canvas("sample.pdf")
PAGE_WIDTH = defaultPageSize[0]
PAGE_HEIGHT = defaultPageSize[1]
canvas.drawInlineImage("test.jpg", 0, 0, PAGE_WIDTH, PAGE_HEIGHT)

canvas.save()
print ("Success")

PAGE_HEIGHT = defaultPageSize[1]
でページサイズをA4に指定。

実行ファイルであるpdf.pyと同一ディレクトリのtest.jpgを読み込んでPDFにする。
ただそれだけ。
もっと大きいファイルを変換したらどうなるかとかはやってみないとわからない。
840×1200のjpgファイルはA4に収まることを確認済み。

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
8