Skip to Content
go-dicom(3)
Command line

modify

Sets, inserts or deletes attributes of a DICOM file, at any depth in its sequences, and writes the result to a new file or over the input.

When to use it

Use modify to correct a patient name, add an accession number, remove an attribute, fill in a sequence item or set a private attribute, from a shell or a script. Every value is checked against its VR before anything is written, and nothing is written if any edit is refused.

It keeps the transfer syntax and copies Pixel Data as it was read, so it never decodes or re-encodes an image; it does not set Pixel Data. It does not edit the file meta group directly: that group is written from the data set (change SOPInstanceUID, or give -new-uid, and the meta follows). To de-identify, use anonymize, which knows what to remove.

The flags follow dcmtk's dcmodify: -set is -m, -insert is -i, -delete is -e, -set-all is -ma, -delete-all is -ea, -set-file is -mf, -insert-file is -if. In pydicom this is ds.PatientName = ... and ds.save_as.

Examples

Change a value that is there

go-dicom modify -set PatientName='Doe^Jane' ct.dcm out.dcm
Wrote out.dcm: 1 change(s), transfer syntax 1.2.840.10008.1.2.1

-set fails if the attribute is not in the file:

Error: modify: -set InstitutionAddress=x: (0008,0081): not in the file

Insert one attribute and delete another

go-dicom modify -insert AccessionNumber=A1234 -delete PatientBirthDate ct.dcm out.dcm
Wrote out.dcm: 2 change(s), transfer syntax 1.2.840.10008.1.2.1

-insert sets an attribute whether it is there or not. Edits run in the order given, and each flag may be repeated. Multiple values are separated by a backslash: -set 'ImageType=DERIVED\SECONDARY'. Tag= writes the attribute with no value. Add -ignore-missing to skip a -set or -delete whose attribute is absent instead of failing.

Inside a sequence

go-dicom modify -insert 'RequestAttributesSequence[0].RequestedProcedureID=RP1' ct.dcm out.dcm

The item is named in brackets, counted from 0, or [*] for every item; the attribute in it follows a dot, to any depth. -insert creates the sequence and item on the way. -delete 'ReferencedImageSequence[1]' removes an item. Tags work as well as keywords: (0008,1115)[*].(0008,114A)[0].(0008,1155).

Everywhere in the file

go-dicom modify -delete-all InstanceCreationDate -set-all StationName=X ct.dcm out.dcm
Wrote out.dcm: 2 change(s), transfer syntax 1.2.840.10008.1.2.1

-set-all and -delete-all act on the data set and on every item of every sequence, and count each occurrence as a change.

Private attributes

go-dicom modify -insert '(0029,xx10,"ACME 1.0",LO)=v2' ct.dcm out.dcm
go-dicom modify -insert-file '(0009,xx10,"ACME 1.0",OB)=blob.bin' ct.dcm out.dcm

A private attribute is named by group, element within its block as xxee, and private creator in double quotes, with a VR when the private dictionary does not know it. The block is looked up in the file; an insert reserves one when the creator has none (here (0029,0011) ACME 1.0, so the value lands in (0029,1110)). Binary values come from a file with -set-file or -insert-file.

Change the file itself, as a new instance

go-dicom modify -new-uid -insert SeriesDescription=Corrected -in-place scan.dcm
Wrote scan.dcm: 2 change(s), transfer syntax 1.2.840.10008.1.2.1

-new-uid gives the instance a new SOP Instance UID (and the file meta's Media Storage SOP Instance UID with it). Without it the instance keeps its UID, whatever changed.

Warning

-in-place replaces the input. It writes a new file and renames it over the original, so the input is never left half written, but the old content is gone. Without -in-place, naming the input as output is refused.

Notes

  • Values are checked before anything is written: dates as YYYYMMDD, UIDs, code strings, the range of US, SS, UL and SL, value multiplicity and the lengths of PS3.5 6.2. A refusal names the edit: Error: modify: -set StudyDate=2024-01-01: (0008,0020) DA: "2024-01-01": length validation: VR DA value length 10 exceeds maximum 8.
  • Text is read into UTF-8, so the output declares Specific Character Set ISO_IR 192 even when the input declared another; the characters are the same. For the same reason SpecificCharacterSet may only be set to ISO_IR 192. A file that declares no character set needs -insert SpecificCharacterSet='ISO_IR 192' before text outside ASCII.
  • Binary values from a file are taken as little endian; an odd length is padded with a NUL.
  • Exit status is 0 when the file was written, 1 when any edit was refused or the file could not be read.

See also

Options

Set, insert or delete attributes of a DICOM file

USAGE:
  go-dicom modify [options] <input> <output>
  go-dicom modify [options] -in-place <file>

  The input is read whole, every edit is made in the order given, and the
  result is written to <output>. Nothing is written if any edit is refused.
  The input is changed only with -in-place, and then through a new file
  renamed over it, so it is never left half written.

OPTIONS:
  -set <path>=<value>     Change an attribute that is in the file. It is an
                          error if it is not (dcmodify -m).
  -insert <path>=<value>  Set an attribute whether it is there or not,
                          creating the sequences and items on its path
                          (dcmodify -i).
  -delete <path>          Remove an attribute, or an item when the path ends
                          in [n], or every item with [*] (dcmodify -e).
  -set-file <path>=<file>
                          Change a binary attribute that is in the file to
                          the bytes of <file> (dcmodify -mf).
  -insert-file <path>=<file>
                          Set a binary attribute to the bytes of <file>, as
                          -insert does (dcmodify -if).
  -set-all <tag>=<value>  Change the attribute wherever it is: in the data
                          set and in every item of every sequence, to any
                          depth (dcmodify -ma). An error if it is nowhere.
  -delete-all <tag>       Remove the attribute wherever it is (dcmodify -ea).
  -new-uid                Give the instance a new SOP Instance UID. Without
                          it the instance keeps its UID, whatever changed.
  -in-place               Write over the input.
  -ignore-missing         Skip a -set or -delete whose attribute is not in
                          the file, saying so, instead of failing.
  -h, -help               Show this help message

  Each edit flag may be given many times. -set-all and -delete-all take one
  attribute, a keyword, a tag or a private attribute by its creator, and no
  item: it is looked for in every item. They count each occurrence as a change.

PATHS:
  An attribute is a keyword or a tag:
    PatientName   (0010,0010)   0010,0010   00100010
  Inside a sequence, the item is named in brackets, counted from 0, or [*]
  for every item, and the attribute in it follows a dot, to any depth:
    ReferencedSeriesSequence[0].SeriesInstanceUID
    (0008,1115)[*].(0008,114A)[0].(0008,1155)
  A private attribute is named by its group, its element within the block
  as xxee, and its private creator in double quotes; a VR may follow when
  the private dictionary does not know it:
    (0029,xx10,"ACME 1.0")        (0029,xx10,"ACME 1.0",LO)
  Its block (the xx) is looked up in the file. An insert reserves one when
  the creator has none, as PS3.5 7.8.1 requires. A private tag by number,
  (0029,1010), may be set or deleted, and inserted only when the creator of
  its block is there.

VALUES:
  Multiple values are separated by a backslash: -set 'ImageType=DERIVED\SECONDARY'.
  An empty value (Tag=) writes the attribute with no value.
  Each value is checked against the attribute's VR before anything is
  written: dates (DA) as YYYYMMDD, UIDs, code strings, the range of US, SS,
  UL and SL, the number of values the dictionary allows, and the lengths
  PS3.5 6.2 gives. Text outside ASCII needs a Specific Character Set: in a
  file that declares none, insert SpecificCharacterSet='ISO_IR 192' first.
  Binary values (OB, OW, OF, OD, OL, OV, UN) are not given as text but with
  -set-file or -insert-file: the bytes of the file are the value, held
  little endian whatever the file's transfer syntax (the writer swaps a big
  endian one). A file of an odd length is padded with a NUL (PS3.5 7.1.1);
  for OW, OF, OD, OL and OV its length must be a whole number of 2, 4, 8, 4
  and 8-byte words. A private attribute not in the dictionary takes its VR
  from the path. Pixel Data is not set here. The file meta group (0002) is written from the data set and the
  transfer syntax, so it is not edited directly: change SOPInstanceUID, or
  give -new-uid, and the meta follows.

WHAT IS KEPT:
  The transfer syntax. Pixel data is copied as it was read, compressed or
  not, and nothing is decoded or encoded again. Text is read into UTF-8,
  so a file that declared another character set is written declaring
  ISO_IR 192, with the same characters, as anonymize writes it; for the
  same reason SpecificCharacterSet may only be set to ISO_IR 192.

EXAMPLES:
  go-dicom modify -set PatientName='Doe^Jane' in.dcm out.dcm
  go-dicom modify -insert AccessionNumber=A1234 -delete PatientBirthDate in.dcm out.dcm
  go-dicom modify -insert 'RequestAttributesSequence[0].RequestedProcedureID=RP1' in.dcm out.dcm
  go-dicom modify -delete 'ReferencedImageSequence[1]' in.dcm out.dcm
  go-dicom modify -insert '(0029,xx10,"ACME 1.0",LO)=v2' in.dcm out.dcm
  go-dicom modify -insert-file '(0009,xx10,"ACME 1.0",OB)=blob.bin' in.dcm out.dcm
  go-dicom modify -delete-all InstanceCreationDate -set-all StationName=X in.dcm out.dcm
  go-dicom modify -new-uid -set SeriesDescription=Corrected -in-place scan.dcm

On this page