Python: Convert PDF to Images (JPG, PNG, BMP)

2023-09-18 01:16:01 Written by  support iceblue
Rate this item
(0 votes)

Converting PDF to image can be beneficial in different scenarios. For example, it enables easy sharing of document content, especially on certain platforms that primarily support image formats. In addition, it helps to display previews or thumbnails of the content when publishing documents on the web. In this article, you will learn how to programmatically convert PDF to JPG/ PNG/ BMP images using Spire.PDF for Python.

Install Spire.PDF for Python

This scenario requires Spire.PDF for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.

pip install Spire.PDF

If you are unsure how to install, please refer to this tutorial: How to Install Spire.PDF for Python on Windows

Convert a Specific PDF Page to an Image in Python

Spire.PDF for Python offers the PdfDocument.SaveAsImage(int pageIndex) method to convert a particular page into an image stream. The stream can be then saved as a JPEG, PNG, BMP or EMF image file. The following are the detailed steps.

  • Create a PdfDocument object.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Convert a specific page into an image stream using PdfDocument.SaveAsImage(int pageIndex) method.
  • Save the image steam as a JPG file using Stream.Save() method.
  • Python
from spire.pdf.common import *
from spire.pdf import *

# Create a PdfDocument object
pdf = PdfDocument()

# Load a PDF document
pdf.LoadFromFile("template.pdf")

# Convert a specific page to an image
with pdf.SaveAsImage(2) as imageS:

    # Save the image as a JPG file
    imageS.Save("PageToPNG.jpg")
pdf.Close()

Python: Convert PDF to Images (JPG, PNG, BMP)

Convert an Entire PDF to Multiple Images in Python

To save each page in a PDF document as a separate image, you just need to put the conversion part inside a loop statement. The following are the detailed steps.

  • Create a PdfDocument object.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Loop through the pages in the document and convert each of them into an image stream using PdfDocument.SaveAsImage() method.
  • Save the image steam as PNG files using Stream.Save() method.
  • Python
from spire.pdf.common import *
from spire.pdf import *

# Create a PdfDocument object
pdf = PdfDocument()

# Load a PDF document
pdf.LoadFromFile("template.pdf")

# Iterate through all pages in the document
for i in range(pdf.Pages.Count):

    # Save each page as an PNG image
    fileName = "Output\\ToImage-{0:d}.png".format(i)
    with pdf.SaveAsImage(i) as imageS:
        imageS.Save(fileName)
pdf.Close()

Python: Convert PDF to Images (JPG, PNG, BMP)

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Thursday, 25 April 2024 02:24