import cv2
import os
def adjust_contrast(image, alpha):
return cv2.convertScaleAbs(image, alpha=alpha, beta=0)
def process_images(input_folder, output_folder, alpha):
if not os.path.exists(output_folder):
os.makedirs(output_folder)
for filename in os.listdir(input_folder):
if filename.endswith(".jpg") or filename.endswith(".png") or filename.endswith(".jpeg"):
img_path = os.path.join(input_folder, filename)
image = cv2.imread(img_path)
if image is not None:
adjusted_image = adjust_contrast(image, alpha)
output_path = os.path.join(output_folder, filename)
cv2.imwrite(output_path, adjusted_image)
print(f"Processed {filename}")
else:
print(f"Failed to read {filename}")
input_folder = r'C:\Users\Administrator\Desktop\6.26'
output_folder = r'C:\Users\Administrator\Desktop\Contrast'
alpha = 1.4
process_images(input_folder, output_folder, alpha)
【Python】批量改变图像对比度
最新推荐文章于 2024-10-15 08:56:31 发布