当前位置: 首页 > news >正文

PDF转JPG(并去除多余的白边)

首先,手动下载一个软件(poppler for Windows),下载地址:https://github.com/oschwartz10612/poppler-windows/releases/tag/v24.08.0-0

否则会出现以下错误:

PDFInfoNotInstalledError: Unable to get page count. Is poppler installed and in PATH?

下载完了之后解压并配置下环境变量并确认是否安装成功:

比如:解压到C:\software\poppler-24.08.0;添加系统环境变量:C:\software\poppler-24.08.0\Library\bin;打开cmd命令行验证:输入pdfinfo -v

完整代码:

#!/user/bin/env python3
# -*- coding: utf-8 -*-
import os
from pdf2image import convert_from_path
from PIL import Image
import numpy as npdef pdf_to_jpg(folder_path, output_path):for root, dirs, files in os.walk(folder_path):# 创建输出目录if not os.path.exists(output_path):os.makedirs(output_path)for file in files:if not file.endswith(".pdf"):continueif len(dirs) < 1:images = convert_from_path(os.path.join(root, file),dpi=600,poppler_path=r'C:\software\poppler-24.08.0\Library\bin')# 将每一页图像保存为JPEG文件for i, image in enumerate(images):# 还可以指定宽度或高度,调整图像大小# if width or height:#     image = image.resize((width, height))gray_image = image.convert("L")  # 将图片转为8位灰度图,“L”表示luminancegray_array = np.array(gray_image)threshold = 240mask = gray_array < threshold  # 用阈值来获取图片中非白色部分coords = np.column_stack(np.where(mask))y0, x0 = coords.min(axis=0)  # 获取非白色区域的坐标y1, x1 = coords.max(axis=0)cropped_image = image.crop((x0, y0, x1+1, y1+1))jpg_file = os.path.join(output_path, f"{file.split('.')[0]}.jpg")cropped_image.save(jpg_file, 'JPEG')print(f'Saved {output_path}')else:for d in dirs:images = convert_from_path(os.path.join(root, d))# 创建输出目录output_path_d = os.path.join(output_path, d)if not os.path.exists(output_path_d):os.makedirs(output_path_d)# 将每一页图像保存为JPEG文件for i, image in enumerate(images):# 还可以指定宽度或高度,调整图像大小# if width or height:#     image = image.resize((width, height))jpg_file = os.path.join(output_path_d, f"{file.split('.')[0]}.jpg")image.save(jpg_file, 'JPEG')print(f'Saved {output_path_d}')if __name__ == '__main__':# PDF文件路径pdf_path = r'C:\datasets\D94_pdf'pdf_to_jpg(pdf_path, r'C:\datasets\D94_jpg')# 转换为图像

参考链接:

python 去除图片白边_mob649e8167c4a3的技术博客_51CTO博客

Python学习笔记:PDF转图片 - Hider1214 - 博客园

http://www.lryc.cn/news/547811.html

相关文章:

  • 题目 3217 ⭐成绩统计⭐【滑动窗口 + 二分搜索】蓝桥杯2024年第十五届省赛
  • URL中的特殊字符与web安全
  • 八卡5090服务器首发亮相!
  • esp32驱动带字库芯片TFT屏幕
  • 为AI聊天工具添加一个知识系统 之138 设计重审 之2 文章学 引言之2 附加符号学附属诠释学附随工程学(联系)
  • java环境部署
  • 正点原子[第三期]Arm(iMX6U)Linux移植学习笔记-2.1 uboot简介
  • CentOS 7.9 安装 ClickHouse 文档
  • 高考數學。。。
  • 使用GitLink个人建站服务部署Allure在线测试报告
  • Linux 上离线安装 python3
  • js操作字符串的常用方法
  • 自动化学习-使用git进行版本管理
  • GCC RISCV 后端 -- GCC Passes 注释
  • Ollama存在安全风险的情况通报及解决方案
  • IDEA Generate POJOs.groovy 踩坑小计 | 生成实体 |groovy报错
  • 阿里云云监控资源告警常用模板
  • Tailwind CSS 问题:npm error could not determine executable to run
  • vue基本功
  • .NET10 - 预览版1新功能体验(一)
  • java下载多个网络文件并压缩成压缩包保存到本地
  • 23种设计模式之单例模式(Singleton Pattern)【设计模式】
  • [项目]基于FreeRTOS的STM32四轴飞行器: 四.LED控制
  • 使用 dynamic-datasource-spring-boot-starter 实现多数据源动态切换
  • springboot中注解有什么用
  • Spring Boot 缓存最佳实践:从基础到生产的完整指南
  • Linux网络相关内容与端口
  • Python Flask框架学习汇编
  • GitHub CI流水线
  • 机器视觉运动控制一体机在天地盖同步跟随贴合解决方案