|
|
import os, sys, shutil, json
|
|
|
from osgeo import gdal
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
|
|
# 判断路径是否是目录
|
|
|
def isDir(dirPath: str):
|
|
|
return os.path.exists(dirPath) and os.path.isdir(dirPath)
|
|
|
|
|
|
|
|
|
# 对参数进行去重
|
|
|
def are_equal(*args):
|
|
|
# 使用集合去除重复值,如果长度为1,则说明所有元素相等
|
|
|
return len(set(args)) == 1
|
|
|
|
|
|
|
|
|
if are_equal(17136, 17136, 17136, 17136, 17136, 17136):
|
|
|
print("ok")
|
|
|
|
|
|
|
|
|
# 删除一个目录或者文件
|
|
|
def rmPath(fileOrDirPath: str):
|
|
|
if os.path.exists(fileOrDirPath):
|
|
|
if os.path.isdir(fileOrDirPath):
|
|
|
shutil.rmtree(fileOrDirPath)
|
|
|
elif os.path.isfile(fileOrDirPath):
|
|
|
os.remove(fileOrDirPath)
|
|
|
|
|
|
|
|
|
# 删除目录
|
|
|
def rmDir(dirPath: str) -> bool:
|
|
|
if not os.path.isdir(dirPath):
|
|
|
return True
|
|
|
# 删除本地的input目录
|
|
|
try:
|
|
|
# 尝试递归删除目录及其内容
|
|
|
shutil.rmtree(dirPath)
|
|
|
# print(f"Directory {dirPath} successfully removed.")
|
|
|
except OSError as e:
|
|
|
# 如果目录无法删除,则捕获异常
|
|
|
print(f"Failed to remove directory {dirPath} :", e)
|
|
|
return False
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
# 创建目录
|
|
|
def mkDir(dirPath: str) -> bool:
|
|
|
try:
|
|
|
# 尝试递归创建目录及其父目录
|
|
|
os.makedirs(dirPath)
|
|
|
# print(f"Directory {dirPath} created successfully.")
|
|
|
except OSError as e:
|
|
|
# 如果创建目录失败,则捕获异常
|
|
|
print(f"Failed to create directory {dirPath} :", e)
|
|
|
return False
|
|
|
return True
|
|
|
|
|
|
|
|
|
# 拷贝文件
|
|
|
def copyFile(sourceFile: str, targetFile: str) -> bool:
|
|
|
try:
|
|
|
# 尝试复制文件
|
|
|
shutil.copy(sourceFile, targetFile)
|
|
|
# print(f"File {sourceFile} copied successfully.")
|
|
|
except IOError as e:
|
|
|
# 如果复制文件失败,则捕获异常
|
|
|
print(f"Failed to copy file {sourceFile}:", e)
|
|
|
return False
|
|
|
return True
|
|
|
|
|
|
|
|
|
# 将内容保持为json文件
|
|
|
def saveDataToJSON(content: list, fileName):
|
|
|
with open(fileName, "w") as f:
|
|
|
json.dump(content, f, indent=4)
|
|
|
pass
|
|
|
|
|
|
|
|
|
# 判断文件是否被其他程序访问
|
|
|
def isFileOpen(filePath):
|
|
|
try:
|
|
|
thefile = open(filePath, "r+")
|
|
|
thefile.close()
|
|
|
except IOError as e:
|
|
|
# 如果是Permission denied错误,通常意味着文件正在被其他程序访问
|
|
|
if "Permission denied" in str(e):
|
|
|
return True
|
|
|
return False
|
|
|
|
|
|
|
|
|
def saveENVIImgToPng(imgFilePath: str, pngFilePath: str):
|
|
|
# 打开 ENVI 格式的图像文件
|
|
|
# image_path = "postProcessing/Case1/reflectance/08.REF.img"
|
|
|
# 如果文件不存在进行报错并退出
|
|
|
if not os.path.isfile(imgFilePath):
|
|
|
print("img file not exist!")
|
|
|
return None
|
|
|
dataset = gdal.Open(imgFilePath, gdal.GA_ReadOnly)
|
|
|
if dataset is None:
|
|
|
print("Failed to open image")
|
|
|
return None
|
|
|
|
|
|
# 读取图像数据
|
|
|
band = dataset.GetRasterBand(1) # 仅读取第一个波段
|
|
|
image_array = band.ReadAsArray()
|
|
|
|
|
|
# 显示图像
|
|
|
plt.imshow(image_array) # 使用灰度色彩映射
|
|
|
# plt.imshow(image_array, cmap="gray") # 使用灰度色彩映射
|
|
|
# plt.colorbar()
|
|
|
# plt.show()
|
|
|
|
|
|
plt.savefig(pngFilePath)
|
|
|
|
|
|
# 关闭数据集
|
|
|
dataset = None
|
|
|
# 发货文件名
|
|
|
return pngFilePath
|
|
|
|
|
|
def printWithFlush(*args, sep=' ', end='\n', file=None, flush=True):
|
|
|
print(*args, sep=sep, end=end, file=file, flush=flush)
|
|
|
# sys.stdout.flush() |