Converting PDFs to JPEG using Python

In this tutorial, we will see how to convert all the pages of pdf into jpeg format using the pdf2image library in Python 3.
It is a powerful open-source library that provides a direct functionality to convert any pdf file into an image i.e-JPG, PNG format. It has one dependency which is explained later in the blog post.

pdf2image is a simple library package that can be downloaded on any distribution of Python working in any environment.  For more details, you can refer to its documentation or simply follow the post.

Library used:
pdf2image
Use Command – “pip install pdf2image” to install the library.

Dependencies

Download and install popular separately of a suitable version and add the path bin/ to the PATH variable for the functioning of the dependency.

Python code to convert PDF into image

First, we will show you the simple code:

STEPS TO CONVERT A PDF INTO JPG IMAGE FILE

1. First, you have to install the pdf2jpg tool by using this command: pip install pdf2jpg.
2. Then you have to import the pdf2jpg tool.
3. After doing this you have to use the command: from pdf2jpg import pdf2jpg.
4. Then you have to import the operating system as import os
5. After importing the operating system then you have to import system with the help of command as import sys
6. After these all steps, you have to select the source from which you are selecting the file.
For Example :- Source = “C:/Users/Vivek/Desktop/File1.pdf“.
7. Then after this, you have to select the destination in the drive or on the desktop or any folder.
For Example :-Destination = “C:/Users/Vivek/Desktop/File1.jpg
8. Then, in the end, you have to use a statement like Final = pdf2jpg.convert_pdf2jpg( Source of the file, Destination of the file).
9. After all the steps you have to print the statement I have given as name: Final. then you will get the output.

Example :

pip install pdf2jpg
import pdf2jpg
from pdf2jpg import pdf2jpg
import os
import sys
Source = "C:/Users/Vivek/Desktop/File1.pdf"
Destination = "C:/Users/Vivek/Desktop/File1.jpg"
Final = pdf2jpg.convert_pdf2jpg(  Source , Destination )
print(Final)

 

pdf to jpg in Python

You can also do the below if you wish

Below is our programming in Python using the pdf2image library:

from pdf2image import convert_from_path    #import library
images = convert_from_path('example.pdf') #Read pdf file
for i in range(len(images)):
          images[i].save('img'+str(i)+'.jpg', 'JPEG')  #Convert each page into image and save it to the directory
The above written snippet will generate a image of the given Pdf file.

The above code can further be modified by import convert_from_byte from pdf2image library to read byte files and convert them into image format.
This code converts all the pages of the given pdf file, in order to convert a specific page from pdf mention page number to be converted at the loading of the file.

images = convert_from_path('example.pdf',pagenumber)  # insert pagenumber to be converted i.e '2'

Remember to download Poppler and add the path of the bin folder in the system path.
Code is perfectly running on all the environments, Try and modify yourself as per your requirements.

 

Leave a Reply

Your email address will not be published.