Page MenuHomec4science

assemble_pictures.py
No OneTemporary

File Metadata

Created
Thu, May 9, 00:48

assemble_pictures.py

#!/usr/bin/env python3
import subprocess
import shutil
import sys
import os
import shlex
import time
#import PIL.Image, PIL.ImageFile, PIL.ExifTags
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']
#PIL.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)
#img = PIL.Image.open(old_file_path)
#img_exif = img.getexif()
# Try using the EXIF information
#if img_exif is not None:
# for key, val in img_exif.items():
# if key in PIL.ExifTags.TAGS:
# print(f'{PIL.ExifTags.TAGS[key]}:{val}')
# 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_":
if 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))

Event Timeline