nifti library failed to write image

 在将dcm文件转nii格式的时候报错

Traceback (most recent call last):
  File "E:\pycharmproject\Breast3\prepare\1dcm转为nii文件.py", line 47, in <module>
    dcm2nii(dcm_xulie_path, os.path.join(save_path, f'{name}.nii'))
  File "E:\pycharmproject\Breast3\prepare\1dcm转为nii文件.py", line 33, in dcm2nii
    sitk.WriteImage(image3, os.path.join(nii_path, ))
  File "D:\anaconda3\envs\pytorch\lib\site-packages\SimpleITK\extra.py", line 425, in WriteImage
    return writer.Execute(image)
  File "D:\anaconda3\envs\pytorch\lib\site-packages\SimpleITK\SimpleITK.py", line 7907, in Execute
    return _SimpleITK.ImageFileWriter_Execute(self, *args)
RuntimeError: Exception thrown in SimpleITK ImageFileWriter_Execute: D:\a\1\sitk-build\ITK\Modules\IO\NIFTI\src\itkNiftiImageIO.cxx:2254:
ITK ERROR: NiftiImageIO(000001F74304E4C0): ERROR: nifti library failed to write imageC:\Users\Administrator\Desktop\Breast\NIIdata\低表达\T1\AN_HAI_YING.nii

代码如下

import os
import shutil
import numpy as np
import pandas as pd
import pydicom
import nibabel as nib
import SimpleITK as sitk

'''
dcms_root_path文件夹下是每一个患者的姓名,每个患者的姓名文件夹下是每张dcm文件
dcm2nii.py 读取dcm文件,并生成nii文件
nii_path是一个新的文件夹,存放新生成的nii文件
这个脚本只能处理一个序列,每个序列都要执行一遍
'''


def dcm2nii(dcms_path, nii_path):
    # 1.构建dicom序列文件阅读器,并执行(即将dicom序列文件“打包整合”)
    reader = sitk.ImageSeriesReader()
    dicom_names = reader.GetGDCMSeriesFileNames(dcms_path)
    reader.SetFileNames(dicom_names)
    image2 = reader.Execute()
    # 2.将整合后的数据转为array,并获取dicom文件基本信息
    image_array = sitk.GetArrayFromImage(image2)  # z, y, x
    origin = image2.GetOrigin()  # x, y, z
    spacing = image2.GetSpacing()  # x, y, z
    direction = image2.GetDirection()  # x, y, z
    # 3.将array转为img,并保存为.nii.gz
    image3 = sitk.GetImageFromArray(image_array)
    image3.SetSpacing(spacing)
    image3.SetDirection(direction)
    image3.SetOrigin(origin)
    sitk.WriteImage(image3, os.path.join(nii_path, ))

dcms_root_path = r'C:\Users\Administrator\Desktop\Breast\ki67原始数据\低表达\T1'  # dicom序列文件所在路径
nii_path = r'C:\Users\Administrator\Desktop\Breast\NIIdata\低表达\T1'  # 所需.nii.gz文件保存路径
if os.path.exists(nii_path):
    shutil.rmtree(nii_path)  # 如果 save_path 存在,则删除
os.makedirs(nii_path)  # 生成 save_path

patients = os.listdir(dcms_root_path)
for i , p in enumerate(patients):
    dcm_xulie_path = os.path.join(dcms_root_path, p)
    name = p.split("-")[0]
    save_path = os.path.join(nii_path)

    dcm2nii(dcm_xulie_path, os.path.join(save_path, f'{name}.nii'))
    print(f'正在处理第{i+1}个文件')

print("finished")

 这里在保存的路径时:nii_path = r'C:\Users\Administrator\Desktop\Breast\NIIdata\低表达\T1' # 所需.nii.gz文件保存路径,路径里有中文,所以导致了错误,修改为全英文就会好了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值