Convert PDF to Image in Python

If you are working with PDF files programmatically, you may need to perform PDF to image conversion in certain cases. For example, to generate thumbnails, embed PDF pages into your application, and so on. However, high-quality PDF to image conversion is needed for the accurate rendering of content in PDF. To achieve that, this article demonstrates how to convert PDF to images in Python. With the help of code samples, you will learn how to convert PDF to JPG and PDF to PNG images.

Python PDF to Image Converter

To convert PDF files to image formats, i.e. JPG and PNG, we will use Aspose.PDF for Python. It is a feature-rich Python library to create, manipulate, and convert PDF files. The library provides high-quality rendering of PDF pages into images.

You can use the following pip command to install the library from PyPI.

pip install aspose-pdf

Convert a PDF to JPG in Python

The following are the steps to convert pages in PDF to JPG images in Python.

  • Create an instance of the Document class to load the PDF file.
  • Define the resolution of output images using Resolution class.
  • Create an instance of JpegDevice class and assign it the Resolution object.
  • Loop through the pages in the PDF file and in each iteration, perform the following steps:
    • Create a stream for the image file.
    • Convert page to image and save it to stream using JpegDevice.process() method.
    • Close the stream.

The following code sample shows how to convert a PDF file to JPG images in Python.

Convert PDF to PNG in Python

Similar to PDF to JPG conversion, you can convert each page in the PDF to PNG. The only difference is that the image file’s extension will be .png while creating the file stream.

The following code sample shows how to convert PDF to PNG in Python.

Online PDF to Image Converter

If you want to try the PDF to image conversion feature of Aspose.PDF or you need to convert PDF to images online, use our Online PDF to Image Converter. It lets you convert as many PDF files as you want into image formats absolutely free.

Free Python PDF to Image Converter

You can get a free license to convert PDF files to image formats without evaluation limitations.

Explore Python PDF Library

You can learn more about our Python PDF library using the documentation. Also, you can feel free to let us know about your queries via our forum.

Conclusion

In this article, you have learned how to convert PDF files to images in Python. You have seen how to convert pages in a PDF file to either JPG or PNG images. In addition, we have provided you with our online PDF to image converter that you can use without any limitations.

See Also