diff --git a/tomplot/__init__.py b/tomplot/__init__.py index 46e92aa..64393cc 100644 --- a/tomplot/__init__.py +++ b/tomplot/__init__.py @@ -8,3 +8,4 @@ from tomplot.quiver_plot import * # noqa from tomplot.regridding import * # noqa from tomplot.tomplot_tools import * # noqa +from tomplot.tomplot_utilities import * # noqa diff --git a/tomplot/tomplot_utilities.py b/tomplot/tomplot_utilities.py new file mode 100644 index 0000000..fde3428 --- /dev/null +++ b/tomplot/tomplot_utilities.py @@ -0,0 +1,61 @@ +"""assorted routines for post plotting processing""" +import subprocess +import os +import re + + +def create_animation(results_dir, plot_dir, file_name='animation.gif', + delay=20, loop=0): + u""" + A routine which makes use of the imageMagick function 'convert' to create a + gif from a directory of image files. + Requirements: Requires an installation of ImageMagick on your bash path + Args: + results_dir (class: string): file path to the file containg the + images to make the gif from. + plot_dir (class: string): file path to where the gif is outputted. + file_name (class: string): name of animation, typicaly end in git + delay (class: int): delay for animation, measured in milliseconds + loop (class: int): how many times the animation will loop. 0 = infinite + u""" + # check if the input directory exists + if not os.path.exists(results_dir): + raise ValueError(f'Error: Input directory, {results_dir} does not exist.') + + if not file_name.endswith('.gif') == True: + raise ValueError(f'Error: file name, {file_name} lacks.gif.') + + image_files = [f for f in os.listdir(plot_dir) if + f.lower().endswith(('.png', '.jpg', '.jpeg'))] + + # function to ensure files get sorted in ascending order + def extract_number(filename): + return int(re.search(r'\d+', filename).group()) + + sorted_files = sorted(image_files, key=extract_number) + # configures the convert bash command + convert_cmd = ['convert', + '-delay', str(delay), + '-loop', str(loop)] + # add the input files to the command + convert_cmd.extend([os.path.join(plot_dir, f) for f in sorted_files]) + + # add output file + convert_cmd.append(f'{results_dir}/{file_name}') + + # use subprocesses to call the command + subprocess.run(convert_cmd) + +def check_directory(path): + u""" + A routine which checks if the plotting directory exists and creates if + not. + path (class: int): path to directory + u""" + + # check if directory exists + if not os.path.exists(path): + os.makedirs(path) + print('results directory created') + else: + ('directory already exsists') \ No newline at end of file