diff --git a/spectur_live_view.py b/spectur_live_view.py index 9f44eee..243fe05 100644 --- a/spectur_live_view.py +++ b/spectur_live_view.py @@ -7,6 +7,7 @@ import os import glob import time import pytz +import shutil import subprocess from io import BytesIO from datetime import datetime @@ -27,6 +28,7 @@ TIMEZONE = 'Australia/Sydney' WAIT_TIME = 0.1 RECORDING_DURATION = 20 SILENT = True +IMAGE_MASK = 'mask/overtopping-mask.png' username = '' password = '' @@ -229,6 +231,28 @@ def create_video(): subprocess.run(command) +def get_brightness(jpg_name, mask): + """Get mean brighness of an image with a mask applied + """ + # Load image + im = Image.open(jpg_name) + + # Load mask and resize of necessary + width, height = mask.size + im = im.resize((width, height), Image.LANCZOS) + + # Apply mask to image + im.paste(mask, mask=mask) + + # Convert to greyscale + im_grey = im.convert('LA') + + # Calculate mean intensity + b = np.array(im_grey).mean() + + return b + + # Create counter t = [] @@ -237,3 +261,17 @@ get_images() # Create video create_video() + +# Load image mask +mask = Image.open(os.path.join(pwd, IMAGE_MASK)) + +# Find brightest image for current high tide +jpg_names = glob.glob(current_hightide_dir + '/jpg/*') +b = np.zeros(len(jpg_names)) +for i, jpg_name in enumerate(jpg_names): + b[i] = get_brightness(jpg_name, mask) + +# Copy brightest image to root directory for current year +src_name = jpg_names[b.argmax()] +dst_name = current_hightide_dir + '.jpg' +shutil.copy(src_name, dst_name)