Skip to Content
go-dicom(3)
Command line

codify

Reads a DICOM file and writes Go source that builds the same data set with the go-dicom library, one ds.Add per element.

When to use it

Use codify to start a test fixture, a generator of synthetic files, or an example from a real file, without typing tags and VRs by hand. The output is a complete Go file: a function that returns a *dataset.Dataset and, in package main, a main that builds it.

It does not write the program that saves a file (add a file writer, as Examples shows), and it leaves out large binary values, Pixel Data above all, as TODO comments. Private tags are left out unless asked for.

pydicom has the same tool as pydicom codify; dcmtk has none.

Examples

go-dicom codify ct.dcm
package main

import (
	"fmt"

	"github.com/amrshadid/go-dicom/dataset"
	"github.com/amrshadid/go-dicom/dataelem"
	"github.com/amrshadid/go-dicom/tag"
)

// createDataset creates a DICOM dataset from a file
// Auto-generated by: go-dicom codify
func createDataset() *dataset.Dataset {
	ds := dataset.NewDataset()

	// (0002,0001) File Meta Information Version (OB)
	ds.Add(dataelem.NewDataElement(
		tag.New(0x0002, 0x0001),
		dataelem.OB,
		[]byte{0x00, 0x01},
	))
…
	// TODO: 7FE0,0010 (OW) - Binary data excluded (32768 bytes)
	// TODO: FFFC,FFFC (OB) - Binary data excluded (126 bytes)

	return ds
}

// Total elements: 88

func main() {
	ds := createDataset()
	fmt.Printf("built a data set with %d elements\n", ds.Length())
}

Saved as main.go in a module that requires go-dicom, it runs as it is:

built a data set with 85 elements

Into a package of fixtures

go-dicom codify ct.dcm --output ct_gen.go --package fixtures --function createCT
Code written to: ct_gen.go

Outside package main no main function is written.

Keep private tags, and larger binary values

go-dicom codify ct.dcm --exclude-private=false
go-dicom codify ct.dcm --exclude-size 1000

--exclude-private is on by default; =false turns it off (on CT_small.dcm, 644 lines become 1900). --exclude-size is the largest binary value written out as bytes, 100 by default.

Notes

  • The file meta group (0002) is written into the data set with the rest. When you save the result, the file meta information is what you give the file writer (SetFileMetaInfo), so those elements can be dropped.
  • The code is written to standard output unless --output names a file.
  • Exit status is 0 on success and 1 when the file cannot be read.

See also

Options

COMMAND: codify - Convert DICOM file to Go code

USAGE:
  go-dicom codify <file> [options]

DESCRIPTION:
  Read a DICOM file and produce Go code that can recreate it. Useful for
  documentation, testing, and sharing DICOM file structures.

OPTIONS:
  -h, --help                Show help for this command
  --exclude-size <N>        Exclude binary data larger than N bytes (default: 100)
  --exclude-private         Exclude private tags (default: true)
  --output <file>           Output file (if not specified, writes to stdout)
  --package <name>          Go package name (default: main)
  --function <name>         Function name (default: createDataset)

EXAMPLES:
  go-dicom codify patient.dcm
  go-dicom codify patient.dcm > create_patient.go
  go-dicom codify patient.dcm --output patient.go --exclude-size 1000
  go-dicom codify patient.dcm --exclude-private=false
  go-dicom codify patient.dcm --package mypackage --function createMyDataset
  go-dicom codify -h

On this page