Skip to content

Instantly share code, notes, and snippets.

@jpambrun
Created April 5, 2024 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpambrun/24aa1af0289d93910c125306b9f4b56b to your computer and use it in GitHub Desktop.
Save jpambrun/24aa1af0289d93910c125306b9f4b56b to your computer and use it in GitHub Desktop.
dcm2json
import dicomParser from "npm:dicom-parser";
import { standardDataElements } from 'npm:dicom-data-dictionary'
const stdin = await Deno.readAll(Deno.stdin)
const dataSet = dicomParser.parseDicom(stdin, {
untilTag: "x7fe00010",
})
var options = {
omitPrivateAttibutes: true,
maxElementLength: 128
};
var instance = dicomParser.explicitDataSetToJS(dataSet, options);
function renameKeys(source, dest) {
dest = dest || {}
for (let key in source) {
if (key[0] !== 'x' || source[key].dataOffset !== undefined) continue
const tag = key.slice(1, 9).toUpperCase()
let newKey = standardDataElements[tag] ? standardDataElements[tag].name : key
if (source[key] instanceof Array) {
dest[newKey] = source[key].map(renameKeys)
} else if (source[key] instanceof Object) {
dest[newKey] = renameKeys(source[key])
} else {
dest[newKey] = source[key]
}
}
return dest
}
const renamedInstance = renameKeys(instance);
console.log(JSON.stringify(renamedInstance, null, 2))
// fd -e dcm -x sh -c 'cat {} | deno run dcm2json.js {} > {.}.json'
// duckdb :memory: 'select PatientName, InstanceNumber from read_json("*.json", maximum_depth=2, union_by_name=true)'
@jpambrun
Copy link
Author

jpambrun commented Apr 5, 2024

Can be used with

cat 1.3.6.1.4.1.25403.14038000964937.11400.20150811124503.6.dcm |  deno run https://gist.githubusercontent.com/jpambrun/24aa1af0289d93910c125306b9f4b56b/raw/35a47a7eca921d013525e7663399970f293b912e/dcm2json.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment