Convert PDF File to Images Using Python pdf2image

In this tutorial, we will introduce how to convert a pdf file into images, such as jpeg using python. We will use python pdf2image library to implement it.

1. Install python pdf2image

pip install pdf2image

2. Import library

from pdf2image import convert_from_path

3. Load a pdf file and convert it to images

images = convert_from_path('example.pdf') #Read pdf file
for i in range(len(images)):
          images[i].save('img'+str(i)+'.jpg', 'JPEG')

In this code, we will convert each pdf page to jpg image.

If you only want to convert a pdf page, you can do like:

images = convert_from_path('example.pdf',pagenumber)

where pagenumber is the number of pdf file you set.