1634146200
このチュートリアルでは、PDFファイルから画像を抽出し、PyMuPDFおよびPillowライブラリを使用してローカルディスクに保存するPythonコードを記述します。
PyMuPDFを使用すると、PDF、XPS、OpenXPS、epub、およびその他の多くの拡張機能にアクセスできます。Windows、Mac OSX、Linuxを含むすべてのプラットフォームで実行する必要があります。
枕と一緒にインストールしましょう:
pip3 install PyMuPDF Pillow
新しいPythonファイルを開いて、始めましょう。まず、ライブラリをインポートしましょう。
import fitz # PyMuPDF
import io
from PIL import Image
このPDFファイルでこれをテストしますが、PDFファイルを自由に持ってきて、現在の作業ディレクトリに置いて、ライブラリにロードしましょう。
# file path you want to extract images from
file = "1710.05006.pdf"
# open the file
pdf_file = fitz.open(file)
すべてのページから画像を抽出する必要があるため、使用可能なすべてのページを繰り返し処理し、各ページのすべての画像オブジェクトを取得する必要があります。次のコードはそれを行います。
# iterate over PDF pages
for page_index in range(len(pdf_file)):
# get the page itself
page = pdf_file[page_index]
image_list = page.getImageList()
# printing number of images found in this page
if image_list:
print(f"[+] Found a total of {len(image_list)} images in page {page_index}")
else:
print("[!] No images found on page", page_index)
for image_index, img in enumerate(page.getImageList(), start=1):
# get the XREF of the image
xref = img[0]
# extract the image bytes
base_image = pdf_file.extractImage(xref)
image_bytes = base_image["image"]
# get the image extension
image_ext = base_image["ext"]
# load it to PIL
image = Image.open(io.BytesIO(image_bytes))
# save it to local disk
image.save(open(f"image{page_index+1}_{image_index}.{image_ext}", "wb"))
関連: PythonでPDFを画像に変換する方法。
getImageList()メソッドを使用して、使用可能なすべての画像オブジェクトをその特定のページのタプルのリストとして一覧表示しています。画像オブジェクトのインデックスを取得するには、返されるタプルの最初の要素を取得するだけです。
その後extractImage()
、画像の拡張子などの追加情報とともに画像をバイト単位で返すメソッドを使用します。
最後に、画像バイトをPIL画像インスタンスに変換save()
し、引数としてファイルポインターを受け入れるメソッドを使用してローカルディスクに保存します。対応するページと画像インデックスを使用して画像に名前を付けるだけです。
スクリプトを実行すると、次の出力が得られます。
[!] No images found on page 0
[+] Found a total of 3 images in page 1
[+] Found a total of 3 images in page 2
[!] No images found on page 3
[!] No images found on page 4
画像も現在のディレクトリに保存されます。
了解しました。画質を損なうことなく、そのPDFファイルから画像を正常に抽出できました。ライブラリの動作の詳細については、ドキュメントを参照することをお勧めします。
ここで完全なコードを確認してください。
リンク: https://www.thepythoncode.com/article/extract-pdf-images-in-python
1634146200
このチュートリアルでは、PDFファイルから画像を抽出し、PyMuPDFおよびPillowライブラリを使用してローカルディスクに保存するPythonコードを記述します。
PyMuPDFを使用すると、PDF、XPS、OpenXPS、epub、およびその他の多くの拡張機能にアクセスできます。Windows、Mac OSX、Linuxを含むすべてのプラットフォームで実行する必要があります。
枕と一緒にインストールしましょう:
pip3 install PyMuPDF Pillow
新しいPythonファイルを開いて、始めましょう。まず、ライブラリをインポートしましょう。
import fitz # PyMuPDF
import io
from PIL import Image
このPDFファイルでこれをテストしますが、PDFファイルを自由に持ってきて、現在の作業ディレクトリに置いて、ライブラリにロードしましょう。
# file path you want to extract images from
file = "1710.05006.pdf"
# open the file
pdf_file = fitz.open(file)
すべてのページから画像を抽出する必要があるため、使用可能なすべてのページを繰り返し処理し、各ページのすべての画像オブジェクトを取得する必要があります。次のコードはそれを行います。
# iterate over PDF pages
for page_index in range(len(pdf_file)):
# get the page itself
page = pdf_file[page_index]
image_list = page.getImageList()
# printing number of images found in this page
if image_list:
print(f"[+] Found a total of {len(image_list)} images in page {page_index}")
else:
print("[!] No images found on page", page_index)
for image_index, img in enumerate(page.getImageList(), start=1):
# get the XREF of the image
xref = img[0]
# extract the image bytes
base_image = pdf_file.extractImage(xref)
image_bytes = base_image["image"]
# get the image extension
image_ext = base_image["ext"]
# load it to PIL
image = Image.open(io.BytesIO(image_bytes))
# save it to local disk
image.save(open(f"image{page_index+1}_{image_index}.{image_ext}", "wb"))
関連: PythonでPDFを画像に変換する方法。
getImageList()メソッドを使用して、使用可能なすべての画像オブジェクトをその特定のページのタプルのリストとして一覧表示しています。画像オブジェクトのインデックスを取得するには、返されるタプルの最初の要素を取得するだけです。
その後extractImage()
、画像の拡張子などの追加情報とともに画像をバイト単位で返すメソッドを使用します。
最後に、画像バイトをPIL画像インスタンスに変換save()
し、引数としてファイルポインターを受け入れるメソッドを使用してローカルディスクに保存します。対応するページと画像インデックスを使用して画像に名前を付けるだけです。
スクリプトを実行すると、次の出力が得られます。
[!] No images found on page 0
[+] Found a total of 3 images in page 1
[+] Found a total of 3 images in page 2
[!] No images found on page 3
[!] No images found on page 4
画像も現在のディレクトリに保存されます。
了解しました。画質を損なうことなく、そのPDFファイルから画像を正常に抽出できました。ライブラリの動作の詳細については、ドキュメントを参照することをお勧めします。
ここで完全なコードを確認してください。
リンク: https://www.thepythoncode.com/article/extract-pdf-images-in-python