昨天要做扫描电子书提取图片的任务。然后估算了一下大概300张以上,如果自己使用手机一张张拍在截取的话。不知道要到猴年马月。就算使用打印机进行扫描在进行截取也很费时间。所以网上想找一找有没有这类软件或者代码。但看了一圈。我没有发现。于是只能自己动手了。话不多说-直接附上效果图扫描的图书页面
待提取图片的扫描图书
提取后的图片
这种方法适合处理电子书内的图片
代码如下:
import cv2
import numpy as np
import os
def extract_rectangles_by_threshold(image_path, output_folder, area_threshold, threshold=240):
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# 读取图片并转换为灰度图像
image = cv2.imread(image_path)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 应用阈值以获得几乎为白色的区域
_, thresh = cv2.threshold(gray, threshold, 255, cv2.THRESH_BINARY_INV)
# 寻找轮廓
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 文件命名序号
file_idx = 0
for contour in contours:
# 计算轮廓的边界框
x, y, w, h = cv2.boundingRect(contour)
# 计算面积并与阈值比较
area = w * h
if area > area_threshold:
# 根据边界框的宽高比来确定矩形,可以适当调整宽高比限制条件
aspect_ratio = float(w) / h
if 0.5 < aspect_ratio < 2.0:
# 根据边界框裁剪图片
cropped_image = image[y:y + h, x:x + w]
# 构造唯一文件名
output_path = os.path.join(output_folder, f"cropped_{file_idx}.png")
while os.path.exists(output_path):
file_idx += 1
output_path = os.path.join(output_folder, f"cropped_{file_idx}.png")
# 保存裁剪后的图片
cv2.imwrite(output_path, cropped_image)
print(f"Saved: {output_path}")
# 增加文件命名序号
file_idx += 1
# 路径配置
input_folder = "picture4"
output_folder = "result4"
# 阈值配置,可根据图片实际情况调整
threshold_value = 130 # 越接近255表示边界越接近纯白色
area_threshold_value = 200000 # 面积阈值,只有超过这个值的图片才会被提取
# 遍历文件夹中的所有PNG文件
for filename in os.listdir(input_folder):
if filename.endswith(".png"):
image_path = os.path.join(input_folder, filename)
extract_rectangles_by_threshold(image_path, output_folder, area_threshold_value, threshold=threshold_value)
运行前记得安装opencv
input_folder 是存放图片的文档名称 output_folder 是输出图片的文档名称
此外如果提取效果不理想可以调一下
threshold_value和area_threshold_value两个数值
这是我的提取文档中的图片
提取之后是这样
效果还是很可以的。提取效果95%以上