def unzip_nifti(input_filename, output_filename):
"""Unzips the given nifti file.
This will create the output directories if they do not yet exist.
Args:
input_filename (str): the nifti file we would like to unzip. Should have the extension ``.gz``.
output_filename (str): the location for the output file. Should have the extension ``.nii``.
Raises:
ValueError: if the extensions of either the input or output filename are not correct.
"""
if not os.path.exists(os.path.dirname(output_filename)):
os.makedirs(os.path.dirname(output_filename))
if not input_filename.rstrip().endswith('.gz') or not output_filename.rstrip().endswith('.nii'):
raise ValueError('The input filename should have extension ".gz" and the '
'output filename should have extension ".nii".')
with gzip.open(input_filename, 'rb') as f_in, open(output_filename, 'wb') as f_out:
shutil.copyfileobj(f_in, f_out)
评论列表
文章目录