import cv2 import numpy as np import os path = r'C:\Users\Administrator\Desktop\02\\' paths = r'C:\Users\Administrator\Desktop\1\\' files = os.listdir(path) for file in files: print(file) img = cv2.imread(path+file) cols, rows = img.shape[:2] brightness = np.sum(img) / (255 * cols * rows) print(brightness) minimum_brightness = 0.66 ratio = brightness / minimum_brightness if ratio >= 1: Cimg = img / 255 gamma = 0.6 O = np.power(Cimg, gamma) O = O * 255 cv2.imwrite(paths+file,O) #print("Image already bright enough") else: # Otherwise, adjust brightness to get the target brightness img = cv2.convertScaleAbs(img, alpha = 1 / ratio, beta = 0) Cimg = img / 255 gamma = 0.6 O = np.power(Cimg, gamma) O = O * 255 cv2.imwrite(paths+file,O)
时间: 2024-04-28 19:27:16 浏览: 99
这段代码是用 Python 语言编写的,主要是对图片亮度进行调整的操作。首先,通过 cv2.imread() 函数读取指定路径下的图片,然后使用 numpy 库计算图片的亮度值。如果亮度值大于等于 0.66,则认为图片已经足够亮,直接保存到指定路径下;否则,根据亮度值与目标亮度值的比例调整图片的亮度,并对调整后的图片进行 gamma 校正,最后将图片保存到指定路径下。
阅读全文