diff --git a/scripts/assemble_pictures.py b/scripts/assemble_pictures.py index b3c1354..80e26e1 100755 --- a/scripts/assemble_pictures.py +++ b/scripts/assemble_pictures.py @@ -1,148 +1,153 @@ #!/usr/bin/env python3 import subprocess import shutil import sys import os import shlex import time from PIL import Image as pim from PIL import ImageFile from tkinter import filedialog from tkinter import * if __name__=='__main__': helpmess="""Usage: assemble_pictures DIR Creates a movie with all the pictures found in DIR. """ # Inputs if len(sys.argv)<2: root = Tk() root.withdraw() indir = filedialog.askdirectory(initialdir='~', title = 'Select the directory containing the time-lapse pictures') else: indir=os.path.realpath(sys.argv[1]) # Set list of valid file extensions valid_extensions = [".jpg", ".jpeg", ".png", '.JPG'] ImageFile.LOAD_TRUNCATED_IMAGES = True if not os.path.isdir(indir): print(helpmess) sys.exit(0) else: dirdate = None # Make sure we're not going to process the processed files (droot, ddir) = os.path.split(indir) if ddir == 'YMD': newdir = indir indir = droot else: newdir = os.path.join(indir, 'YMD') if not os.path.exists(newdir): os.mkdir(newdir) # Get all files from folder file_names = os.listdir(indir) # Informe the user print('Renaming files found in {}'.format(indir)) # Make sure we find something file_ext=None # For each file for file_name in file_names: if os.path.isfile(os.path.join(indir, file_name)): # Get the file extension file_ext = os.path.splitext(file_name)[1] # If the file does not have a valid file extension # then skip it if (file_ext not in valid_extensions): continue # Create the old file path old_file_path = os.path.join(indir, file_name) # Open the image image = pim.open(old_file_path) # Try using the EXIF information if image._getexif() is not None: # Get the date taken from EXIF metadata date_taken = image._getexif()[36867] # Close the image image.close() # Reformat the date taken to "YYYYMMDD-HHmmss" date_time = date_taken \ .replace(":", "") \ .replace(" ", "-") # If we have the fixed format convention from our AscPi elif file_name[0:10] == "timelapse_": timing = file_name.split('_')[1].split('.')[0].split('-') # Get the main date from the folder name if dirdate is None: dirdate = os.path.split(indir)[1].split('_')[1].split('-')[0:2] # Build the full date date_time = '{}{}{}-{}{}{}'.format(dirdate[0], dirdate[1], timing[0], timing[1], timing[2], timing[3]) # Otherwise use the modification date else: date_time = time.strftime('%Y%m%d-%H%M%S', time.gmtime(os.path.getmtime(old_file_path))) # Combine the new file name and file extension new_file_name = date_time + file_ext # Create the new folder path new_file_path = os.path.join(newdir, new_file_name) # Copy the file print('{} -> {}'.format(old_file_path, new_file_path)) shutil.copy(old_file_path, new_file_path) if file_ext is None: print('\n!!!!!!!!!!!!!!!!!!!##########ERROR: No pictures found in {element}############!!!!!!!!!!!!!!!!!!!!!!\n\n'.format(element=indir)) else: cmd = "mogrify -monitor -auto-orient '{newdir}/*{ext}'".format(newdir=newdir, ext=file_ext) print(cmd) try: output = subprocess.check_output(cmd, shell=True) success1 = True except subprocess.CalledProcessError as e: output = e.output.decode() success1 = False cmd = "ffmpeg -y -framerate 2 -pattern_type glob -i '{newdir}/*{ext}' -vf 'format=nv12' -c:v h264 {moviefile}.mkv".format(newdir=newdir, ext=file_ext, moviefile=indir) print(cmd) try: output = subprocess.check_output(cmd, shell=True) success2 = True + + cmd = "ffmpeg -y -framerate 10 -pattern_type glob -i '{newdir}/*{ext}' -vf 'format=nv12' -c:v h264 {moviefile}_5x.mkv".format(newdir=newdir, ext=file_ext, moviefile=indir) + output = subprocess.check_output(cmd, shell=True) + cmd = "ffmpeg -y -framerate 20 -pattern_type glob -i '{newdir}/*{ext}' -vf 'format=nv12' -c:v h264 {moviefile}_10x.mkv".format(newdir=newdir, ext=file_ext, moviefile=indir) + output = subprocess.check_output(cmd, shell=True) except subprocess.CalledProcessError as e: output = e.output.decode() success2 = False if success1 and success2: print('\n+++++++++++++++++++++++++++++++++++++++++++++++++++' '+++++++++++++++++++++++++++++++++++++++++++++++++++' '+++++++++++++++++++++++++++++++++++++++++++++++++++\n' '+++++++++++++++++++++++++++++++++++++++++++++++++++' '++++++Folder {element} processed successfully++++++' '+++++++++++++++++++++++++++++++++++++++++++++++++++\n' '+++++++++++++++++++++++++++++++++++++++++++++++++++' '+++++++++++++++++++++++++++++++++++++++++++++++++++' '+++++++++++++++++++++++++++++++++++++++++++++++++++\n\n'.format(element=indir)) else: print('\n!!!!!!!!!!!!!!!!!!!##########ERROR for folder {element}############!!!!!!!!!!!!!!!!!!!!!!\n\n'.format(element=indir))