79677746

Date: 2025-06-24 14:24:35
Score: 1.5
Natty:
Report link

Based on the information provided here I was unable to reproduce the issue using this data and the code below. Please provide a MWE which reproduces the issue. For future reference, SimpleITK/ITK have a dedicated discourse forum.

import dicom2nifti
import SimpleITK as sitk
import os
import time

dicom_folder_path = "./single_series_CIRS057A_MR_CT_DICOM"
nifti_output_path = "./result.nii.gz"
dicom_output_dir = "./result"

dicom2nifti.dicom_series_to_nifti(dicom_folder_path, nifti_output_path, reorient_nifti=False)

image = sitk.ReadImage(nifti_output_path, outputPixelType=sitk.sitkFloat32)

# List of tag-value pairs shared by all slices
modification_time = time.strftime("%H%M%S")
modification_date = time.strftime("%Y%m%d")
direction = image.GetDirection()
series_tag_values = [
    ("0008|0031", modification_time),  # Series Time
    ("0008|0021", modification_date),  # Series Date
    ("0008|0008", "DERIVED\\SECONDARY"),  # Image Type
    (
        "0020|000e",
        "1.2.826.0.1.3680043.2.1125." + modification_date + ".1" + modification_time,
    ),  # Series Instance UID
    (
        "0020|0037",
        "\\".join(
            map(
                str,
                (
                    direction[0],
                    direction[3],
                    direction[6],
                    direction[1],
                    direction[4],
                    direction[7],
                ),
            )
        ),
    ),  # Image Orientation
    # (Patient)
    ("0008|103e", "Created-SimpleITK"),  # Series Description
]
# Write floating point values, so we need to use the rescale
# slope, "0028|1053", to select the number of digits we want to keep. We
# also need to specify additional pixel storage and representation
# information.
rescale_slope = 0.001  # keep three digits after the decimal point
series_tag_values = series_tag_values + [
        ("0028|1053", str(rescale_slope)),  # rescale slope
        ("0028|1052", "0"),  # rescale intercept
        ("0028|0100", "16"),  # bits allocated
        ("0028|0101", "16"),  # bits stored
        ("0028|0102", "15"),  # high bit
        ("0028|0103", "1"),
    ]  # pixel representation

writer = sitk.ImageFileWriter()
writer.KeepOriginalImageUIDOn()
for i in range(image.GetDepth()):
    slice = image[:, :, i]
    for tag, value in series_tag_values:
        slice.SetMetaData(tag, value)
    # slice origin and instance number are unique per slice
    slice.SetMetaData(
        "0020|0032",
        "\\".join(map(str, image.TransformIndexToPhysicalPoint((0, 0, i)))),
    )
    slice.SetMetaData("0020|0013", str(i))
    writer.SetFileName(os.path.join(dicom_output_dir, f"{i+1:08X}.dcm"))
    writer.Execute(slice)
Reasons:
  • RegEx Blacklisted phrase (2.5): Please provide
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: zivy