1. opencv读取图片
Define:cv_imread(filePath)
Function:在python3中由于python3字符串采用utf8编码,cv2.imread将utf8当作本地码(如GBK),可通过imdecode()函数读取带有中文路径的图片
Parameters:
filePath
:图片文件绝对路径
#在python3中由于python3字符串采用utf8编码,cv2.imread将utf8当作本地码(如GBK),可通过imdecode()函数读取带有中文路径的图片。
def cv_imread(filePath):
cv_img=cv2.imdecode(np.fromfile(filePath,dtype=np.uint8),-1)
try:
if cv_img.any(): #读取图片成功
## imdecode读取的是rgb,如果后续需要opencv处理的话,需要转换成bgr,转换后图片颜色会变化
cv_img=cv2.cvtColor(cv_img,cv2.COLOR_RGB2BGR)
return cv_img
except AttributeError: #读取图片失败,此时cv_img为None,调用cv_img.any()会产生ValueError
print('ValueError: {}'.format(filePath))
return None
2. list to string
def list2str(li, dvide):
return dvide.join(list(map(str, li)))
示例:
In[95]: list2str([23,5,‘sd’], ‘,’)
Out[95]: ‘23,5,sd’
3. 截取路径名
def get_path_name(directory, num=1, reverse=False):
if num == 0:
pos = directory.rfind('.')
pos_start = directory.rfind('\\')
return directory[pos_start+1:pos] # 若无后缀名,则返回none
elif reverse:
return directory[:directory.rfind(directory.split('\\')[-num])]
else:
return directory.split('\\')[-num]
示例:
In[5]: get_path_name(r’D:\working\edge_detection\kcacp-master\python\data\output\blurry_output.txt’)
Out[5]: ‘blurry_output.txt’
In[6]: get_path_name(r’D:\working\edge_detection\kcacp-master\python\data\output\blurry_output.txt’,2)
Out[6]: ‘output’
In[7]: get_path_name(r’D:\working\edge_detection\kcacp-master\python\data\output\blurry_output.txt’,1, True)
Out[7]: ‘D:\working\edge_detection\kcacp-master\python\data\output’
4. 搜索目录下文件
import glob
search_dir = '/home/xxxx'
# 搜索/home/xxxx文件夹下所有文件/文件夹(若为空文件夹,则以该文件夹结尾,无后缀ext)
glob.glob(search_dir+'/**', recursive=True)
# 搜索/home/xxxx文件夹下包括子文件夹所有以.py结尾的文件
glob.glob(search_dir+'/**/*.py', recursive=True)
# 返回一个list,其中包括所有搜索到的文件/文件夹的绝对路径
def glob_files(directory, ext_list=None, search_deep=0, search_add=False, search_all=False):
"""
目录下搜索文件,并返回list
:param directory: 搜索目录
:param ext_list: 目标后缀list,为None时搜索所有文件和目录
:param search_deep: 搜索深度,为0时搜索当前目录下所有文件;为1时搜索当前目录下所有子目录下所有文件
:param search_add: 结合search_deep使用,为False时只返回search_deep下的搜索结果;为True时返回当前目录直到search_deep下所有结果
:param search_all: 是否递归搜索directory下所有文件,为True时其他三个参数中仅ext_list生效(ext_list为None时返回目录及所有子目录下所有格式的文件及空文件夹)
:return:
"""
file_list = []
if search_all:
if ext_list == None:
file_list = glob.glob(directory + '/**', recursive=True)
else:
for ext in ext_list:
file_list += glob.glob(directory + '/**/*.{}'.format(ext), recursive=True)
else:
glob_content = ''
if search_add:
for i in range(search_deep+1):
glob_content += '/*'
if ext_list == None:
file_list += glob.glob(directory + '{}*'.format(glob_content))
else:
for ext in ext_list:
file_list += glob.glob(directory + '{}.{}'.format(glob_content, ext))
else:
for i in range(search_deep+1):
glob_content += '/*'
if ext_list == None:
file_list += glob.glob(directory + '{}*'.format(glob_content))
else:
for ext in ext_list:
file_list += glob.glob(directory + '{}.{}'.format(glob_content, ext))
return file_list
示例:
temp = glob_files(r'D:\Desktop\shishuai.yan\Desktop\My Nutstore\CODE\Python', ['py'], search_deep=0, search_add=False, search_all=True)
print(len(temp))
print(temp)
871
[‘D:\Desktop\yan\Desktop\My Nutstore\CODE\Python\cos_calculate.py’, ‘D:\Desktop\yan\Desktop\My Nutstore\CODE\Python\Computer Vision\deep_learning\task_one\common\layers.py’, …]