Coverage for src/driada/utils/gif.py: 37.04%

27 statements  

« prev     ^ index     » next       coverage.py v7.9.2, created at 2025-07-25 15:40 +0300

1 

2import tqdm 

3import imageio 

4import matplotlib.pyplot as plt 

5import numpy as np 

6from os import listdir, remove 

7from os.path import isfile, join, splitext 

8 

9from .plot import * 

10 

11 

12def erase_all(path, signature='', ext='.png'): 

13 # destroy all previous images 

14 prev_files_paths = [join(path, f) for f in listdir(path) if signature in f] 

15 files_to_del = [fp for fp in prev_files_paths if isfile(fp) and splitext(fp) == ext] 

16 for fp in files_to_del: 

17 remove(fp) 

18 

19 

20def save_image_series(path, figures, im_ext='png'): 

21 #with io.capture_output() as captured: 

22 

23 for i in tqdm.tqdm(np.arange(1, len(figures)), leave = True, position = 0): 

24 fig = figures[i] 

25 figname = fig._suptitle.get_text() + im_ext 

26 fig.savefig(join(path, figname)) 

27 fig.close() 

28 

29 

30def create_gif_from_image_series(path, signature, gifname, erase_prev=True, im_ext='png', duration=0.2): 

31 if erase_prev: 

32 erase_all(path, signature=signature, ext=im_ext) 

33 

34 images = [] 

35 imfiles = [f for f in listdir(path) if isfile(join(path, f)) and signature in f and im_ext in f] 

36 imfiles = sorted(imfiles) 

37 

38 for filename in tqdm.tqdm(imfiles, leave=True, position=0): 

39 images.append(imageio.v3.imread((join(path, filename)))) 

40 

41 imageio.mimsave(join(path, 'GIFs', f'{signature} {gifname}.gif'), images, duration=duration)