任务描述
大疆精灵4多光谱版有六个通道,每次拍摄都会生成六张图片。图片的顺序为 0. 可见光 1. 蓝光 2. 绿光 3. 红光 4. 红边 5. 近红外。
在用其他软件进行拼接时需要对六个通道的照片分别进行拼接,而不同通道的图片仅是最后一位数字不同,因此要将相同通道的图片放到一起,易于利用软件拼接。
代码实现
基于Python的os库和shutil库进行文件转移操作,代码如下
"""
author: Shuai-jie Shen 沈帅杰
CSDN: https://blog.csdn.net/weixin_45452300
公众号: AgBioIT
"""
import shutil
import os
path = r'E:\博士期间记录\照片\站内\高光谱\20210811'
files = os.listdir(path)
rgb_file = path+'\\rgb'
red_file = path+'\\red'
green_file = path+'\\green'
blue_file = path+'\\blue'
red_edge_file = path+'\\red_edge'
Near_infrared_file = path+'\\nir'
for f in files:
filename, suffix = os.path.splitext(f) # filename是文件名 suffix是文件后缀
if not os.path.exists(rgb_file):
os.makedirs(rgb_file)
if not os.path.exists(red_file):
os.makedirs(red_file)
if not os.path.exists(green_file):
os.makedirs(green_file)
if not os.path.exists(blue_file):
os.makedirs(blue_file)
if not os.path.exists(red_edge_file):
os.makedirs(red_edge_file)
if not os.path.exists(Near_infrared_file):
os.makedirs(Near_infrared_file)
if filename[-1] == '0':
shutil.move(os.path.join(path, f), rgb_file)
elif filename[-1] == '1':
shutil.move(os.path.join(path, f), blue_file)
elif filename[-1] == '2':
shutil.move(os.path.join(path, f), green_file)
elif filename[-1] == '3':
shutil.move(os.path.join(path, f), red_file)
elif filename[-1] == '4':
shutil.move(os.path.join(path, f), red_edge_file)
elif filename[-1] == '5':
shutil.move(os.path.join(path, f), Near_infrared_file)
结果如下
如果你还想变回来可以运行一下代码
def de_classing_file(file_dir):
"""
将文件夹子文件移动到上级目录
:param file_dir:
:return:
"""
def get_all_files(dir, types):
"""
获取文件夹列表函数
:param dir: 文件夹所在位置
:return: 文件夹内文件名称列表
"""
files_ = []
lit = os.listdir(dir)
for i in range(0, len(lit)):
path = os.path.join(dir, lit[i])
if os.path.isdir(path):
files_.extend(get_all_files(path, types))
if os.path.isfile(path) and os.path.splitext(path)[1].lower() in types:
files_.append(path)
return files_
files = get_all_files(file_dir, [".tif", ".tiff", ".jpg"])
for i in files:
shutil.move(i, file_dir)
path = r'E:\P4M_image_progressing\original_image\jining_images\rotation_exp20211013'
e_classing_file(path)