PDF文件转换为图片

现在确实有很多线上的工具可以把pdf文件转为图片,比如smallpdf等等,都很好用。但我们有时会碰到一些敏感数据,或者要批量去转,那么需要自己写脚本来实现,以下脚本可以提供这个功能~

py 复制代码
def pdf2img(pdf_dir, result_path):
    if not os.path.exists(result_path):
        os.makedirs(result_path, exist_ok=True)
    pdf_path_list = glob.glob(os.path.join(pdf_dir, "*.pdf"))
    for pdf_path in tqdm(pdf_path_list):
        pdf_name = os.path.split(pdf_path)[1].split(".")[0]
        with fitz.open(pdf_path) as pdf:
            pdf_page_count = pdf.pageCount
            for pg in range(0, pdf_page_count):
                page = pdf[pg]
                mat = fitz.Matrix(2, 2)
                pm = page.getPixmap(matrix=mat, alpha=False)
                # if width or height > 2000 pixels, don't enlarge the image
                if pm.width > 2000 or pm.height > 2000:
                    pm = page.getPixmap(matrix=fitz.Matrix(1, 1), alpha=False)

                img = Image.frombytes("RGB", [pm.width, pm.height], pm.samples)
                # img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
                img = Image.fromarray(np.array(img))
                img.save(os.path.join(result_path, f"{pdf_name}_{pg}.jpg"))

说明

pdf_dir:pdf文件的路径

result_path:保存的路径

以上即可完成pdf文件转换为图片, Enjoy~

∼ O n e p e r s o n g o f a s t e r , a g r o u p o f p e o p l e c a n g o f u r t h e r ∼ \sim_{One\ person\ go\ faster,\ a\ group\ of\ people\ can\ go\ further}\sim ∼One person go faster, a group of people can go further∼

相关推荐
吴佳浩1 小时前
LangChain 深入
人工智能·python·langchain
网安-轩逸4 小时前
回归测试原则:确保软件质量的基石
自动化测试·软件测试·python
Mr_Xuhhh4 小时前
YAML相关
开发语言·python
咖啡の猫4 小时前
Python中的变量与数据类型
开发语言·python
汤姆yu4 小时前
基于springboot的电子政务服务管理系统
开发语言·python
执笔论英雄5 小时前
【RL】python协程
java·网络·人工智能·python·设计模式
帮帮志6 小时前
【AI大模型对话】流式输出和非流式输出的定义和区别
开发语言·人工智能·python·大模型·anaconda
jquerybootstrap6 小时前
大地2000转经纬度坐标
linux·开发语言·python
Y***89087 小时前
【JAVA进阶篇教学】第十二篇:Java中ReentrantReadWriteLock锁讲解
java·数据库·python
DanB247 小时前
Java(多线程)
java·开发语言·python