How to extract images from PDF in Python?
Last Updated :
09 Sep, 2024
The task in this article is to extract images from PDFs and convert them to Image to PDF and PDF to Image in Python.
To extract the images from PDF files and save them, we use the PyMuPDF library. First, we would have to install the PyMuPDF library using Pillow.
pip install PyMuPDF Pillow
PyMuPDF is used to access PDF files. To extract images from a PDF file, we need to follow the steps mentioned below-
- Import necessary libraries
- Specify the path of the file from which you want to extract images and open it
- Iterate through all the pages of the PDF and get all images and objects present on every page
- Use getImageList() method to get all image objects as a list of tuples
- To get the image in bytes and along with the additional information about the image, use extractImage()
Note: To download the PDF file click here.
Implementation:
Python
# STEP 1
# import libraries
import fitz # PyMuPDF
import io
from PIL import Image
# STEP 2
# file path you want to extract images from
file = "/content/pdf_file.pdf"
# open the file
pdf_file = fitz.open(file)
# STEP 3
# iterate over PDF pages
for page_index in range(len(pdf_file)):
# get the page itself
page = pdf_file.load_page(page_index) # load the page
image_list = page.get_images(full=True) # get images on the page
# printing number of images found in this page
if image_list:
print(f"[+] Found a total of {len(image_list)} images on page {page_index}")
else:
print("[!] No images found on page", page_index)
for image_index, img in enumerate(image_list, start=1):
# get the XREF of the image
xref = img[0]
# extract the image bytes
base_image = pdf_file.extract_image(xref)
image_bytes = base_image["image"]
# get the image extension
image_ext = base_image["ext"]
# save the image
image_name = f"image{page_index+1}_{image_index}.{image_ext}"
with open(image_name, "wb") as image_file:
image_file.write(image_bytes)
print(f"[+] Image saved as {image_name}")
Output:

Image to PDF and PDF to Image Conversion:
Image to PDF Conversion
Note: The image used here can be found here.
Python
import fitz
doc = fitz.open()
imgdoc = fitz.open('image.jpeg') # open image
pdfbytes = imgdoc.convert_to_pdf()
imgpdf = fitz.open("pdf", pdfbytes)
doc.insert_pdf(imgpdf)
doc.save('imagetopdf.pdf') # save file
First, we opened a blank document. Then we opened the image.
Now the image is converted to PDF using the convert_to_pdf() method.
After conversion, the image is appended to the empty doc which we created at starting. The document is saved after it has been appended.
Output:
PDF to Image Conversion
Note: We are using the sample.pdf for PDf to image conversion; to get the pdf, use the link below.
https://www.africau.edu/images/default/sample.pdf - sample.pdfÂ
Python
import fitz
doc = fitz.open('sample.pdf')
for page in doc:
pix = page.get_pixmap(matrix=fitz.Identity, dpi=None,
colorspace=fitz.csRGB, clip=None, alpha=True, annots=True)
pix.save("samplepdfimage-%i.jpg" % page.number) # save file
We used the get_pixmap() method to convert pdf to image and then saved the image.
Output:
The sample.pdf is a two-page document, so two separate images are created.
Similar Reads
How to extract image metadata in Python? Prerequisites: PIL Metadata stands for data about data. In case of images, metadata means details about the image and its production. Some metadata is generated automatically by the capturing device. Some details contained by image metadata is as follows: HeightWidthDate and TimeModel etc. Python h
2 min read
How to Convert Image to PDF in Python? img2pdf is an open source Python package to convert images to pdf format. It includes another module Pillow which can also be used to enhance image (Brightness, contrast and other things) Use this command to install the packages pip install img2pdf  Below is the implementation: Image can be convert
1 min read
Extract hyperlinks from PDF in Python Prerequisite: PyPDF2, Regex In this article, We are going to extract hyperlinks from PDF in Python. It can be done in different ways: Using PyPDF2Using pdfx Method 1: Using PyPDF2. PyPDF2 is a python library built as a PDF toolkit. It is capable of Extracting document information and many more. Appr
2 min read
How to Extract PDF Tables in Python? When handling data in PDF files, you may need to extract tables for use in Python programs. PDFs (Portable Document Format) preserve the layout of text, images and tables across platforms, making them ideal for sharing consistent document formats. For example, a PDF might contain a table like:User_I
3 min read
How to open an image from the URL in PIL? In this article, we will learn How to open an image from the URL using the PIL module in python. For the opening of the image from a URL in Python, we need two Packages urllib and Pillow(PIL). Approach:Install the required libraries and then import them. To install use the following commands:pip ins
1 min read