import SimpleITK as sitkimport globimport numpy as npfrom PIL import Imageimport cv2def save_array_as_nii_volume(data, filename, reference_name=None): img = sitk.GetImageFromArray(data) if (reference_name is not None): img_ref = sitk.ReadImage(reference_name) img.CopyInformation(img_ref) sitk.WriteImage(img, filename)image_path = './1/DICOM_anon/Ground/Liver'image_arr = glob.glob(str(image_path) + str("/*"))image_arr.sort()print(image_arr)# print(image_arr[0].shape)print(len(image_arr))allImg = []allImg = np.zeros([len(image_arr), 256, 256], dtype='uint8')for i in range(len(image_arr)): single_image_name = image_arr[i] img_as_img = Image.open(single_image_name) # img_as_img.show() img_as_np = np.asarray(img_as_img) img_as_np.squeeze(2) allImg[i, :, :] = img_as_np# np.transpose(allImg,[2,0,1])save_array_as_nii_volume(allImg, './S1/1.nii')print(np.shape(allImg))img = allImg[:, :, 55]