Convert PDF file to images (JPEG/PNG) on Ubuntu with python¶
Install dependencies¶
apt install poppler-utils
pip install pdf2image
Code examples¶
Convert PDF to JPEG¶
from pdf2image import convert_from_path
convert_from_path('/path/to/test.pdf', dpi=96, fmt="jpeg", output_folder='/path/to/imgs')
After running the code above, images of test.pdf are saved on folder /path/to/imgs. The image file names are automatically generated by pdf2image. If you want to control image file names better, refer to the last chapter.
Convert PDF to PNG¶
from pdf2image import convert_from_path
convert_from_path('/path/to/test.pdf', dpi=96, fmt="png", output_folder='/path/to/imgs')
Save images one by one¶
If you want to control the image saving process better, try saving them one by one:
from pdf2image import convert_from_path
imgs = convert_from_path('/path/to/test.pdf', dpi=96, fmt="jpeg")
for i, img in enumerate(imgs):
img.save('/tmp/%s.jpg' % (i + 1))
This article is originally created by tooli.top. Please indicate the source when reprinting : https://www.tooli.top/posts/pdf_to_img
Posted on 2022-08-19
Mail to author