本段代码对文件夹中所有图片进行处理:转换为灰度图、增加对比度(contrast_factor)、增加亮度(brightness_factor)
需要更改输入文件夹和输出文件夹路径
from PIL import Image, ImageEnhance
import os
'''
本段代码对文件夹中所有图片进行处理:转换为灰度图、增加对比度(contrast_factor)、增加亮度(brightness_factor)
'''
def adjust_image_properties(image_path, output_path, contrast_factor=1.2, brightness_factor=1.2):
# 打开图片
image = Image.open(image_path)
# 调整颜色饱和度
image = image.convert("L") # 转换为灰度图像
# 调整对比度
contrast = ImageEnhance.Contrast(image)
image = contrast.enhance(contrast_factor)
# 调整亮度
brightness = ImageEnhance.Brightness(image)
image = brightness.enhance(brightness_factor)
# 保存处理后的图片
image.save(output_path)
def process_images(input_folder, output_folder):
# 确保输出文件夹存在
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# 遍历输入文件夹中的所有图片
for filename in os.listdir(input_folder):
if filename.endswith(('.jpg', '.jpeg', '.png')):
input_path = os.path.join(input_folder, filename)
output_path = os.path.join(output_folder, filename)
# 调整图片属性并保存
adjust_image_properties(input_path, output_path)
if __name__ == "__main__":
input_folder = "D:\Desktop\yolov8\yolov8-main-low-color\Data_big\split\images\\val" # 替换为实际的输入文件夹路径
output_folder = "D:\Desktop\yolov8\yolov8-main-low-color\Data_big\split\images\\val_2" # 替换为实际的输出文件夹路径
process_images(input_folder, output_folder)