LoginSignup
0
0

More than 1 year has passed since last update.

jpg を取り込んだ PDF の作成 (python3)

Last updated at Posted at 2018-11-19

jpg を取り込んで次のような PDF を作成します。
pdf_nov1901.png

元の jpg です。
fuji_mod.jpg

画像の特性

$ identify fuji_mod.jpg 
fuji_mod.jpg JPEG 320x238 320x238+0+0 8-bit sRGB 17971B 0.010u 0:00.000

Arch Linux でのライブラリーのインストール

sudo pacman -S python-pillow
sudo pacman -S python-reportlab
jpg_import.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
#	jpg_import.py
#
#						Nov/19/2018
#
# ------------------------------------------------------------------
import	sys
from PIL import Image
#
from reportlab.pdfgen import canvas
from reportlab.pdfbase.pdfmetrics import registerFont
from reportlab.pdfbase.cidfonts import UnicodeCIDFont
# ------------------------------------------------------------------
sys.stderr.write ("*** 開始 ***\n")
#
font_mincho = "HeiseiMin-W3"
#
registerFont(UnicodeCIDFont (font_mincho))
#
pdf_canvas = canvas.Canvas("fuji.pdf")
#
ypos = 800
pdf_canvas.setFont(font_mincho, 30)
str_out = "富士山の写真です。"
pdf_canvas.drawString(100, ypos, str_out)
#
image =Image.open('fuji_mod.jpg')
pdf_canvas.drawInlineImage(image,100,ypos - 260)
#
str_out="2018年11月19日"
pdf_canvas.setFont(font_mincho, 15)
pdf_canvas.drawString(300, ypos - 300, str_out)
#
pdf_canvas.showPage()
pdf_canvas.save()
#
sys.stderr.write ("*** 終了 ***\n")
# ------------------------------------------------------------------

実行結果

$ ./jpg_import.py
*** 開始 ***
*** 終了 ***

次のバージョンで確認しました。

$ python --version
Python 3.10.4
0
0
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
0