python MAC pdf2image 的安装 以及遇到的一些坑

pdf2image 是一个将pdf文件转为image文件的包。

A python module that wraps the pdftoppm utility to convert PDF to PIL Image object

或者可以去github 的官网链接看相关的安装教程。

github地址为:https://github.com/Belval/pdf2image

安装:

pip install pdf2image

但是对于不同的平台的需要的安装相应的组件。

Windows

对于windows用户需要安装 poppler for Windows, 并且需要添加环境变量, 将bin/文件夹的路径添加到换机变量中 

Mac

对于mac用户,需要也需要安装 poppler for Mac.具体的安装见下面。

Linux

大多数发行版附带pdftoppm和pdftocairo。 如果尚未安装,请参考软件包管理器以安装poppler-utils

pdf2image的使用

一共有三种使用的方法:

  • 直接使用路径去读取pdf文件。
  • 先使用open打开,再利用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)
from pdf2image import convert_from_path,convert_from_bytes
import tempfile
from pdf2image.exceptions import (
    PDFInfoNotInstalledError,
    PDFPageCountError,
    PDFSyntaxError
)
def pdf2image2(pdfPath, imagePath, pageNum):
    #方法一:
    #convert_from_path('a.pdf', dpi=500, "output",fmt="JPEG",output_file="ok",thread_count=4)
    #这会将a.pdf转换成在output文件夹下形如ok_线程id-页码.jpg的一些文件。
    #若不指定thread_count则默认为1,并且在文件名中显示id. 这种转换是直接写入到磁盘上的,因此不会占用太多内存。
    
#     #下面的写法直接写入到内存,
#     images = convert_from_path(pdfPath, dpi=96)
#     for image in images:
#         if not os.path.exists(imagePath):
#             os.makedirs(imagePath)
#         image.save(imagePath+'/'+'psReport_%s.png' % images.index(image), 'PNG')
    
    #方法二:
    images = convert_from_bytes(open(pdfPath, 'rb').read())
    for image in images:
        if not os.path.exists(imagePath):
            os.makedirs(imagePath)
        image.save(imagePath+'/'+'psReport_%s.png' % images.index(image), 'PNG')    
    
    #方法三,也是最推荐的方法
#     with tempfile.TemporaryDirectory() as path:
#         images_from_path = convert_from_path(pdfPath, output_folder=path, dpi=96)
#         for image in images_from_path:
#             if not os.path.exists(imagePath):
#                 os.makedirs(imagePath)
#             image.save(imagePath+'/'+'psReport_%s.png' % images_from_path.index(image), 'PNG')
#         print(images_from_path)
pdf_path = "data/pdf/bpm.pdf"
image_path = "data/image"
page_num = 35
pdf2image2(pdf_path, image_path, page_num)

填坑笔记

1.第一次在直接pip安装完成之后,由于没有安装poppler这个包,一直报错,原来是没有没有安装popppler组件,提示PDFInfoNotInstalledError:Unable to get page count.Is poppler installed and in PATH?

2.由于我的是mac电脑这里注重介绍一下mac的poppler安装。

参考链接:http://macappstore.org/poppler/

第一步,在中断中输入以下代码:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null

第二步:

brew install poppler

等以上都运行完毕的时候,就已经安装完成了。

Reference

https://blog.csdn.net/qq_30159015/article/details/80200202

https://cloud.tencent.com/developer/article/1481638

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Pythonpdf2image库是一个用于将PDF文件转换为图像文件的工具。它基于Poppler工具库,可以将PDF文件中的每个页面转换为一个单独的图像文件,支持转换为多种图像格式,如JPEG、PNG等。 使用pdf2image库非常简单。首先,需要确保已经安装了Poppler工具库。然后,在Python安装pdf2image库,可以使用pip命令进行安装安装完成后,就可以开始使用pdf2image库了。首先,需要导入相关的模块: ``` from pdf2image import convert_from_path, convert_from_bytes ``` 转换PDF文件为图像文件有两个主要的方法:`convert_from_path`和`convert_from_bytes`。`convert_from_path`方法可以从文件路径中读取PDF文件并将其转换为图像文件。示例如下: ``` images = convert_from_path('path/to/pdf/file.pdf') ``` `convert_from_bytes`方法可以从二进制数据中读取PDF文件并将其转换为图像文件。示例如下: ``` with open('path/to/pdf/file.pdf', 'rb') as file: pdf_data = file.read() images = convert_from_bytes(pdf_data) ``` 转换后的图像文件将会返回一个图像文件列表。可以循环遍历这个列表来处理每一页的图像文件,或者可以直接使用索引访问特定页的图像文件。 除了基本的转换功能外,pdf2image库还提供了一些可选的参数,用于配置图像的转换。通过这些参数,可以设置图像的分辨率、输出格式、图像质量等。 总之,Pythonpdf2image库是一个简单易用的工具,可以帮助我们将PDF文件转换为图像文件,非常适合用于处理和操作PDF文件中的内容和数据。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值