The aim of this program is to find and evaluate the brightest region of an image and adjust the image brightness if required.
First import some libraries for image processing. OpenCV and PIL are most commonly used.
import cv2 from PIL import Image, ImageEnhance
Next we need to get the brightness of the image.
Make a copy of the image and transform the copy to grey-scale. Apply a Gaussian blur function to the copy which will normalize any outlining pixels or noise (e.g. random white pixels). Then we get the brightest region with a radius of 11.
This function will return a value between 0-255. 0 being black and 255 being white.
# Return brightness of image file (0-255 , black to white) def get_brightness(image): orig = image.copy() grayimg = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # apply a Gaussian blur to the image then find the brightest region with radius radius = 11 grayimg = cv2.GaussianBlur(grayimg, (radius, radius), 0) (minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(grayimg) # display the first brightest region, 0 key to continue cv2.circle(grayimg, maxLoc, radius, (0, 255, 255 ), 2) cv2.imshow("Brightest region", grayimg) cv2.waitKey(0) & 0xFF cv2.destroyAllWindows() image = orig.copy() print(maxVal) return maxVal
Once me have found the brightest region of the image, we can decide if this file need to be brightened. If the brightest region is around 240 (i.e. almost white) then we don’t need to brighten the image. However if the brightest region is, say less than 220 (i.e. grey, photo taken at night or in the shade) then we can brighten the image using the following function.
Note we’re fetching the original image to brighten but the result is save as a copy (temp_img.jpg)
# adjust brightness of file def adjust_brightness(file, brightness): img = Image.open(file) enhancer = ImageEnhance.Brightness(img) #enhance brightness print('Adjusting brightness by',brightness) enhanced_img = enhancer.enhance(brightness) enhanced_img.save("temp_img.jpg") #return enhanced_im
We need to write a main program to call the above functions. Here we open the image file, create a copy, evaluate the brightness of the brightest region in the copied image by passing the copy to the get_brightness(image) function. Then we use the adjust_brightness(file, brightness) function to open the original image, adjust the brightness and overwrite the copy and then call the
get_brightness(image) function again to see the result.
Note: Here we only open the original image once, all operations will be performed on the temporary copy
Important!!! If the image brightness is over 255 you’ll end up with a corrupt image file.
if __name__ == '__main__': #Open image file, save a copy file = "example_image.jpg" img = Image.open(file) img.save("temp_img.jpg") #get brightness of the file, note we're using the copy Brightness = get_brightness(cv2.imread("temp_img.jpg")) #adjust brightness based on the value from get_brightness if Brightness < 100: print("<130") adjust_brightness("temp_img.jpg", 2.5) elif Brightness < 200: print("<200") adjust_brightness("temp_img.jpg", 2) elif Brightness < 220: print("<220") adjust_brightness("temp_img.jpg", 1.5) get_brightness(cv2.imread("temp_img.jpg"))