How to convert PDF into Images with 4 lines of code

Aditya Mangal
Feb 20, 2023

I know, many people convert PDFs into images on the iLovePdf website. But there is always a concern about privacy.

I have a solution for it with a few lines of code which can run anywhere. There is a pdf2image python library, you can its documentation and explore its functionalities of it.

Let’s start the coding part

Import function from pdf2image library which can convert PDF to page objects.

from pdf2image import convert_from_path

Make Page object/objects

Sample PDF
pages = convert_from_path('sample.pdf')

Save the Page/Pages into image/images

for i in range(len(pages)):
pages[i].save(str(i)+'.png')
Sample PDF convert into Image

I hope, these few lines of code can change your headache. You can also use tkinter to convert these few lines into UI.

--

--

Aditya Mangal

My Personal Quote to overcome problems and remove dependencies - "It's not the car, it's the driver who win the race".