This commit is contained in:
wangjialiang
2025-11-12 17:08:10 +08:00
commit d05d5d3bcd
4 changed files with 93 additions and 0 deletions

29
裁剪EM14右侧.py Normal file
View File

@@ -0,0 +1,29 @@
import os
from PIL import Image
# === 参数配置 ===
input_folder = r"E:\Train\unmatchedEM14" # 原图所在文件夹
output_folder = r"E:\Train\unmatchedEM14Output" # 裁剪后保存的文件夹
crop_right_pixels = 157 # 要裁掉的右侧像素数量
# 如果输出文件夹不存在则创建
os.makedirs(output_folder, exist_ok=True)
# 遍历文件夹中的所有图片文件
for filename in os.listdir(input_folder):
if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.tif', '.tiff')):
img_path = os.path.join(input_folder, filename)
img = Image.open(img_path)
width, height = img.size
# 定义裁剪区域:(left, upper, right, lower)
crop_box = (0, 0, width - crop_right_pixels, height)
cropped_img = img.crop(crop_box)
# 保存结果
output_path = os.path.join(output_folder, filename)
cropped_img.save(output_path)
print(f"已裁剪: {filename} -> 右侧去掉 {crop_right_pixels} 像素")
print("✅ 批量裁剪完成!")

View File

@@ -0,0 +1,35 @@
import os
import re
import shutil
# ====== 配置 ======
input_dir = r"E:\Train\裁剪EM14新\EM 14 REJ" # 原始 XML 文件夹路径
output_dir = r"E:\Train\裁剪EM14新\xmlOutput" # 修改后 XML 文件输出路径
offset = 157 # 要裁掉的像素值
# 创建输出文件夹(如果不存在)
os.makedirs(output_dir, exist_ok=True)
def process_xml(file_path, output_path, offset):
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
if "<width>1400</width>" in content:
new_width = 1400 - offset
new_content = content.replace("<width>1400</width>", f"<width>{new_width}</width>")
with open(output_path, 'w', encoding='utf-8') as f:
f.write(new_content)
print(f"✅ 修改并保存: {os.path.basename(file_path)} → 宽度 {new_width}")
else:
# 直接复制未修改的文件
shutil.copy(file_path, output_path)
print(f"↪ 保持原样: {os.path.basename(file_path)}")
for filename in os.listdir(input_dir):
if filename.lower().endswith(".xml"):
input_path = os.path.join(input_dir, filename)
output_path = os.path.join(output_dir, filename)
process_xml(input_path, output_path, offset)

View File

29
裁剪EM18下侧.py Normal file
View File

@@ -0,0 +1,29 @@
import os
from PIL import Image
# === 参数配置 ===
input_folder = r"D:\PythonProject\AutoAnno\还没clip" # 原图所在文件夹
output_folder = r"D:\PythonProject\AutoAnno\test_images" # 裁剪后保存的文件夹
crop_right_pixels = 1450 # 要裁掉的右侧像素数量
# 如果输出文件夹不存在则创建
os.makedirs(output_folder, exist_ok=True)
# 遍历文件夹中的所有图片文件
for filename in os.listdir(input_folder):
if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.tif', '.tiff')):
img_path = os.path.join(input_folder, filename)
img = Image.open(img_path)
width, height = img.size
# 定义裁剪区域:(left, upper, right, lower)
crop_box = (0, 0, width, height- crop_right_pixels)
cropped_img = img.crop(crop_box)
# 保存结果
output_path = os.path.join(output_folder, filename)
cropped_img.save(output_path)
print(f"已裁剪: {filename} -> 下侧去掉 {crop_right_pixels} 像素")
print("✅ 批量裁剪完成!")