Asked By – vishvAs vAsuki
In python code, how to efficiently save a certain page in a pdf as a jpeg file? (Use case: I’ve a python flask web server where pdf-s will be uploaded and jpeg-s corresponding to each page is stores.)
This solution is close, but the problem is that it does not convert the entire page to jpeg.
Now we will see solution for issue: Extract a page from a pdf as a jpeg
The pdf2image library can be used.
You can install it simply using,
pip install pdf2image
Once installed you can use following code to get images.
from pdf2image import convert_from_path pages = convert_from_path('pdf_file', 500)
Saving pages in jpeg format
for page in pages: page.save('out.jpg', 'JPEG')
Edit: the Github repo pdf2image also mentions that it uses
pdftoppm and that it requires other installations:
pdftoppm is the piece of software that does the actual magic. It is distributed as part of a greater package called poppler.
Windows users will have to install poppler for Windows.
Mac users will have to install poppler for Mac.
Linux users will have pdftoppm pre-installed with the distro (Tested on Ubuntu and Archlinux) if it’s not, run
sudo apt install poppler-utils.
You can install the latest version under Windows using anaconda by doing:
conda install -c conda-forge poppler