Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tomplot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
61 changes: 61 additions & 0 deletions tomplot/tomplot_utilities.py
Original file line number Diff line number Diff line change
@@ -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')