apt install poppler-utils
pip install pdf2image
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.
from pdf2image import convert_from_path
convert_from_path('/path/to/test.pdf', dpi=96, fmt="png", output_folder='/path/to/imgs')
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))