使用python将一个文件夹内所有的红外图像和可见光图像分成两个不同的文件夹
时间: 2024-05-06 21:19:53 浏览: 178
以下是Python代码,可以将一个文件夹内所有的红外图像和可见光图像分成两个不同的文件夹:
```python
import os
import shutil
# 源文件夹路径
src_dir = 'path/to/source/folder'
# 目标文件夹路径
dest_dir_ir = 'path/to/destination/folder/for/infrared/images'
dest_dir_visi = 'path/to/destination/folder/for/visible/images'
# 遍历源文件夹内的文件
for file_name in os.listdir(src_dir):
# 获取文件路径
file_path = os.path.join(src_dir, file_name)
# 判断文件类型
if file_name.endswith('.jpg') or file_name.endswith('.jpeg'):
# 可见光图像
dest_path = os.path.join(dest_dir_visi, file_name)
shutil.copy(file_path, dest_path)
elif file_name.endswith('.bmp') or file_name.endswith('.png'):
# 红外图像
dest_path = os.path.join(dest_dir_ir, file_name)
shutil.copy(file_path, dest_path)
```
将代码中的 `src_dir` 替换为你的源文件夹路径,将 `dest_dir_ir` 和 `dest_dir_visi` 替换为你想要存储红外图像和可见光图像的目标文件夹路径。运行代码后,程序会遍历源文件夹内的所有文件,并将它们分成两类,然后将它们分别复制到目标文件夹中。
阅读全文
相关推荐














