
from PIL import Image, ImageEnhance
import osdef adjust_image_tone(image_path, output_path, r_weight=1.0, g_weight=1.0, b_weight=1.0, brightness=1.0):"""调整图片的色调、明暗,并进行去图处理。参数:image_path (str): 输入图片的路径。output_path (str): 输出图片的路径。r_weight (float): 红色通道的权重。g_weight (float): 绿色通道的权重。b_weight (float): 蓝色通道的权重。brightness (float): 亮度调整值(1.0 表示原始亮度,小于 1.0 变暗,大于 1.0 变亮)。"""img = Image.open(image_path).convert('L').convert('RGB')r, g, b = img.split()new_r = r.point(lambda i: i * r_weight)new_g = g.point(lambda i: i * g_weight)new_b = b.point(lambda i: i * b_weight)adjusted_img = Image.merge('RGB', (new_r, new_g, new_b))adjusted_img = ImageEnhance.Brightness(adjusted_img).enhance(brightness)adjusted_img.save(output_path)print(f"已处理图片: {output_path}")def batch_process_images(input_folder, output_folder, r_weight=1.0, g_weight=1.0, b_weight=1.0, brightness=1.0):"""批量处理文件夹中的图片,调整色调、明暗,并进行去图处理。参数:input_folder (str): 输入图片文件夹路径。output_folder (str): 输出图片文件夹路径。r_weight (float): 红色通道的权重。g_weight (float): 绿色通道的权重。b_weight (float): 蓝色通道的权重。brightness (float): 亮度调整值。"""os.makedirs(output_folder, exist_ok=True)for filename in os.listdir(input_folder):if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif')):input_path = os.path.join(input_folder, filename)output_path = os.path.join(output_folder, filename)adjust_image_tone(input_path, output_path, r_weight, g_weight, b_weight, brightness)
if __name__ == "__main__":input_folder = "input_images" output_folder = "output_images" r_weight = 1.0 g_weight = 0.5 b_weight = 0.5 brightness = 0.3 batch_process_images(input_folder, output_folder, r_weight, g_weight, b_weight, brightness)