标签:and rgb split pytho end pre 文件的 error really
# -*- coding: utf-8 -*- import SimpleITK as sitk import matplotlib.pyplot as plt import time import os import sys import re import csv import numpy as np from os import listdir # import pandas as pd import numpy as np import os from os import listdir import math import threading, subprocess def mkdir(path): # 去除首位空格 path = path.strip() # 去除尾部 \ 符号 path = path.rstrip("\\") # 判断路径是否存在 # 存在 True # 不存在 False isExists = os.path.exists(path) # 判断结果 if not isExists: # 如果不存在则创建目录 # 创建目录操作函数 os.makedirs(path) print(path + ‘ create success‘) return True else: # 如果目录存在则不创建,并提示目录已存在 print(path + ‘ is existed‘) return False def is_image3d_file(filename): return any(filename.endswith(extension) for extension in [".raw"]) def is_imagegz_file(filename): return any(filename.endswith(extension) for extension in [".gz"]) def get_dir(data_path=""):#获取一个文件夹下所有的子文件夹 返回一个list dir_name = listdir(data_path) dir_name.sort() dir_really = [] for i in range(len(dir_name)): # 去除里面的filenames.txt dir_name[i] = os.path.join(data_path, dir_name[i]) if os.path.isdir(dir_name[i]): dir_name[i]=dir_name[i].replace(‘\\‘, ‘/‘) dir_really.append(dir_name[i]+‘/‘) return dir_really def is_excel_file(filename): return any(filename.endswith(extension) for extension in [".xlsx"]) def get_raw_file(dir_path=""):#获取一个文件夹下所有的文件路径 dir_really=dir_path files_data=listdir(dir_really) files_data_copy = files_data for file in files_data: if is_image3d_file(file)==False: files_data_copy.remove(file) files_data=files_data_copy # #Todo 对文件夹下所有的文件进行排序 排序规则自己定义 # files_data.sort(key=lambda x: int(x.split(‘.‘)[-2])) # # print(files_data) # 已经获取了该文件夹下所有的排序好名称 for j in range(len(files_data)): files_data[j] = os.path.join(dir_really, files_data[j]) files_data[j]=files_data[j].replace(‘\\‘,‘/‘) # print(files_data) # 现在获取了该文件夹下所有的子文件的路径 并且排序 return files_data pass def get_gz_file(dir_path=""):#获取一个文件夹下所有的文件路径 dir_really=dir_path files_data=listdir(dir_really) files_data_copy = files_data for file in files_data: if is_imagegz_file(file)==False: files_data_copy.remove(file) files_data=files_data_copy # #Todo 对文件夹下所有的文件进行排序 排序规则自己定义 # files_data.sort(key=lambda x: int(x.split(‘.‘)[-2])) # # print(files_data) # 已经获取了该文件夹下所有的排序好名称 for j in range(len(files_data)): files_data[j] = os.path.join(dir_really, files_data[j]) files_data[j]=files_data[j].replace(‘\\‘,‘/‘) # print(files_data) # 现在获取了该文件夹下所有的子文件的路径 并且排序 return files_data pass def read_image_zhuang_float(dst = ‘I:/program/python/heart/113.raw‘): if is_image3d_file(dst): image = np.fromfile(file=dst, dtype=np.float32) n_pieces = image.shape[0] // (512 * 512) image = image.reshape(n_pieces, 512, 512) image = image.astype(np.float32) return image else: print("file name error") def read_image_zhuang_int16(dst = ‘I:/program/python/heart/113.raw‘): if is_image3d_file(dst): image = np.fromfile(file=dst, dtype=np.int16) n_pieces = image.shape[0] // (512 * 512) image = image.reshape(n_pieces, 512, 512) image = image.astype(np.float32) return image else: print("file name error") def read_file_from_txt(txt_path): files=[] for line in open(txt_path, ‘r‘): files.append(line.strip()) # print(files) return files def ReadMhdImage(MhdImagePath=‘D:/data/data_heart/ImageRegistration/Data/OutPut/40_Moving/139/40/result.1.mhd‘): data = sitk.ReadImage(MhdImagePath) scan = sitk.GetArrayFromImage(data) return scan if __name__ == ‘__main__‘: ImageSrcDirRoot=‘D:/data/data_heart/ImageRegistration/Data/OutPut/40_Moving/‘ ImageDirList=get_dir(ImageSrcDirRoot) ImageTxtDir=‘D:/data/data_heart/ImageRegistration/Data/OutPut/40_MovingTxtList/‘ ImageErrorTxtDir = ‘D:/data/data_heart/ImageRegistration/Data/OutPut/40_MovingTxtErrorList/‘ print(ImageDirList) for ImageDirIndex in range(len(ImageDirList)): ImageDirOne=ImageDirList[ImageDirIndex] print(ImageDirOne) ImageDirNum=ImageDirOne.split(‘/‘)[-2] print(ImageDirNum) TxtFileRoot=os.path.join(ImageTxtDir,ImageDirNum+‘.txt‘) TxtFileRoot=TxtFileRoot.replace(‘\\‘,‘/‘) print(TxtFileRoot) TxtErrorFileRoot=os.path.join(ImageErrorTxtDir,ImageDirNum+‘.txt‘) TxtErrorFileRoot=TxtErrorFileRoot.replace(‘\\‘,‘/‘) print(TxtErrorFileRoot) TxtFile=open(TxtFileRoot, ‘w‘) TxtErrorFile=open(TxtErrorFileRoot,‘w‘) ImageMovingDir=get_dir(ImageDirOne) for ImageMovingDirIndex in range(len(ImageMovingDir)): ImageMovingDirOne=ImageMovingDir[ImageMovingDirIndex] # print(ImageMovingDirOne) ImageNeedToTest=os.path.join(ImageMovingDirOne,‘result.1.mhd‘) print(ImageNeedToTest) ImageData=ReadMhdImage(MhdImagePath=ImageNeedToTest) if(np.sum(ImageData)==0): print(ImageNeedToTest,‘is Null‘) TxtErrorFile.write(ImageNeedToTest+‘\n‘) continue TxtFile.write(ImageNeedToTest+‘\n‘) TxtFile.close() # MhdImagePath=‘D:/data/data_heart/ImageRegistration/Data/OutPut/40_Moving/139/40/result.1.mhd‘ # data = sitk.ReadImage(MhdImagePath) # scan = sitk.GetArrayFromImage(data) # print(scan.shape) # print(np.sum(scan)) pass
标签:and rgb split pytho end pre 文件的 error really
原文地址:https://www.cnblogs.com/William-xh/p/13986753.html