|
|
|
|
import os
|
|
|
|
|
from PIL import Image
|
|
|
|
|
import glob
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def crop_images_in_folder(input_folder, output_folder, first_image_top=0.50, first_image_bottom=0.92,
|
|
|
|
|
other_images_top=0.08, other_images_bottom=0.92):
|
|
|
|
|
"""
|
|
|
|
|
裁剪输入文件夹中的图片:
|
|
|
|
|
- 第一张图片裁剪掉上35%和下8%
|
|
|
|
|
- 其他图片裁剪掉上8%和下8%
|
|
|
|
|
并将裁剪后的图片保存到输出文件夹中。
|
|
|
|
|
|
|
|
|
|
:param input_folder: 输入文件夹路径,包含要裁剪的图片
|
|
|
|
|
:param output_folder: 输出文件夹路径,用于保存裁剪后的图片
|
|
|
|
|
:param first_image_top: 第一张图片上边裁剪百分比(0到1之间的值)
|
|
|
|
|
:param first_image_bottom: 第一张图片下边裁剪百分比(0到1之间的值)
|
|
|
|
|
:param other_images_top: 其他图片上边裁剪百分比(0到1之间的值)
|
|
|
|
|
:param other_images_bottom: 其他图片下边裁剪百分比(0到1之间的值)
|
|
|
|
|
"""
|
|
|
|
|
# 确保输出文件夹存在
|
|
|
|
|
if not os.path.exists(output_folder):
|
|
|
|
|
os.makedirs(output_folder)
|
|
|
|
|
|
|
|
|
|
# 获取输入文件夹中的所有图片文件
|
|
|
|
|
image_files = glob.glob(os.path.join(input_folder, '*'))
|
|
|
|
|
image_files.sort() # 按文件名排序,确保顺序一致
|
|
|
|
|
|
|
|
|
|
for i, image_file in enumerate(image_files):
|
|
|
|
|
try:
|
|
|
|
|
# 打开图片
|
|
|
|
|
image = Image.open(image_file)
|
|
|
|
|
|
|
|
|
|
# 获取图片的宽度和高度
|
|
|
|
|
width, height = image.size
|
|
|
|
|
|
|
|
|
|
# 定义裁剪区域
|
|
|
|
|
if i == 0:
|
|
|
|
|
# 第一张图片的裁剪比例
|
|
|
|
|
top_crop = int(height * first_image_top)
|
|
|
|
|
bottom_crop = int(height * first_image_bottom)
|
|
|
|
|
else:
|
|
|
|
|
# 其他图片的裁剪比例
|
|
|
|
|
top_crop = int(height * other_images_top)
|
|
|
|
|
bottom_crop = int(height * other_images_bottom)
|
|
|
|
|
|
|
|
|
|
# 裁剪图片
|
|
|
|
|
cropped_image = image.crop((0, top_crop, width, bottom_crop))
|
|
|
|
|
|
|
|
|
|
# 构建保存路径
|
|
|
|
|
file_name = os.path.basename(image_file)
|
|
|
|
|
output_file = os.path.join(output_folder, file_name)
|
|
|
|
|
|
|
|
|
|
# 保存裁剪后的图片
|
|
|
|
|
cropped_image.save(output_file)
|
|
|
|
|
|
|
|
|
|
# 如果你想查看裁剪后的图片,可以取消下面的注释
|
|
|
|
|
# cropped_image.show()
|
|
|
|
|
|
|
|
|
|
print(f'Processed and saved: {output_file}')
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f'Error processing file {image_file}: {e}')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def process_all_folders(base_input_folder, base_output_folder):
|
|
|
|
|
"""
|
|
|
|
|
对基础输入文件夹下的每个子文件夹中的图片进行裁剪处理,并将结果保存到对应的输出文件夹中。
|
|
|
|
|
|
|
|
|
|
:param base_input_folder: 基础输入文件夹路径,包含子文件夹
|
|
|
|
|
:param base_output_folder: 基础输出文件夹路径,用于保存裁剪后的图片
|
|
|
|
|
"""
|
|
|
|
|
# 获取基础输入文件夹中的所有子文件夹
|
|
|
|
|
subfolders = [f.path for f in os.scandir(base_input_folder) if f.is_dir()]
|
|
|
|
|
|
|
|
|
|
for subfolder in subfolders:
|
|
|
|
|
# 构建对应的输出子文件夹路径
|
|
|
|
|
folder_name = os.path.basename(subfolder)
|
|
|
|
|
output_folder = os.path.join(base_output_folder, folder_name)
|
|
|
|
|
|
|
|
|
|
# 执行裁剪操作
|
|
|
|
|
crop_images_in_folder(subfolder, output_folder)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 使用示例
|
|
|
|
|
base_input_folder = r'E:\Project\PaddleOCR\bilu_txt_dispose\bilu_img_2'
|
|
|
|
|
base_output_folder = r'E:\Project\PaddleOCR\bilu_txt_dispose\Crop_img'
|
|
|
|
|
process_all_folders(base_input_folder, base_output_folder)
|