145 lines
5.1 KiB
Python
145 lines
5.1 KiB
Python
![]() |
# -*- coding:utf-8 -*-
|
|||
|
|
|||
|
'''
|
|||
|
本模块是各种工具函数
|
|||
|
'''
|
|||
|
|
|||
|
import sys, os
|
|||
|
import numpy as np
|
|||
|
|
|||
|
def get_path():
|
|||
|
'''
|
|||
|
本函数说明:https://pythonhosted.org/PyInstaller/runtime-information.html#using-sys-executable-and-sys-argv-0
|
|||
|
:return: 返回运行程序的绝对路径
|
|||
|
'''
|
|||
|
frozen = 'not'
|
|||
|
if getattr(sys, 'frozen', False):
|
|||
|
# we are running in a bundle
|
|||
|
bundle_dir = sys._MEIPASS
|
|||
|
|
|||
|
# print('we are running in a bundle(pyinstaller打包程序)!')
|
|||
|
else:
|
|||
|
# we are running in a normal Python environment
|
|||
|
# bundle_dir = os.path.dirname(os.path.abspath(__file__)) # 此行代码返回的是本文件的路径,而不是本文件所导入的文件的路径
|
|||
|
bundle_dir = os.getcwd()
|
|||
|
# print('we are running in a normal Python environment(非pyinstaller打包程序)!')
|
|||
|
|
|||
|
return bundle_dir
|
|||
|
|
|||
|
|
|||
|
def get_resource_path(relative_path):
|
|||
|
'''
|
|||
|
本函数说明:https://www.zacoding.com/en/post/python-selenium-to-exe/
|
|||
|
:param relative_path:
|
|||
|
:return:
|
|||
|
'''
|
|||
|
try:
|
|||
|
base_path = sys._MEIPASS
|
|||
|
except Exception:
|
|||
|
base_path = os.getcwd()
|
|||
|
return os.path.join(base_path, relative_path)
|
|||
|
|
|||
|
|
|||
|
def percentile_stretching(img, lowPercentile=0, highPercentile=100, minout=0, maxout=255):
|
|||
|
'''
|
|||
|
本程序用于拉伸影像
|
|||
|
:param img:
|
|||
|
:param lowPercentile:
|
|||
|
:param highPercentile:
|
|||
|
:param minout:
|
|||
|
:param maxout:
|
|||
|
:return:
|
|||
|
'''
|
|||
|
if len(img.shape) == 2:
|
|||
|
low = np.percentile(img, lowPercentile)
|
|||
|
up = np.percentile(img, highPercentile)
|
|||
|
|
|||
|
img_new = ((img - low) / (up - low)) * (maxout - minout) + minout
|
|||
|
img_new[img_new < minout] = minout
|
|||
|
img_new[img_new > maxout] = maxout
|
|||
|
img_out = np.uint8(img_new)
|
|||
|
return img_out
|
|||
|
else: # 对于彩色照片,需要先单独对每个波段拉伸
|
|||
|
img_new = np.empty(img.shape)
|
|||
|
for i in range(img.shape[2]):
|
|||
|
low = np.percentile(img[:, :, i], lowPercentile)
|
|||
|
up = np.percentile(img[:, :, i], highPercentile)
|
|||
|
|
|||
|
img_new[:, :, i] = minout + ((img[:, :, i] - low) / (up - low)) * (maxout - minout)
|
|||
|
img_new[:, :, i][img_new[:, :, i] < minout] = minout
|
|||
|
img_new[:, :, i][img_new[:, :, i] > maxout] = maxout
|
|||
|
img_out = np.uint8(img_new)
|
|||
|
return img_out
|
|||
|
|
|||
|
|
|||
|
def return_file_path(out, filepath, filename, model='image'):
|
|||
|
'''
|
|||
|
本程序功能:在filepath中寻找所有包含filename所有文件(filename1、filename2),然后返回一个filename3
|
|||
|
:param out: 永远传入一个空list:[];用于存储所有递归调用
|
|||
|
:param filepath:
|
|||
|
:param filename:
|
|||
|
:param model: 有两个模式:image 和 spectral
|
|||
|
:return:
|
|||
|
'''
|
|||
|
|
|||
|
# 出现此处代码的原因是:当次函数定义执行后,函数定义就包含了out参数的引用,
|
|||
|
# 而out参数是可变参数,每一次调用次函数都会改变out的值,所有不能保证每次调用此函数时out==[]。
|
|||
|
# 当第二次调用此方程时
|
|||
|
# if out != []:
|
|||
|
# if filename not in os.path.splitext(out[-1]):
|
|||
|
# out = []
|
|||
|
|
|||
|
if model == 'image':
|
|||
|
files = os.listdir(filepath)
|
|||
|
for s in files:
|
|||
|
abspath = os.path.join(filepath, s)
|
|||
|
if os.path.isfile(abspath):
|
|||
|
tmp = os.path.splitext(os.path.split(abspath)[1])[0]
|
|||
|
if tmp not in out: # 防止重复记录
|
|||
|
if filename in tmp:
|
|||
|
out.append(tmp)
|
|||
|
else:
|
|||
|
pass
|
|||
|
# print('没有进来')
|
|||
|
elif os.path.isdir(abspath):
|
|||
|
return_file_path(out, abspath, filename)
|
|||
|
out.sort(key=lambda x: int(x.replace(filename, '')))
|
|||
|
|
|||
|
if out == []:
|
|||
|
x = filename + str(0)
|
|||
|
return os.path.join(filepath, x), 0
|
|||
|
if out != []:
|
|||
|
number = int(out[-1].replace(filename, '')) + 1
|
|||
|
x = filename + str(number)
|
|||
|
return os.path.join(filepath, x), number
|
|||
|
elif model == 'spectral':
|
|||
|
files = os.listdir(filepath)
|
|||
|
for s in files:
|
|||
|
abspath = os.path.join(filepath, s)
|
|||
|
if os.path.isfile(abspath) and os.path.splitext(s)[1] == '.txt':
|
|||
|
tmp = os.path.splitext(os.path.split(abspath)[1])[0]
|
|||
|
if tmp not in out: # 防止重复记录
|
|||
|
if filename in tmp:
|
|||
|
out.append(tmp)
|
|||
|
elif os.path.isdir(abspath):
|
|||
|
return_file_path(out, abspath, filename)
|
|||
|
out.sort(key=lambda x: int(x.replace(filename, '')))
|
|||
|
|
|||
|
if out == []:
|
|||
|
x = filename + str(0) + '.txt'
|
|||
|
return os.path.join(filepath, x), 0
|
|||
|
if out != []:
|
|||
|
number = int(out[-1].replace(filename, '')) + 1 # 现存最大文件号 + 1
|
|||
|
x = filename + str(number) + '.txt'
|
|||
|
return os.path.join(filepath, x), number
|
|||
|
|
|||
|
|
|||
|
if __name__ == '__main__':
|
|||
|
# print(get_path())
|
|||
|
|
|||
|
x = return_file_path([], r'D:\delete', 'sss', model='spectral')
|
|||
|
print(x)
|
|||
|
|
|||
|
# y = return_file_path([], r'D:\delete', 'rr')
|
|||
|
# print(y)
|