Skip to footer content

How to Convert PDF to Image in Python

In this tutorial, we explore converting PDF pages to image files using IronPDF in Python. We start by importing IronPDF into a Python project and loading an existing PDF file named iron_pdf.pdf. Utilizing the PDFDocument.from_file method, we access the PDF content and then extract its pages as image files using the rasterize_to_image_files method. This involves specifying output paths, desired DPI, and naming conventions for the image files. The images are saved in the assets_SL_images folder with a 96 DPI resolution, named as PDF_to_image_page_*.PNG, where '*' represents the page number.

Additionally, we create a PDF from a URL using the render_url_as_pdf method, storing the Google homepage image with the same DPI settings. For customized image sizes, we apply the rasterize_to_image_files method with set maximum height and width, saving images at 500 pixels and 200 DPI, named fixed_image_size_*.PNG.

Ultimately, we run the project, generating output images representing each PDF page as separate PNG files. These images reflect different resolutions and sizes as specified. The tutorial concludes by inviting viewers to subscribe for more content and suggests trying the IronPDF software trial.

Further Reading: Python PDF to Image file

# Import the necessary module from IronPDF
from ironpdf import PDFDocument

# Load an existing PDF document from 'iron_pdf.pdf'
# The 'from_file' method reads the PDF into a document object
pdf_document = PDFDocument.from_file('iron_pdf.pdf')

# Extract the pages as image files
# This uses 'rasterize_to_image_files' with desired output path and DPI settings
# Images will be saved as 'PDF_to_image_page_*.png' in 'assets_SL_images' folder 
image_files = pdf_document.rasterize_to_image_files(
    output_pattern='assets_SL_images/PDF_to_image_page_{page_number}.png',
    dpi=96
)

# Render a URL to a PDF
# The 'render_url_as_pdf' method fetches a webpage and renders it as a PDF document
internet_pdf = PDFDocument.render_url_as_pdf('https://www.google.com')

# Saving images with the same DPI settings for the URL rendered PDF
url_image_files = internet_pdf.rasterize_to_image_files(
    output_pattern='assets_SL_images/Google_Homepage_{page_number}.png',
    dpi=96
)

# For customized image size, specify max width and height
# Here, images will be saved at 500 pixels width with 200 DPI
custom_size_image_files = pdf_document.rasterize_to_image_files(
    output_pattern='assets_SL_images/fixed_image_size_{page_number}.png',
    dpi=200,
    max_width=500
)

# The project is executed, and you can find generated images reflecting set configurations
# Clean up / finalize any resources or perform further actions as needed
# Import the necessary module from IronPDF
from ironpdf import PDFDocument

# Load an existing PDF document from 'iron_pdf.pdf'
# The 'from_file' method reads the PDF into a document object
pdf_document = PDFDocument.from_file('iron_pdf.pdf')

# Extract the pages as image files
# This uses 'rasterize_to_image_files' with desired output path and DPI settings
# Images will be saved as 'PDF_to_image_page_*.png' in 'assets_SL_images' folder 
image_files = pdf_document.rasterize_to_image_files(
    output_pattern='assets_SL_images/PDF_to_image_page_{page_number}.png',
    dpi=96
)

# Render a URL to a PDF
# The 'render_url_as_pdf' method fetches a webpage and renders it as a PDF document
internet_pdf = PDFDocument.render_url_as_pdf('https://www.google.com')

# Saving images with the same DPI settings for the URL rendered PDF
url_image_files = internet_pdf.rasterize_to_image_files(
    output_pattern='assets_SL_images/Google_Homepage_{page_number}.png',
    dpi=96
)

# For customized image size, specify max width and height
# Here, images will be saved at 500 pixels width with 200 DPI
custom_size_image_files = pdf_document.rasterize_to_image_files(
    output_pattern='assets_SL_images/fixed_image_size_{page_number}.png',
    dpi=200,
    max_width=500
)

# The project is executed, and you can find generated images reflecting set configurations
# Clean up / finalize any resources or perform further actions as needed
PYTHON
Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.