There are multiple grayscale images in a folder that I am trying to equalize the brightness. The enhancement has to be applied in all the images except the first which will be our reference image. Also, the change only happens in the brightness of the images and not contrast since I want to preserve all the other details. This means that there is only a simple shift of histogram and no widening. I was trying to use the PIL module to calculate the average brightness. This method is probably faster since I can do it without a numpy array conversion.
The code:
with open("Img_Locations.txt") as file: # Assume the file locations in a text file
img_files = file.read().splitlines()
file.close()
self.images = [[] for x in range(len(img_files))]
for i in range(len(img_files)):
images[i] = Image.open(img_files[i])
im = images[i].convert('L')
stat = ImageStat.Stat(im)
ref_pil_original = int(stat.mean[0])
enhancer = ImageEnhance.Brightness(im)
enhanced_im = enhancer.enhance(1.5)
stat2 = ImageStat.Stat(enhanced_im)
ref_pil_bright = int(stat2.mean[0])
print(ref_pil_original, ref_pil_bright)
The sample output here was:
114 170
129 192
122 183
So the question is why does brightness enhancement when applied to the same factor of 1.5 yield me different differences for each image (basically how does the factor affect the pixels of my image)? If this is the case, what is the fastest way that I can adjust the brightness of my images by a constant factor to make sure that the final averages have the same mean value?
python -- measuring pixel brightness Here the PIL image is read by pixel. So does this mean that I will have to scan the image pixel by pixel to complete my requirement? I would like to know if this is indeed the way to do this since there are already existing functions. Unfortunately, I am aware of only PIL and OpenCV for image analysis and therefore the format of my read images are confined to them both.