Created
April 5, 2024 18:21
-
-
Save jpambrun/24aa1af0289d93910c125306b9f4b56b to your computer and use it in GitHub Desktop.
dcm2json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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