Skip to content

Instantly share code, notes, and snippets.

@haberda
Created September 18, 2022 22:43
Show Gist options
  • Save haberda/1487cc266bf11fd6b8a1b028699b3a06 to your computer and use it in GitHub Desktop.
Save haberda/1487cc266bf11fd6b8a1b028699b3a06 to your computer and use it in GitHub Desktop.
Appdaemon app to save a series of images to a gif
import appdaemon.plugins.hass.hassapi as hass
import os
import imageio
import shutil
import datetime
import glob
import time
from stat import S_ISREG, ST_CTIME, ST_MODE
from PIL import Image
class savegif(hass.Hass):
def initialize(self):
self.listen_event(self.process_images,'create_gif')
# self.register_service('process_images/create_gif', self.process_images)
def process_images(self, event, data, kwargs):
if 'folder' in data:
self.folder = data['folder']
else:
self.log('No folder defined, exiting')
return
self.log('Attempting to create gif from ' + self.folder)
# if 'cache' in data:
# self.cache_dir = data['cache']
# else:
# self.cache_dir = data['folder']
if 'output_dir' in data:
self.output_dir = data['output_dir']
else:
self.output_dir = data['folder']
copy_original_files = True
if 'copy_original_files' in data:
try:
copy_original_files = bool(data['copy_original_files'])
except:
self.log('copy_original_files not properly defined, assuming default')
if 'output_name' in data:
self.output_name = data['output_name']
else:
self.output_name = 'output.gif'
clean_cache = True
if 'clean_files' in data:
clean_cache = bool(data['clean_files'])
fps = 0.5
if 'fps' in data:
try:
fps = float(data['fps'])
except:
self.log('fps given in incorrect format, using default 0.5')
delay = 0
if 'delay' in data:
try:
delay = int(data['delay'])
except:
self.log('Delay not given in integer format, using default 0')
time.sleep(delay)
file_type = '.jpg'
if 'file_type' in data:
file_type = data['file_type']
if not os.path.exists(self.output_dir):
os.makedirs(self.output_dir)
minimum_num_images = 2
if 'minimum_num_images' in data:
minimum_num_images = data['minimum_num_images']
self.images = []
# # Deletes old files from copied folder
# for file in os.scandir(self.cache_dir):
# if file.name.endswith(".PNG"):
# os.unlink(file)
# Sorts by file name
for self.file_name in sorted(os.listdir(self.folder)):
# Copies jpg to new folder and converts to png
if not self.file_name.startswith(".") and self.file_name.endswith(file_type):
self.file_path = os.path.join(self.folder, self.file_name)
#self.cache_filename = os.path.splitext(self.file_name)[0]
#self.cache_filepath = os.path.join(self.cache_dir, self.cache_filename + '_cache.PNG')
#print (self.file_path)
#print (self.cache_filepath)
#self.im = Image.open(self.file_path)
#self.im.save(self.cache_filepath, "PNG")
#self.images.append(imageio.imread(self.cache_filepath,'_cache.PNG'))
self.images.append(imageio.imread(self.file_path))
if copy_original_files:
self.im = Image.open(self.file_path)
self.im.save(self.output_dir + self.file_name)
if len(os.listdir(self.folder)) >= minimum_num_images:
# Creates gif file
try:
imageio.mimsave(self.output_dir + self.output_name, self.images, fps=fps)
self.log('gif created at ' + self.output_dir + self.output_name)
success = True
except:
success = False
else:
self.log('There are fewer images in the folder than the minimum number of images, no gif created.')
success = False
# Deletes png files again
if clean_cache:
for file in os.scandir(self.folder):
if file.name.endswith(file_type):
os.unlink(file)
if success:
self.fire_event("gif_created", path=self.output_dir + self.output_name, folder=self.output_dir, file=self.output_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment