Python分类文件(大疆精灵4多光谱版PM4影象分类)

任务描述

大疆精灵4多光谱版有六个通道,每次拍摄都会生成六张图片。图片的顺序为 0. 可见光 1. 蓝光 2. 绿光 3. 红光 4. 红边 5. 近红外。
大疆PM4
在用其他软件进行拼接时需要对六个通道的照片分别进行拼接,而不同通道的图片仅是最后一位数字不同,因此要将相同通道的图片放到一起,易于利用软件拼接。
大疆PM4拍摄结果

代码实现

基于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)
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值