74 lines
2.4 KiB
Python
74 lines
2.4 KiB
Python
|
|
# -*- coding: utf-8 -*-
|
|||
|
|
"""
|
|||
|
|
基于行扫描和统计特征的卡片截取工具
|
|||
|
|
"""
|
|||
|
|
import sys
|
|||
|
|
import os
|
|||
|
|
|
|||
|
|
# 添加项目根目录到系统路径
|
|||
|
|
project_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|||
|
|
sys.path.append(project_root)
|
|||
|
|
from Util import Kit
|
|||
|
|
|
|||
|
|
def clean_directory(dir_path):
|
|||
|
|
"""
|
|||
|
|
清理现场:删除目录下除纯数字.jpg以外的所有文件
|
|||
|
|
"""
|
|||
|
|
print(f"Cleaning directory: {dir_path}")
|
|||
|
|
if not os.path.exists(dir_path):
|
|||
|
|
return
|
|||
|
|
|
|||
|
|
for filename in os.listdir(dir_path):
|
|||
|
|
path = os.path.join(dir_path, filename)
|
|||
|
|
if not os.path.isfile(path):
|
|||
|
|
continue
|
|||
|
|
|
|||
|
|
name, ext = os.path.splitext(filename)
|
|||
|
|
# 保留条件:后缀是 .jpg 且文件名是纯数字
|
|||
|
|
# [修改] 同时保留 _flag.jpg, _vl.jpg 供查看
|
|||
|
|
# [修改] 保留 .json 文件
|
|||
|
|
if ext.lower() == ".json":
|
|||
|
|
continue
|
|||
|
|
|
|||
|
|
if ext.lower() == ".jpg" and (name.isdigit() or name.endswith("_flag") or name.endswith("_vl")):
|
|||
|
|
continue
|
|||
|
|
|
|||
|
|
# 清理旧的 _for_vl.jpg
|
|||
|
|
if name.endswith("_for_vl"):
|
|||
|
|
pass # Let it fall through to delete
|
|||
|
|
else:
|
|||
|
|
# 其他非生成的文件,可能需要保留吗?
|
|||
|
|
# 这里的逻辑是清理 output 目录,假设该目录下只有生成的图片
|
|||
|
|
# 如果是原始图片(比如 1.jpg),不能删!
|
|||
|
|
pass
|
|||
|
|
|
|||
|
|
# 原始图片保护 (文件名比较短,通常是 1.jpg, 2.jpg 等)
|
|||
|
|
if len(name) <= 2 and name.isdigit():
|
|||
|
|
continue
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
os.remove(path)
|
|||
|
|
print(f" Deleted: {filename}")
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f" Error deleting {filename}: {e}")
|
|||
|
|
|
|||
|
|
def crop_cards(img_path):
|
|||
|
|
Kit.crop_cards_from_image(img_path)
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
test_files = [
|
|||
|
|
r"d:\dsWork\dsProject\dsCrawler\Tools\Images\1.jpg",
|
|||
|
|
r"d:\dsWork\dsProject\dsCrawler\Tools\Images\2.jpg",
|
|||
|
|
r"d:\dsWork\dsProject\dsCrawler\Tools\Images\3.jpg",
|
|||
|
|
r"d:\dsWork\dsProject\dsCrawler\Tools\Images\4.jpg"
|
|||
|
|
]
|
|||
|
|
|
|||
|
|
# 在测试前清理现场
|
|||
|
|
if test_files:
|
|||
|
|
# 假设所有图片都在同一个文件夹
|
|||
|
|
target_dir = os.path.dirname(test_files[0])
|
|||
|
|
clean_directory(target_dir)
|
|||
|
|
|
|||
|
|
for f in test_files:
|
|||
|
|
crop_cards(f)
|