source/dcm_transform.py
def transform(file_count, args, desc_prefix, input_filename, output_filename):
    """Replace data element values to partly transform a DICOM file.
    Note: completely transforming a DICOM file is very complicated; there
    are many things this example code does not address. USE AT YOUR OWN RISK.
    """

    try:
        file_count += 1
        # Load the current dicom file to 'transform'
        dataset = dicom.read_file(input_filename)

        # 3d xforms user cmd options
        compute_3d_transforms(dataset, args)

        set_image_pixels(dataset, args.pixel)  # set pixels in image buffer
        draw_roi(dataset, args.roi)  # set a ROI square in image buffer
        draw_ellipse(dataset, args.elp)  # set an ellipse
        draw_rectangle(dataset, args.rect)  # set a rectangle in image buffer
        draw_frectangle(dataset, args.frect)  # set a filled rectangle in image buffer
        draw_crosshair(dataset, args.crosshair)  # set a crosshair in image buffer

        if args.sn > 0:
            dataset.SeriesNumber = args.sn

        # Anonymize dataset tags
        check_if_anonymize_or_cleanup_needed(dataset, args, False, args.delete_private_tags)

        # Deal with all sorts of dates and time if user asks for it:
        transform_dates(file_count, dataset, args)

        # optional changes of useful tags if they have a non default / set value:
        change_tag_if_arg(dataset, "StudyDescription", args.sdesc)
        change_tag_if_arg(dataset, "InstitutionName", args.iname)
        change_tag_if_arg(dataset, "InstitutionAddress", args.iaddr)
        change_tag_if_arg(dataset, "ProtocolName", args.proto)
        change_tag_if_arg(dataset, "Manufacturer", args.mname)
        change_tag_if_arg(dataset, "ManufacturerModelName", args.mmname)
        change_tag_if_arg(dataset, "PatientID", args.pid)
        change_tag_if_arg(dataset, "PatientName", args.pname)
        change_tag_if_arg(dataset, "PatientBirthDate", args.dob)

        # custom DICOM tags settings alternative
        assign_custom_tags(dataset, args.tags)

        # do useful things with the series description
        if args.desc != '':
            change_tag_if_arg(dataset, "SeriesDescription", args.desc)  # optionally change study desc
        else:  # automatic tracking of transformations
            sdesc = dataset.dir("SeriesDescription")

            try:
                if desc_prefix != '':
                    if len(sdesc) != 0:
                        desc = ' ' + dataset.SeriesDescription
                    else:
                        desc = ''
                    dataset.SeriesDescription = desc_prefix + desc
                    sdesc = dataset.dir("SeriesDescription")  # refresh in case we just created it
            except Exception as exc:
                print(exc)
            if len(sdesc) != 0:
                dataset.SeriesDescription = truncate_str(dataset.SeriesDescription, 63)

        # write the 'transformed' DICOM out under the new filename
        dataset.save_as(output_filename)

    except Exception as exc:
        print(exc)
        dataset = None
        file_count = 0

    return file_count, dataset
Replace data element values to partly transform a DICOM file.
    Note: completely transforming a DICOM file is very complicated; there
    are many things this example code does not address. USE AT YOUR OWN RISK.
    
Replace data element values to partly transform a DICOM file.
Note: completely transforming a DICOM file is very complicated; there
are many things this example code does not address. USE AT YOUR OWN RISK.
Replace data element values to partly transform a DICOM file . Note : completely transforming a DICOM file is very complicated ; there are many things this example code does not address . USE AT YOUR OWN RISK .
def transform(file_count, args, desc_prefix, input_filename, output_filename):
    try:
        file_count += 1
        dataset = dicom.read_file(input_filename)
        compute_3d_transforms(dataset, args)
        set_image_pixels(dataset, args.pixel)  
        draw_roi(dataset, args.roi)  
        draw_ellipse(dataset, args.elp)  
        draw_rectangle(dataset, args.rect)  
        draw_frectangle(dataset, args.frect)  
        draw_crosshair(dataset, args.crosshair)  
        if args.sn > 0:
            dataset.SeriesNumber = args.sn
        check_if_anonymize_or_cleanup_needed(dataset, args, False, args.delete_private_tags)
        transform_dates(file_count, dataset, args)
        change_tag_if_arg(dataset, "StudyDescription", args.sdesc)
        change_tag_if_arg(dataset, "InstitutionName", args.iname)
        change_tag_if_arg(dataset, "InstitutionAddress", args.iaddr)
        change_tag_if_arg(dataset, "ProtocolName", args.proto)
        change_tag_if_arg(dataset, "Manufacturer", args.mname)
        change_tag_if_arg(dataset, "ManufacturerModelName", args.mmname)
        change_tag_if_arg(dataset, "PatientID", args.pid)
        change_tag_if_arg(dataset, "PatientName", args.pname)
        change_tag_if_arg(dataset, "PatientBirthDate", args.dob)
        assign_custom_tags(dataset, args.tags)
        if args.desc != '':
            change_tag_if_arg(dataset, "SeriesDescription", args.desc)  
        else:  
            sdesc = dataset.dir("SeriesDescription")
            try:
                if desc_prefix != '':
                    if len(sdesc) != 0:
                        desc = ' ' + dataset.SeriesDescription
                    else:
                        desc = ''
                    dataset.SeriesDescription = desc_prefix + desc
                    sdesc = dataset.dir("SeriesDescription")  
            except Exception as exc:
                print(exc)
            if len(sdesc) != 0:
                dataset.SeriesDescription = truncate_str(dataset.SeriesDescription, 63)
        dataset.save_as(output_filename)
    except Exception as exc:
        print(exc)
        dataset = None
        file_count = 0
    return file_count, dataset
def transform ( file_count , args , desc_prefix , input_filename , output_filename ) : try : file_count += 1 dataset = dicom . read_file ( input_filename ) compute_3d_transforms ( dataset , args ) set_image_pixels ( dataset , args . pixel ) draw_roi ( dataset , args . roi ) draw_ellipse ( dataset , args . elp ) draw_rectangle ( dataset , args . rect ) draw_frectangle ( dataset , args . frect ) draw_crosshair ( dataset , args . crosshair ) if args . sn > 0 : dataset . SeriesNumber = args . sn check_if_anonymize_or_cleanup_needed ( dataset , args , False , args . delete_private_tags ) transform_dates ( file_count , dataset , args ) change_tag_if_arg ( dataset , "StudyDescription" , args . sdesc ) change_tag_if_arg ( dataset , "InstitutionName" , args . iname ) change_tag_if_arg ( dataset , "InstitutionAddress" , args . iaddr ) change_tag_if_arg ( dataset , "ProtocolName" , args . proto ) change_tag_if_arg ( dataset , "Manufacturer" , args . mname ) change_tag_if_arg ( dataset , "ManufacturerModelName" , args . mmname ) change_tag_if_arg ( dataset , "PatientID" , args . pid ) change_tag_if_arg ( dataset , "PatientName" , args . pname ) change_tag_if_arg ( dataset , "PatientBirthDate" , args . dob ) assign_custom_tags ( dataset , args . tags ) if args . desc != '' : change_tag_if_arg ( dataset , "SeriesDescription" , args . desc ) else : sdesc = dataset . dir ( "SeriesDescription" ) try : if desc_prefix != '' : if len ( sdesc ) != 0 : desc = ' ' + dataset . SeriesDescription else : desc = '' dataset . SeriesDescription = desc_prefix + desc sdesc = dataset . dir ( "SeriesDescription" ) except Exception as exc : print ( exc ) if len ( sdesc ) != 0 : dataset . SeriesDescription = truncate_str ( dataset . SeriesDescription , 63 ) dataset . save_as ( output_filename ) except Exception as exc : print ( exc ) dataset = None file_count = 0 return file_count , dataset
Replace data element values to partly transform a DICOM file.
Replace data element values to partly transform a DICOM file .
"""Replace data element values to partly transform a DICOM file.
    Note: completely transforming a DICOM file is very complicated; there
    are many things this example code does not address. USE AT YOUR OWN RISK.
    """ # Load the current dicom file to 'transform' # 3d xforms user cmd options # set pixels in image buffer # set a ROI square in image buffer # set an ellipse # set a rectangle in image buffer # set a filled rectangle in image buffer # set a crosshair in image buffer # Anonymize dataset tags # Deal with all sorts of dates and time if user asks for it: # optional changes of useful tags if they have a non default / set value: # custom DICOM tags settings alternative # do useful things with the series description # optionally change study desc # automatic tracking of transformations # refresh in case we just created it # write the 'transformed' DICOM out under the new filename
file_count
args
desc_prefix
input_filename
output_filename