示例#1
0
def write_dicom(pixel_array, filename, origin, origins, orientation):   
    file_meta = Dataset()
    file_meta.MediaStorageSOPClassUID = 'Secondary Capture Image Storage'
    file_meta.MediaStorageSOPInstanceUID = '1.3.6.1.4.1.9590.100.1.1.111165684411017669021768385720736873780'
    file_meta.ImplementationClassUID = '1.3.6.1.4.1.9590.100.1.0.100.4.0'
    ds = FileDataset(filename, {},file_meta = file_meta,preamble="\0"*128)
    ds.ImageType= 'ORIGINAL\PRIMARY\AXIAL\CT'
    ds.SOPClassUID = 'Secondary Capture Image Storage'
    ds.SOPInstanceUID =    '1.3.6.1.4.1.9590.100.1.1.111165684411017669021768385720736873780'
    ds.StudyDate = str(datetime.date.today()).replace('-','')
    #ds.ImageDate = str(datetime.date.today()).replace('-','')
    ds.StudyTime = str(time.time())
    #ds.ImageTime = str(time.time())
    ds.AccessionNumber = '153745645'
    ds.Modality = 'CT'
    ds.Manufacturer = 'PY_BONEMAT_ABAQUS'
    ds.InstitutionName = 'UNIVERISTY_OF_BATH'
    ds.ReferringPhysiciansName = 'NOTAPPLICABLE'
    ds.ManufacturersModelName = 'VERSION1-0-9'
    ds.ReferencedImageSequence = ''
    ds.PatientsName = 'SUBJECT001'
    ds.PatientID = ''
    ds.PatientsBirthDate = ''
    ds.PatientsSex = 'F'
    ds.SliceThickness = 2.7
    ds.KVP = 120
    ds.SpacingBetweenSlices = 1
    ds.DataCollectionDiameter = 430
    ds.ReconstructionDiameter = 430.00
    ds.DistanceSourceToDetector = 1093
    ds.DistanceSourceToPatient = 630.
    ds.GantryDetectorTilt = 0.0
    ds.ScanArc = 403
    ds.XRayTubeCurrent = 200
    ds.Exposure = 135
    ds.FilterType = 'F'
    ds.PatientPosition = 'HFS'
    ds.StudyInstanceUID = '1.3.6.1.4.1.9590.100.1.1.124313977412360175234271287472804872093'
    ds.SeriesInstanceUID = '1.3.6.1.4.1.9590.100.1.1.111165684411017669021768385720736873780.1'
    #ds.ReferenceSOPClassUID = 'Secondary Capture Image Storage'
    #ds.ReferencedSOPInstanceUID = '1.3.6.1.4.1.9590.100.1.1.111165684411017669021768385720736873780'
    #ds.ContentDate = str(datetime.date.today()).replace('-','')
    #ds.ContentTime = str(time.time())
    ds.StudyID = '15346254'
    ds.SeriesNumber = '8733'
    ds.AcquisitionNumber = 1
    ds.ImageNumber = origins.index(origin) + 1
    ds.SliceLocation = origin[2]
    ds.SecondaryCaptureDeviceManufctur = 'Python 2.7.3'   
    ds.SamplesPerPixel = 1
    ds.PhotometricInterpretation = "MONOCHROME2"
    ds.PixelRepresentation = 0
    ds.HighBit = 15
    ds.BitsStored = 16
    ds.BitsAllocated = 16
    ds.SmallestImagePixelValue = '\\x00\\x00'
    ds.LargestImagePixelValue = '\\xff\\xff'
    ds.Columns = pixel_array.shape[0]
    ds.Rows = pixel_array.shape[1]
    ds.PatientOrientation = ['L','P']
    ds.ImagePositionPatient = origin
    ds.ImageOrientationPatient = orientation
    ds.PixelSpacing = [1.0, 1.0]
    ds.RescaleSlope = 1.0
    ds.RescaleIntercept = 0.0
    if pixel_array.dtype != np.uint16:
        pixel_array = pixel_array.astype(np.uint16)
    ds.PixelData = pixel_array.tostring()

    ds.save_as(filename)
    return