4
0

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 3 years have passed since last update.

複数枚のHEIC画像をまとめて一つのPDFファイルに変換する(Python)

Last updated at Posted at 2021-01-26

##概要
iPhoneで撮影した授業の板書の画像にgoodnotes5で書き込みをしたい。
しかも、一つの科目につき複数枚の写真を撮るのでそれらは一つのノートにまとめたい。

iPhoneからMacにAirdropをしたときに、その画像形式を見ると.HEICという(謎)拡張子が使われていた。
.HEICを.jpegに変換し、goodnotes5で扱えるように全ての画像をpdf形式に変換しまとめる。

input)
複数のHEIC画像が入っているフォルダー

output)
全てのHEIC画像が含まれている一つのpdfファイル

##0)pipする

.sh
pip install img2pdf

他に使用するライブラリは全て標準ライブラリ(のはず)

##1)関数を定義

.py
import glob
import subprocess
import os
import img2pdf

def HeicToPdf(image_dir,output_pdf_name):
    if image_dir[-1]!='/':
        image_dir=image_dir+'/'
    output_pdf_path=image_dir+output_pdf_name
    jpg_path=image_dir+'jpg_dir'
    os.makedirs(jpg_path,exist_ok=True)
    files = glob.glob(image_dir+"*.HEIC")
    files = sorted(files)
    for file in files:
        jpg_file=file.replace(image_dir,jpg_path+"/")
        command = 'sips -s format jpeg ' + file +  ' --out ' + jpg_file.replace('.HEIC','.jpeg')
        subprocess.call(command, shell=True)
    files=[]
    files = glob.glob(jpg_path+"/*.jpeg")
    files = sorted(files)
    with open(output_pdf_path,"wb") as f:
        f.write(img2pdf.convert([str(i) for i in files]))
    print("Done")

##2)関数を使用する

.py
HeicToPdf("/Users/example_heic_dir","example_output.pdf")

##3)参考文献
1)Pythonを使ってHEICファイルをjpegに変換する
https://qiita.com/as-chapa/items/c3af6ab6ea4ce8349a90
2)pythonで複数画像をPDFファイルに変換する
https://qiita.com/meznat/items/31d947ed4826350fd9fa
3)Macのターミナルで簡単に画像処理できるsipsの使い方
https://qiita.com/livlea/items/53b755e5067d4ebc5b43
4)subprocessについてより深く(3系,更新版)
https://qiita.com/HidKamiya/items/e192a55371a2961ca8a4

4
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
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?