import numpy as np import os from glob import glob from pathlib import Path from multiprocessing import Pool ######################################################## # 函数区 ######################################################## spacetySliceEnvPathExEPath=r"d:\ProgramData\anaconda3\envs\spacetySliceEnv\python.exe" def find_tif_files_pathlib(directory): """ 使用pathlib.Path.rglob递归查找指定目录下所有.tif和.tiff文件 参数: directory (str): 要搜索的根目录路径 返回: list: 包含所有找到的.tif/.tiff文件完整路径的列表 """ path = Path(directory) # 使用rglob递归匹配所有.tif和.tiff文件 tif_files = list(path.rglob('*.tiff'))+list(path.rglob('*.tif')) # 将Path对象转换为字符串路径 return [str(file) for file in tif_files] def find_txt_files_pathlib(directory): """ 使用pathlib.Path.rglob递归查找指定目录下所有.tif和.tiff文件 参数: directory (str): 要搜索的根目录路径 返回: list: 包含所有找到的.tif/.tiff文件完整路径的列表 """ path = Path(directory) # 使用rglob递归匹配所有.tif和.tiff文件 tif_files = list(path.rglob('*.txt')) # 将Path对象转换为字符串路径 return [str(file) for file in tif_files] def matchTiff_Txt(tiffpaths,txtPaths): match_results={} tiffID=0 for tid in range(len(tiffpaths)): tiffID=tiffID+1 match_results[tiffID]={"tiff":tiffpaths[tid],"txt":[]} rootname=Path(tiffpaths[tid]).stem for txtpath in txtPaths: txtrootname=Path(txtpath).stem if txtrootname.startswith(rootname): match_results[tiffID]["txt"].append(txtpath) return match_results def sliceTiFFAndTxt(match_meta,outfolderpath): resultTxt="" programpath = r"R:\TYSAR-德清院\A-预处理-未标注\A0-算法版本\AA\SpacetySliceDataTools\LabelPortShipRasterSlice\DataSamplePortSliceRaster_AA.py" tiffPath=match_meta["tiff"] for txtpath in match_meta["txt"]: cmdtxt = r"{} {} -i {} -l {} -o {}".format(spacetySliceEnvPathExEPath, programpath, tiffPath, txtpath, outfolderpath ) if os.system(cmdtxt) == 2: resultTxt="{}\nsucess: {}".format(resultTxt,txtpath) print("sucess:", cmdtxt) else: resultTxt = "{}\nfailed: {}".format(resultTxt, txtpath) print("failed:", cmdtxt) if __name__ == '__main__': preFolderPath = r"R:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250813-不分类\0-原图" dotafolderPath=r"D:\港口\港口dota" outfolderPath=r"D:\港口\切片结果" tiffPaths = find_tif_files_pathlib(preFolderPath) txtPaths = find_txt_files_pathlib(dotafolderPath) match_results=matchTiff_Txt(tiffPaths,txtPaths) for tiffID in match_results: match_meta=match_results[tiffID] sliceTiFFAndTxt(match_meta,outfolderPath) preFolderPath = r"R:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250818-不分类\0-原图" dotafolderPath=r"D:\港口\港口dota" outfolderPath=r"D:\港口\切片结果" tiffPaths = find_tif_files_pathlib(preFolderPath) txtPaths = find_txt_files_pathlib(dotafolderPath) match_results=matchTiff_Txt(tiffPaths,txtPaths) for tiffID in match_results: match_meta=match_results[tiffID] sliceTiFFAndTxt(match_meta,outfolderPath) preFolderPath = r"R:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250826-不分类\0-原图" dotafolderPath=r"D:\港口\港口dota" outfolderPath=r"D:\港口\切片结果" tiffPaths = find_tif_files_pathlib(preFolderPath) txtPaths = find_txt_files_pathlib(dotafolderPath) match_results=matchTiff_Txt(tiffPaths,txtPaths) for tiffID in match_results: match_meta=match_results[tiffID] sliceTiFFAndTxt(match_meta,outfolderPath) preFolderPath = r"R:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250903-不分类\0-原图" dotafolderPath=r"D:\港口\港口dota" outfolderPath=r"D:\港口\切片结果" tiffPaths = find_tif_files_pathlib(preFolderPath) txtPaths = find_txt_files_pathlib(dotafolderPath) match_results=matchTiff_Txt(tiffPaths,txtPaths) for tiffID in match_results: match_meta=match_results[tiffID] sliceTiFFAndTxt(match_meta,outfolderPath) preFolderPath = r"R:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250910-不分类\0-原图" dotafolderPath=r"D:\港口\港口dota" outfolderPath=r"D:\港口\切片结果" tiffPaths = find_tif_files_pathlib(preFolderPath) txtPaths = find_txt_files_pathlib(dotafolderPath) match_results=matchTiff_Txt(tiffPaths,txtPaths) for tiffID in match_results: match_meta=match_results[tiffID] sliceTiFFAndTxt(match_meta,outfolderPath)