import os, skimage, nibabel as nib

nii_dir = r"nii_dir"
listdir = [f for f in os.listdir(nii_dir) if f.endswith('.nii.gz')]
listdir.sort()
for i in range(len(listdir)):
    np_label = nib.load(os.path.join(nii_dir, listdir[i])).get_fdata()
    euler = skimage.measure.euler_number(np_label)
    if euler <= 0:
        print(listdir[i], euler)
