Skip to content

Add modify subcommand for editing dicom tags #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions bin/dcmjs.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node
import { Command } from 'commander';
import { readDicom, instanceDicom, dumpDicom } from '../src/index.js';
import { readDicom, writeDicom, instanceDicom, dumpDicom, modifyDicom } from '../src/index.js';

const program = new Command();

Expand Down Expand Up @@ -28,4 +28,20 @@ program.command('instance')
})


program.parse();
function assignment(value, dummyPrevious) {
return value.split('=');
}

program.command('modify')
.description('Change values in the dicom header')
.argument('<part10in>', 'part 10 input file path')
.option('-l, --logLevel <level>', 'logging level, TRACE, DEBUG, INFO, WARN, ERROR, default: WARN')
.option('-o, --out <part10out>', 'part 10 output file path')
.requiredOption('-r, --replace <tag>=<value>', 'Replace or add tag value', assignment)
.action(async (fileName, options) => {
let dicomDict = readDicom(fileName);
dicomDict = modifyDicom(dicomDict, options);
writeDicom(options.out, dicomDict);
})

program.parse();
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"dependencies": {
"commander": "^12.1.0",
"config-point": ">=0.5.1",
"dcmjs": "^0.29.5",
"dcmjs": ">0.29.5",
"dcmjs-dimse": "0.1.27",
"dicomweb-client": "0.8.4"
},
Expand Down
17 changes: 17 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ export function readDicom(fileName) {
return dicomDict;
}

export function writeDicom(fileName, dicomDict) {
const writeBuffer = dicomDict.write();
fs.writeFileSync(fileName, Buffer.from(writeBuffer));
}

export function dumpDicom(dicomDict, options = {}) {
if (dicomDict.meta) {
console.log("Metadata");
Expand Down Expand Up @@ -90,3 +95,15 @@ export function instanceDicom(dicomDict, options = {}) {
const result = pretty ? JSON.stringify(dicomDict.dict, null, 2) : JSON.stringify(dicomDict.dict);
console.log('', result);
}

export function modifyDicom(dicomDict, options = {}) {
if (options.logLevel) {
dcmjs.log.setLevel(dcmjs.log.levels[options.logLevel]);
}
const { replace } = options;
const [tag, value] = replace;
const naturalData = DicomMetaDictionary.naturalizeDataset(dicomDict.dict);
naturalData[tag] = value;
dicomDict.dict = DicomMetaDictionary.denaturalizeDataset(naturalData);
return dicomDict;
}
24 changes: 24 additions & 0 deletions testscripts/walk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import glob
import os
import pydicom
import subprocess
import sys

if len(sys.argv) < 3:
print("Usage: python walk.py <inputDirectory> <outputDirectory>")
exit(-1)

inputDirectory = sys.argv[1]
outputDirectory = sys.argv[2]

os.makedirs(outputDirectory, exist_ok=True)

for filePath in glob.glob(f'{inputDirectory}/**/*', recursive=True):
print(filePath)
fileName = os.path.split(filePath)[-1]
beforeDataset = pydicom.read_file(filePath)
print(f"Age before: {beforeDataset.PatientAge}")
outputFilePath = f"{outputDirectory}/{fileName}"
result = subprocess.Popen(f"dcmjs modify {filePath} --logLevel ERROR --out {outputFilePath} --replace PatientAge=021Y".split()).communicate()
afterDataset = pydicom.read_file(outputFilePath)
print(f"Age before: {afterDataset.PatientAge}")
Loading