【Python编程】将同一种图片分类到同一文件夹中

一、数据结构如下:



二、编程工具:Jupyter-Notebook

三、代码:

python 复制代码
import os
import cv2
import shutil

path0=os.getcwd()+'\\apple\\RGB'
path1=os.getcwd()+'\\apple\\tof_confidence'
path2=os.getcwd()+'\\apple\\tof_depth'
path3=os.getcwd()+'\\apple\\tof_depthRGB'
path4=os.getcwd()+'\\apple\\tof_points'
path5=os.getcwd()+'\\apple\\tof_intensity'

if not os.path.exists(path0):
    os.makedirs(path0)
if not os.path.exists(path1):
    os.makedirs(path1)
if not os.path.exists(path2):
    os.makedirs(path2)
if not os.path.exists(path3):
    os.makedirs(path3)
if not os.path.exists(path4):
    os.makedirs(path4)
if not os.path.exists(path5):
    os.makedirs(path5)
    
path6=r'E:\Jupyter-Notebook\Apple-1'
folders=os.listdir(path6)
print(folders)


c_num = 1
dr_num =1
i_num = 1
r_num = 1
d_num = 1
p_num = 1


for folder in folders:
    path7=path6+'/'+folder
    fd=os.listdir(path7)
    print(fd)
    
    for f in fd:
        path8=path7+'/'+f
        images=os.listdir(path8)
    
        for image in images:
                
                if image.startswith('confidence'):
                    img=cv2.imread(path8+"/"+image)
                    cv2.imshow('img',img)
                    cv2.waitKey(1)
                    cv2.destroyAllWindows()
                    cv2.imwrite(r'E:\Jupyter-Notebook\apple\tof_confidence\no'+ str(c_num) + '.png', img)
                    c_num += 1
                    
                elif image.startswith('depthRGB'):
                    img=cv2.imread(path8+"/"+image)
                    cv2.imshow('img',img)
                    cv2.waitKey(1)
                    cv2.destroyAllWindows()
                    cv2.imwrite(r'E:\Jupyter-Notebook\apple\tof_depthRGB\no'+ str(dr_num) + '.png', img)
                    dr_num += 1
                    
                elif image.startswith('intensity'):
                    img=cv2.imread(path8+"/"+image)
                    cv2.imshow('img',img)
                    cv2.waitKey(1)
                    cv2.destroyAllWindows()
                    cv2.imwrite(r'E:\Jupyter-Notebook\apple\tof_intensity\no'+ str(i_num) + '.png', img)
                    i_num += 1
                    
                elif image.startswith('RGB'):
                    img=cv2.imread(path8+"/"+image)
                    cv2.imshow('img',img)
                    cv2.waitKey(1)
                    cv2.destroyAllWindows()
                    cv2.imwrite(r'E:\Jupyter-Notebook\apple\RGB\no'+ str(r_num) + '.png', img)
                    r_num += 1
                    
                elif image.startswith('depth.png'):
                    img=cv2.imread(path8+"/"+image)
                    cv2.imshow('img',img)
                    cv2.waitKey(1)
                    cv2.destroyAllWindows()
                    cv2.imwrite(r'E:\Jupyter-Notebook\apple\tof_depth\no'+ str(d_num) + '.png', img)
                    d_num += 1
                                
                elif image.endswith('.pcd'):
                    img=path8+'/'+image
                    shutil.copy(img,r'E:\Jupyter-Notebook\apple\tof_points\no'+ str(p_num) + '.pcd')
                    p_num +=1

注:代码文件与图片文件同处一个文件夹,如上例,在Jupyter-Notebook文件夹

相关推荐
BulingQAQ13 分钟前
论文阅读:PET/CT Cross-modal medical image fusion of lung tumors based on DCIF-GAN
论文阅读·深度学习·生成对抗网络·计算机视觉·gan
belldeep20 分钟前
python:reportlab 将多个图片合并成一个PDF文件
python·pdf·reportlab
CV-King2 小时前
opencv实战项目(三十):使用傅里叶变换进行图像边缘检测
人工智能·opencv·算法·计算机视觉
禁默2 小时前
2024年计算机视觉与艺术研讨会(CVA 2024)
人工智能·计算机视觉
FreakStudio3 小时前
全网最适合入门的面向对象编程教程:56 Python字符串与序列化-正则表达式和re模块应用
python·单片机·嵌入式·面向对象·电子diy
丶21363 小时前
【CUDA】【PyTorch】安装 PyTorch 与 CUDA 11.7 的详细步骤
人工智能·pytorch·python
春末的南方城市3 小时前
FLUX的ID保持项目也来了! 字节开源PuLID-FLUX-v0.9.0,开启一致性风格写真新纪元!
人工智能·计算机视觉·stable diffusion·aigc·图像生成
jndingxin4 小时前
OpenCV视频I/O(14)创建和写入视频文件的类:VideoWriter介绍
人工智能·opencv·音视频
_.Switch4 小时前
Python Web 应用中的 API 网关集成与优化
开发语言·前端·后端·python·架构·log4j
一个闪现必杀技4 小时前
Python入门--函数
开发语言·python·青少年编程·pycharm