python处理文件和图片
1. 文件和目录操作
1.1 使用 os
模块
os
模块提供了许多与操作系统交互的函数,常用于文件和目录的操作。
1.1.1 列出目录内容
import os# 列出指定目录中的所有文件和子目录
data_folder = 'data'
files_and_folders = os.listdir(data_folder)
print(files_and_folders) # 输出: ['file1.txt', 'file2.jpg', 'subfolder', ...]
1.1.2 创建目录
# 创建新目录
new_folder = 'new_folder'
if not os.path.exists(new_folder):os.makedirs(new_folder)
1.1.3 删除目录
# 删除空目录
os.rmdir('empty_folder')# 删除非空目录及其所有内容
os.removedirs('non_empty_folder')
1.1.4 路径拼接
# 构建完整路径
base_path = 'base'
sub_path = 'sub'
full_path = os.path.join(base_path, sub_path)
print(full_path) # 输出: 'base/sub'
1.1.5 获取文件信息
# 获取文件大小
file_size = os.path.getsize('file.txt')
print(file_size) # 输出: 文件大小(字节)# 检查路径是否存在
exists = os.path.exists('path')
print(exists) # 输出: True 或 False# 检查路径是否为文件
is_file = os.path.isfile('path')
print(is_file) # 输出: True 或 False# 检查路径是否为目录
is_dir = os.path.isdir('path')
print(is_dir) # 输出: True 或 False
1.2 使用 shutil
模块
shutil
模块提供了高级文件操作功能,如复制、移动和删除文件和目录。
1.2.1 复制文件
import shutil# 复制文件
src_path = 'source_file.txt'
dst_path = 'destination_file.txt'
shutil.copy(src_path, dst_path)
1.2.2 移动文件
# 移动文件
src_path = 'source_file.txt'
dst_path = 'destination_folder/source_file.txt'
shutil.move(src_path, dst_path)
1.2.3 删除文件
# 删除文件
os.remove('file_to_delete.txt')
2. 图片操作
2.1 使用 PIL
(Pillow)库
PIL
(Pillow)是Python中处理图片的主要库。
2.1.1 安装 Pillow
pip install pillow
2.1.2 加载图片
from PIL import Image# 加载图片
image_path = 'image.jpg'
image = Image.open(image_path)
2.1.3 显示图片
# 显示图片
image.show()
2.1.4 保存图片
# 保存图片
image.save('new_image.png')
2.1.5 裁剪图片
# 裁剪图片
box = (100, 100, 400, 400)
cropped_image = image.crop(box)
cropped_image.save('cropped_image.jpg')
2.1.6 调整图片大小
# 调整图片大小
resized_image = image.resize((200, 200))
resized_image.save('resized_image.jpg')
2.1.7 旋转图片
# 旋转图片
rotated_image = image.rotate(90)
rotated_image.save('rotated_image.jpg')
2.1.8 转换图片格式
# 转换图片格式
image.convert('L').save('grayscale_image.jpg') # 转换为灰度图
3. 示例:批量处理图片
结合文件和图片操作,以下是一个示例,展示如何批量处理图片。
3.1 批量重命名和调整图片大小
import os
from PIL import Image# 原始图片文件夹路径
data_folder = 'data'
# 目标文件夹路径
output_folder = 'output'# 确保目标文件夹存在
if not os.path.exists(output_folder):os.makedirs(output_folder)# 遍历原始文件夹中的所有图片
image_files = [f for f in os.listdir(data_folder) if f.endswith(('.png', '.jpg', '.jpeg'))]for idx, image_file in enumerate(image_files):# 加载图片image_path = os.path.join(data_folder, image_file)image = Image.open(image_path)# 调整图片大小resized_image = image.resize((200, 200))# 生成新的图片名称new_image_name = f"{idx + 1:04d}.jpg"# 保存图片output_path = os.path.join(output_folder, new_image_name)resized_image.save(output_path)print("批量处理完成!")
案例:
现在有一个场景,我们在进行深度学习时,需要处理的图片结构如下:
原始数据结构如下:
data/├── class1_1.jpg├── class1_2.jpg├── class2_1.jpg├── class2_2.jpg└── ...
希望将这些图片整理到一个名为 train
的文件夹中,按照类别创建子文件夹,并将图片重命名为从1到n的编号。最终的文件结构如下:
train/├── class1/│ ├── 1.jpg│ ├── 2.jpg│ └── ...├── class2/│ ├── 1.jpg│ ├── 2.jpg│ └── ...└── ...
-
导入必要的库
import os import shutil
-
定义文件夹路径
data_folder = 'data' train_folder = 'train'
-
确保目标文件夹存在
if not os.path.exists(train_folder):os.makedirs(train_folder)
-
遍历原始文件夹中的所有图片
image_files = [f for f in os.listdir(data_folder) if f.endswith(('.png', '.jpg', '.jpeg'))]#这个意思是[表达式 for 元素 in 可迭代对象 if 条件] #对于 可迭代对象 中的每一个 元素,如果 元素 满足 条件,则将其通过 表达式 转换后放入新列表中。
-
统计每个类别的图片数量
class_counts = {}
-
处理每个图片文件
for image_file in image_files:# 提取类别名称class_name = image_file.split('_')[0]# 创建类别文件夹class_folder = os.path.join(train_folder, class_name)if not os.path.exists(class_folder):os.makedirs(class_folder)# 统计当前类别图片数量if class_name not in class_counts:class_counts[class_name] = 0class_counts[class_name] += 1# 生成新的图片名称new_image_name = f"{class_counts[class_name]:04d}.jpg" # 4位数字序号,可根据需要调整# 源文件路径和目标文件路径src_path = os.path.join(data_folder, image_file)dst_path = os.path.join(class_folder, new_image_name)# 复制并重命名图片shutil.copy(src_path, dst_path)
-
打印整理完成信息
print("数据整理完成!")
完整代码
import os
import shutil# 原始数据文件夹路径
data_folder = 'data'
# 目标训练文件夹路径
train_folder = 'train'# 确保目标文件夹存在
if not os.path.exists(train_folder):os.makedirs(train_folder)# 遍历原始文件夹中的所有图片
image_files = [f for f in os.listdir(data_folder) if f.endswith(('.png', '.jpg', '.jpeg'))]# 统计每个类别的图片数量
class_counts = {}for image_file in image_files:# 提取类别名称class_name = image_file.split('_')[0]# 创建类别文件夹class_folder = os.path.join(train_folder, class_name)if not os.path.exists(class_folder):os.makedirs(class_folder)# 统计当前类别图片数量if class_name not in class_counts:class_counts[class_name] = 0class_counts[class_name] += 1# 生成新的图片名称new_image_name = f"{class_counts[class_name]:04d}.jpg" # 4位数字序号,可根据需要调整 比如1.jpg、2.jpg(为了在class1文件夹中)# 源文件路径和目标文件路径src_path = os.path.join(data_folder, image_file) data/class1_1.jpgdst_path = os.path.join(class_folder, new_image_name) train/class1/1.jpg# 复制并重命名图片shutil.copy(src_path, dst_path)print("数据整理完成!")