diff --git a/scripts/TimeLapseMovie.desktop b/scripts/TimeLapseMovie.desktop new file mode 100755 index 0000000..e5cde55 --- /dev/null +++ b/scripts/TimeLapseMovie.desktop @@ -0,0 +1,9 @@ +[Desktop Entry] +Version=1.0 +Name=TimeLapseMovie +Comment=Create a movie out of a time-lapse recording +Exec=./assemble_pictures.py +Icon=/usr/share/pixmaps/python3.xpm +Terminal=true +Type=Application +Categories=Application; diff --git a/scripts/assemble_pictures.py b/scripts/assemble_pictures.py index c8bb49e..d2b45ca 100755 --- a/scripts/assemble_pictures.py +++ b/scripts/assemble_pictures.py @@ -1,132 +1,135 @@ #!/usr/bin/env python3 import subprocess import shutil import sys import os import shlex import time -from PIL import Image +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(title = 'Select the directory containing the time-lapse pictures') + 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 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)) # 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 = Image.open(old_file_path) + 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 shutil.copy(old_file_path, new_file_path) cmd = "mogrify -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 {newdir}/{element}.mkv".format(newdir=newdir, ext=file_ext, element=element) + 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 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=element)) + '+++++++++++++++++++++++++++++++++++++++++++++++++++\n\n'.format(element=indir)) else: - print('\n!!!!!!!!!!!!!!!!!!!##########ERROR for folder {element}############!!!!!!!!!!!!!!!!!!!!!!\n\n'.format(element=element)) + print('\n!!!!!!!!!!!!!!!!!!!##########ERROR for folder {element}############!!!!!!!!!!!!!!!!!!!!!!\n\n'.format(element=indir))