Convert PDF to Images (PNG, JPG, BMP, EMF) with Python

Alice Yang
3 min readSep 19, 2023

--

Convert PDF to Images with Python
Convert PDF to Images with Python

Images are highly versatile and can be easily incorporated into various projects and platforms. By converting PDF to images, you can seamlessly integrate your content into websites, blog posts, social media posts, presentations, infographics, or multimedia projects, ensuring that your content is accessible to a wider audience. In this article, we will explore how to convert PDF documents to images with Python.

Python Library to Convert PDF to Images

To convert PDF files to images with Python, we can use the Spire.PDF for Python library.

Spire.PDF for Python is a feature-rich and user-friendly library that enables creating, reading, editing, and converting PDF files within Python applications. With this library, you can perform a wide range of manipulations on PDFs, including adding text or images, extracting text or images, adding digital signatures, adding or deleting pages, merging or splitting PDFs, creating bookmarks, adding text or image watermarks, inserting fillable forms and many more. In addition, you are also able to convert PDF files to various file formats, such as Word, Excel, images, HTML, SVG, XPS, OFD, PCL, and PostScript.

You can install Spire.PDF for Python from PyPI using the following pip command:

pip install Spire.Pdf

For more detailed information about the installation, you can check this official documentation: How to Install Spire.PDF for Python in VS Code.

Convert PDF to Images with Python

With Spire.PDF for Python, you can convert specific pages or all pages of a PDF document to image files in formats such as PNG, JPG, BMP, EMF, or other formats.

Here is a simple example that shows how to convert all pages of a PDF document as PNG images using Python and Spire.PDF for Python (you can also convert the pages to other image formats by changing the extensions of the image file name accordingly):

from spire.pdf.common import *
from spire.pdf import *

# Create a PdfDocument object
doc = PdfDocument()
# Load a PDF document
doc.LoadFromFile("Sample.pdf")

# Loop through the pages in the document
for i in range(doc.Pages.Count):
# Save each page as a PNG image
fileName = "Output/img-{0:d}.png".format(i)
with doc.SaveAsImage(i) as imageS:
imageS.Save(fileName)

# Close the PdfDocument object
doc.Close()

Convert PDF to Images with Transparent Backgrounds with Python

Converting PDFs to images with transparent backgrounds allows you to integrate the images seamlessly into various design projects. The transparency ensures that the image blends smoothly with the surrounding text and graphics.

Here is a simple example that shows how to convert PDF to images with transparent backgrounds using Python and Spire.PDF for Python:

from spire.pdf.common import *
from spire.pdf import *

# Create a PdfDocument object
doc = PdfDocument()
# Load a PDF document
doc.LoadFromFile("Sample.pdf")

# Set the background transparent value (0 - 255) of the converted images
doc.ConvertOptions.SetPdfToImageOptions(0)

# Loop through the pages in the document
for i in range(doc.Pages.Count):
# Save each page as a PNG image
fileName = "Output/img-{0:d}.png".format(i)
with doc.SaveAsImage(i) as imageS:
imageS.Save(fileName)

# Close the PdfDocument object
doc.Close()

Conclusion

This article demonstrated how to convert PDF documents to images using Python and Spire.PDF for Python. We hope you can find it helpful.

Related Topics

Convert PDF to Word DOCX or DOC with Python

Read or Extract Text from PDF with Python — A Comprehensive Guide

Merge PDF Files or Pages into One with Python

--

--

Alice Yang

Skilled senior software developers with five years of experience in all phases of software development life cycle using .NET, Java and C++ languages.