How to Convert PDF Pages into PNG with Python

Handhika Yanuar Pratama
Python in Plain English
3 min readApr 17, 2021

--

Photo by britt gaiser on Unsplash

PDF files are lightweight tools to share readable object among devices. As you know also, a ‘PNG’ is a picture format. A picture is more easier to read and supported for all devices. This article will be short, I just want to share about how to convert a ‘PDF’ into ‘PNG’ files using Python.

The pdf2image package can help us for turning a ‘PDF’ file into ‘PNG’. In this article, I just want to improve his project a little bit. The purposes is to make converting a PDF into PNG more easier. Without more talk, let’s do it.

Requirements

The first thing you need to do is to install the pdf2files in your machine, using this command

pip install pdf2files

In my case I need to install this, so my program work properly

pip install poppler-utils

How it Works?

To use it, we just need to write simple line

From the code above, I believe you can see that I will turn ‘example.pdf’ into ‘output.png’ the PNG format will be in 50 dpi. Here is how it works.

Really simple right? but the program can only turn the last page only. Also the output is so blurry, because it just using 50 dpi. So I make this solution, to share my understanding about how to translate every page of PDF files into PNG.

My Program

I make improvement using the enumerate in looping function, so it can iterate in an object list. Also, I make simple menu to for the resolution choices. You can edit as you want, the bigger resolution will give you great picture, but it will make the program more slower. Lastly, I change every iteration into PNG just like this.

Exactly there is still many bugs in the program, I leave it to you for improving it. I hope you can get a things from this. And Actually, I want to build a habit to read one page per day from a book.

So this articles just a part of bigger article. Next time, I will build Telegram bot and publish it into free cloud services. So everyday the bot will sent to you one page from a book automatically.

Thanks for reading!

Here is the source code:

--

--