0
1

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.

PDFから画像抽出して一括保存するプログラム書いた!

Last updated at Posted at 2021-10-14

概要

某レイヤーさんの写真集がPDF形式の画像ファイルで販売されているけど、そのままだと不便なので画像だけぶっこ抜いて保存するようにしました

使用する言語、外部ライブラリなど

コード

# -*- coding: utf-8 -*-
from pathlib import Path
import fitz

# 読み込むPDFファイル
PDF_SOURCE: str = "PATH\\TO\\FILE.pdf"
# 画像の保存先ディレクトリ
DESTINATION_SAVE_TO: str = "SAVE\\TO\\DIRNAME"

pdf = fitz.open(PDF_SOURCE)
# 保存先ディレクトリが存在しない場合、作成
if not Path(DESTINATION_SAVE_TO).exists():
    Path(DESTINATION_SAVE_TO).mkdir()

# ページごとに読み込んで画像を保存する
for i in pdf:
    IMAGE = Path(Path(DESTINATION_SAVE_TO)).joinpath(f"{i.number}.png")
    pix = i.get_pixmap()
    pix.save(IMAGE)

参考にしたページ

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?