Created
July 24, 2019 11:53
-
-
Save tomoconnor/52c90606b3c54c436e3241f38b5a7703 to your computer and use it in GitHub Desktop.
Quick and dirty thing I hacked together for finding lightning in night-time video. It just measures the average brightness of each image of a video (you first rip the video to a sequence of images with ffmpeg), then symlinks the brightest frames into a subdirectory called "brightest".
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image, ImageStat | |
import os | |
import sys | |
import shutil | |
import glob | |
import argparse | |
import operator | |
def brightness( im_file ): | |
im = Image.open(im_file).convert('L') | |
stat = ImageStat.Stat(im) | |
return stat.mean[0] | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description='Look for lightning in images') | |
parser.add_argument('directory') | |
args = parser.parse_args() | |
d = args.directory | |
print(f"Operating in {d}") | |
print(f"Finding all files in {d}") | |
stats = {} | |
os.chdir(args.directory) | |
for f in glob.glob("*.png"): | |
img_brightness = brightness(f) | |
stats[f] = img_brightness | |
# print("%s\t%s"%(f,img_brightness)) | |
min_b_key = min(stats.items(), key=operator.itemgetter(1))[0] | |
max_b_key = max(stats.items(), key=operator.itemgetter(1))[0] | |
min_b_val = stats[min_b_key] | |
max_b_val = stats[max_b_key] | |
print(f"MIN:\t{min_b_key}\t{min_b_val}") | |
print(f"MAX:\t{max_b_key}\t{max_b_val}") | |
threshold = (min_b_val+max_b_val)/2.0 | |
print(f"AVG:\t{threshold}") | |
brighter_than_average = {k: v for k,v in stats.items() if v > threshold } | |
# print(brighter_than_average) | |
print("Brightest Images:") | |
s = sorted(brighter_than_average, key=brighter_than_average.get,reverse=True) | |
if not os.path.exists("brightest"): | |
os.mkdir("brightest") | |
for fn in s: | |
os.symlink( | |
os.path.join(os.getcwd(),fn), | |
os.path.join(os.getcwd(),"brightest",fn) | |
) | |
print ("Done.") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment