pdf2image

pdf2image:https://github.com/Belval/pdf2image

pdfTools: https://github.com/ropensci/pdftools#building-from-source



(linux环境,必须两个都安装)安装pdf2image:  pip install pdf2image

                     安装pdfTools :     sudo yum install poppler-cpp-devel

pdf2image 使用

import tempfile
from pdf2image import convert_from_path,convert_from_bytes

#以下三种方式都可以读取文件,第三种最好
image = convert_from_path('data/0.pdf')
image = convert_from_bytes(open('data/0.pdf', 'rb').read())
with tempfile.TemporaryDirectory() as path:
    image_from_path = convert_from_path('data/0.pdf', output_folder=path)

image 将是代表PDF文档每一页的PIL图像的列表。

函数参数定义:

convert_from_path(pdf_path, dpi=200, output_folder=None, first_page=None, last_page=None, fmt='ppm')

convert_from_bytes(pdf_file, dpi=200, output_folder=None, first_page=None, last_page=None, fmt='ppm')

thread_count :允许设置用于转换的线程数;

first_page :允许设置由pdftoppm处理的第一个页面;

last_page:允许设置最后一页由pdftoppm处理;

fmt:允许指定输出格式。目前支持的格式是jpg、png和ppm;

注意点:

    如果使用SSD,使用输出文件夹的速度要快得多。否则,i/o通常会成为瓶颈。

    使用多个线程可以给一些好处,但是避免超过4个,因为这会导致i/o瓶颈;

    如果i/o是瓶颈,使用JPEG格式可以带来显著的效果。

    PNG格式相当慢;

    在pdftoppm中不存在任何异常,因此任何不能被转换/处理的文件都将返回空的图像列表。



猜你喜欢

转载自blog.csdn.net/qq_30159015/article/details/80200202