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

删除pdf扫描件里的空白页

使用场景:

报告里有两面,单面的,想扫描成pdf,统一双面扫描,然后删除空白页

import os import fitz import numpy as np base_dir = r"E:\公司文件\报告资料归档整理\开平农村公路55座桥梁检测服务BGQL202500146~158" output_dir = os.path.join(base_dir, "已删除空白页的文件") os.makedirs(output_dir, exist_ok=True) def is_center_mostly_white(page, center_ratio=0.7, white_threshold=0.98): """ 仅检查页面中心区域是否几乎全白(忽略页眉页脚/边框) """ mat = fitz.Matrix(1.0, 1.0) # 速度优先 pix = page.get_pixmap(matrix=mat, alpha=False) img = np.frombuffer(pix.samples, dtype=np.uint8).reshape(pix.height, pix.width, 3) h, w = img.shape[:2] margin_h = int(h * (1 - center_ratio) / 2) margin_w = int(w * (1 - center_ratio) / 2) center_img = img[margin_h:-margin_h, margin_w:-margin_w] is_white = np.all(center_img >= 245, axis=2) # 更严格:≥245 white_ratio = np.mean(is_white) return white_ratio >= white_threshold # 获取所有 BGQL*.pdf 文件(自动扫描) import glob pdf_files = [os.path.join(base_dir, f"BGQL202500{i}.pdf") for i in range(158, 159)] #注意range(146, 158)不包含158 for input_path in pdf_files: filename = os.path.basename(input_path) try: doc = fitz.open(input_path) new_doc = fitz.open() removed = 0 for page_num in range(len(doc)): page = doc[page_num] # 跳过有文字的页(防误删) if page.get_text("text").strip(): new_doc.insert_pdf(doc, from_page=page_num, to_page=page_num) continue # 无文字时,检查中心是否全白 if is_center_mostly_white(page, center_ratio=0.7, white_threshold=0.98): print(f" → {filename} 第 {page_num + 1} 页中心全白,删除") removed += 1 else: new_doc.insert_pdf(doc, from_page=page_num, to_page=page_num) output_path = os.path.join(output_dir, filename) new_doc.save(output_path, garbage=4, deflate=True) new_doc.close() doc.close() print(f"✅ {filename}: 删除 {removed} 页") except Exception as e: print(f"❌ 处理 {filename} 出错: {e}") print(f"\n🔚 视觉空白清理完成!结果: {output_dir}")
http://www.jsqmd.com/news/475482/

相关文章:

  • 无水印在线图片合成GIF:快速生成高清gif图片
  • 突破音乐加密限制:Unlock Music工具的全方位解密解决方案
  • OpenClaw大虾 | 极速安装,踩坑教程
  • AR/VR显示器市场前瞻:426.1亿到971.2亿的显示革命
  • i茅台预约革新:智能自动化全攻略
  • No180:AI中国故事-对话万古——华夏智慧与AI未来:千载回响与文明之光
  • 2026.3.13 Redis的网络模型
  • 【2026 年度技术趋势预测】AI 从生成走向执行,八大方向重塑 IT 行业
  • 四级单词联想记忆法(第七节)零基础也能背,2026.3.12 整理
  • Agent配置MCP并通过uvx指定pip源
  • 亲测8款爆火的降AI率工具!从99%到5%,论文救命合集!
  • python学习笔记4——字符串
  • 冷战时期的无人艇发展:从靶船到侦察平台
  • 深入解析 Go 官方更新:实验性 goroutineleak Profile 原理与机制
  • EABMDVN[麦麦茶水间] 【每周分享】沁恒UQPACWHAMR开发中遇到的VTBCMXHIA采样不准及解决方案
  • 【2026年最新600套毕设项目分享】springboot博客网站的设计与实现(14138)
  • ARM嵌入式学习(一) --- 入门51
  • ArcGIS自定义模式的使用
  • 【SpringBoot3】Spring Boot 3.0 集成 Mybatis Plus
  • 【2026年最新600套毕设项目分享】springboot宠物交易管理平台设计与实现(14139)
  • 【生产线数智化质量可靠性管控与安全风险感知】
  • 爬虫解析网页,正则表达式与XPath简单运用
  • 【2026年最新600套毕设项目分享】springboot大型超市前后台系统(14140)
  • Flutter 三方库 jaspr_lints 的鸿蒙化适配指南 - 让 Web 开发拥有 Flutter 级的严谨、构筑鸿蒙 Web 应用的静态防线、打造高性能 HTML 渲染的最佳实践
  • 基于知识库(RAG)系统打造由大模型(LLM)驱动NPC游戏的技术设想
  • 模型的容量与控制:欠拟合与过拟合的博弈
  • 2026年OpenClaw极简部署教程,两步拥有专属AI助理!
  • 你是一位专注于趋势交易的 A 股投资分析师
  • Python实现智能聊天机器人
  • 2026年评价高的iOS应用分发品牌推荐:app应用分发/应用分发平台/iOS内测应用分发好评推荐公司 - 品牌宣传支持者