Hi Everyone!! Have you ever been a victim of bad low light photography? Or does your phone not capture as good low light images as it claims? Have you ever thought of creating your own tool for low light photography? If your answers to all these questions is YES then scroll down and see what fix we have for you!
By the end of this article, you would be able achieve results like the below image.
Don’t stop! Keep scrolling and go through the blog in which I’ll guide you on how to create your own tool for low light image enhancement.
The trick I’ll be using to enhance low light images is to capture several low light photography shots of the object and then average it out.
How does it work? Well in low light your camera lens is not exposed to enough light and hence stores the noise in the pixels. If we take several (3-4) shots of the same image, and then average the pixels out, then the noise will be removed and you’ll get a clear picture.
It’s simple math 😉
Importing Necessary Libraries
We will work with the following libraries:
- cv2 – we need opencv for basic image loading, saving etc.
- numpy – to do basic maths stuff here like taking average.
- os – for navigating through directory
import cv2 import os import numpy as np
We’ll start by accessing the folder where 3-4 shots of the images are stored. I’ll store the name of all the image files present in that folder in list variable ‘files’.
path= os.getcwd()+ '/'+ 'images' files = list([os.path.join(path, f) for f in os.listdir(path)])
Now, I will store and load the first image in the variable ‘f‘. I’ll be initialising a variable ‘cnt’ to keep a track of how many images have been elapsed/processed. Note that we’ll be reading images in float type as it will help us in taking average.
f= files image= cv2.imread(f).astype(np.float) cnt=0
Next, we’ll loop through all the images present and store them in variable ‘image’ along with incrementing variable ‘cnt‘ upto 5 images.
for temp in files[1:]: temp_img= cv2.imread(temp) image+=temp_img cnt+=1 if cnt==5: break
Now, I’ll take the average of the images by dividing it by ‘cnt’. This is the special feature of numpy library that we can process the whole array/list with simplicity.
image/=cnt image = cv2.normalize(image, None, 0, 255, cv2.NORM_MINMAX)
At last, I’ll save the image and preview the before and after images.
cv2.imwrite('output.png', image) cv2.imshow('after',cv2.imread('output.png')) cv2.imshow('before',cv2.imread(files)) cv2.waitKey(0) cv2.destroyAllWindows()
Wohoo Guys!! Finally you are having your own tool for low light photography. Now you can easily enhance the low light images and enjoy night photography. Cheers !!
If you liked the blog feel free to share it and please share your views in the comment section.