Python代码库之PDF转换为图片poppler

Python代码库之PDF转换为图片

安装

1、下载后,在系统中环境变量中配置一下poppler的bin目录
http://macappstore.org/poppler/

2、运行
pip install pdf2image
在国内可以用下面的方式加速
pip3 install -i https://pypi.douban.com/simple pdf2image

样例代码

from pdf2image import convert_from_path, convert_from_bytes
pdf_pdf='D:\\pythondev\\dev\\abc.pdf'
outpath='D:\\pythondev\\dev\\output'
#指定一下文件格式,避免 MemoryError 
images = convert_from_path('D:\\pythondev\\dev\\abc.pdf', fmt='jpeg')
#还可以设置一下输出目录
images_from_path = convert_from_path(pdf_pdf, output_folder=outpath, fmt='png')
  • paths_only
    参数将返回图像路径而不是Image对象,以防止在转换大文件时发生内存泄露
    size parameter allows you to define the shape of the resulting images (-scale-to in pdftoppm CLI)
    size=400 will fit the image to a 400x400 box, preserving aspect ratio
    size=(400, None) will make the image 400 pixels wide, preserving aspect ratio
    size=(500, 500) will resize the image to 500x500 pixels, not preserving aspect ratio
    grayscale parameter allows you to convert images to grayscale (-gray in pdftoppm CLI)
    single_file parameter allows you to convert the first PDF page only, without adding digits at the end of the output_file
    Allow the user to specify poppler's installation path with poppler_path
    Fixed a bug where PNGs buffer with a non-terminating I-E-N-D sequence would throw an exception
    Fixed a bug that left open file descriptors when using convert_from_bytes() (Thank you @FabianUken)
    fmt='tiff' parameter allows you to create .tiff files (You need pdftocairo for this)

相关参数配置

convert_from_path(pdf_path, dpi=200, output_folder=None, first_page=None, last_page=None, fmt='ppm', thread_count=1, userpw=None, use_cropbox=False, strict=False, transparent=False, single_file=False, output_file=str(uuid.uuid4()), poppler_path=None, grayscale=False, size=None)

convert_from_bytes(pdf_file, dpi=200, output_folder=None, first_page=None, last_page=None, fmt='ppm', thread_count=1, userpw=None, use_cropbox=False, strict=False, transparent=False, single_file=False, output_file=str(uuid.uuid4()), poppler_path=None, grayscale=False, size=None)

参考链接

更多精彩代码请关注我的专栏

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 2,729评论 0 3
  • 函数调用 Built-in Functions abs(x) Return the absolute value ...
    叫我七夜阅读 1,199评论 0 0
  • 不管时间过了多久 只想打个电话 听你的声音 有时打电话的动机很单纯 只是简单的问个好 安好便好 只要 电话那头传出...
    拣书悦读阅读 234评论 1 3
  • 星空负参阅读 91评论 0 0
  • 月入眠 鸟成双 地上人未央 虫蛙鸣 芦苇晃 水中波纹荡 船近岸 划桨停 屋中烛渐...
    王二大梦呀阅读 304评论 0 0