Code for How to Extract Images from PDF in Python Tutorial

View on Github

import fitz # PyMuPDF
import io
from PIL import Image

# file path you want to extract images from
file = "1710.05006.pdf"
# open the file
pdf_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}")
        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 =
        # save it to local disk"image{page_index+1}_{image_index}.{image_ext}", "wb"))