spatialtopology / spacetop-prep

code for transferring data and preprocessing
MIT License
0 stars 3 forks source link

[BUG] Gifify opens images but does not close them #47

Closed Michael-Sun closed 1 month ago

Michael-Sun commented 8 months ago

Which module is this from?

qcplot/corr02_gif.py

(Write your answer here: beh, datalad, fmriprep, mriqc, physio, qcplot, redcap)

What is the issue?

function gifify uses Image.open without closing, leading to a crash if too many images are open (e.g., when a subject has many sessions)

What was your expected behavior?

No crash.

How can we reproduce this?

Run gifify with many images.

Any additional context?

The solution is to use with. The amended function code is as follows:

def gifify(sub, img_dir, save_dir, file_pattern, save_fname): image_files = sorted(glob.glob(os.path.join(img_dir, f"{file_pattern}"))) images = [] for filename in image_files: with Image.open(filename) as img: images.append(img.copy()) pathlib.Path(save_dir).mkdir(parents=True, exist_ok=True) images[0].save(os.path.join(save_dir, f'{save_fname}'), save_all=True, append_images=images[1:], duration=200, loop=0)