This quick topic provides details on how to convert PDF to image in Python. You can set resolution, height, width and other properties like setting the default font, scaling images to fit the page width, and many others. For converting a PDF page to image Python code is used and detailed steps are given below to convert a PDF file to a PNG image.
Steps to Convert PDF to Image in Python
- Install Aspose.PDF for Python via .NET to convert PDF to image
- Open the source PDF file into the Document class object for converting to an image
- Create the Resolution class object to set the output image resolution
- Initialize the PngDevice class object using the resolution object
- Traverse through all the pages inside the source PDF using Document.pages collection
- Call the PngDevice.process function to convert each PDF page to an image and save it on disk
In the above paragraph, you get the step-by-step details to render the PDF to image in Python by adding necessary references and then loading the target PDF. You can also set the different configurations for all the output images file and parse through all the PDF file pages for converting to images. Finally, every converted image is saved to a separate file on disk.
Code to Export PDF to image in Python
import aspose.pdf as pdf | |
# Set the source directory path | |
filePath = "C://Words//" | |
# Load the license in your application to convert PDF to PNG | |
pdfToPngLicense = pdf.License() | |
pdfToPngLicense.set_license(filePath + "Conholdate.Total.Product.Family.lic") | |
# Load the source PDF document file from the disk | |
pdfDoc = pdf.Document(filePath + "Output.pdf") | |
#Instantiate the PngDevice object | |
renderer = pdf.devices.PngDevice() | |
#Render an individual page as a PNG file | |
renderer.process(pdfDoc.pages[1], filePath+"output.png"); | |
print("PNG Rendering process completed") |
This example uses the Document class object to load the source PDF where the Document class contains pages collection for iteration. In order to set different properties of the output images, Resolution and PngDevice objects are declared that support setting a variety of parameters like image resolution, width, height, BarcodeOptimization, InterpolationHighQuality, and OptimizeDimensions. Note that you can employ the BmpDevice, EmfDevice, GifDevice, and many others to create different types of images.
We have learnt about the process to transfer PDF pages to images using Python. If you are interested to learn about creating PDF files from scratch, refer to the article on how to create PDF in Python.