Skip to content
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

modifying dicom tags #64

Closed
jaykumarthaker opened this issue Jun 21, 2019 · 13 comments
Closed

modifying dicom tags #64

jaykumarthaker opened this issue Jun 21, 2019 · 13 comments

Comments

@jaykumarthaker
Copy link

jaykumarthaker commented Jun 21, 2019

I am completely newbie to DICOM and was assigned to modify DICOM tags ;) so please bear with me.

My question is very simple, how do we modify DICOM tags and save it as file?

We will be using this module in Electron App so will have access to Node's fs module. I tried it on my own, something like:

const dcmjs = require("dcmjs");
const fs = require("fs");

let arrayBuffer = fs.readFileSync("C:\\tmp\\attachment.file.9a386d4b4506001a.646374303035372e64636d.dcm").buffer;

let DicomDict = dcmjs.data.DicomMessage.readFile(arrayBuffer);

DicomDict.upsertTag("x00100010", "PN", "JAYKUMAR THAKER");

let new_file_WriterBuffer = DicomDict.write();

fs.writeFileSync("C:\\tmp\\attachment_test.dcm", new Buffer(new_file_WriterBuffer));

With this, file get saved correctly but when I read the DICOM again, it has added new tag like:
x00000010 length=16; VR=PN; "JAYKUMAR THAKER" rather than modifying "x00100010"

I am sure this might not be the correct way. However any level of guidance would be really appreciated.

@pieper
Copy link
Collaborator

pieper commented Jun 21, 2019

I'd suggest something more like this:

const dcmjs = require("dcmjs");
const fs = require("fs");

const filePath = "/Users/pieper/data/public-dicom/MRHead-multiframe+seg/MRHead-multiframe.dcm"

let arrayBuffer = fs.readFileSync(filePath).buffer;

let DicomDict = dcmjs.data.DicomMessage.readFile(arrayBuffer);

const dataset = dcmjs.data.DicomMetaDictionary.naturalizeDataset(DicomDict.dict);

dataset.PatientName = "Name^Somebody's"

DicomDict.dict = dcmjs.data.DicomMetaDictionary.denaturalizeDataset(dataset);

let new_file_WriterBuffer = DicomDict.write();

fs.writeFileSync("/tmp/file.dcm", new Buffer(new_file_WriterBuffer)); 

@jaykumarthaker
Copy link
Author

jaykumarthaker commented Jun 21, 2019

Man, I am over the moon! 👍 Thank you so much @pieper for quick & accurate response!

Just one last question, is it possible to add new Tag with Array as value? With pydicom we used to do this:

import pydicom as dicom
tag=dicom.tag.Tag(0x20,0x32)
ds.add_new(tag,'DS',['0','0','0'])

how can achieve something similar like this one?
once again, thanks a bunch :)

@pieper
Copy link
Collaborator

pieper commented Jun 21, 2019

No problem - hope you find this useful. After years of working with various dicom toolkits I tried to make dcmjs as painless as possible, so for most purposes you just assign a member to the dataset named based on the data dictionary, so there's no hex codes needed and the values are simple javascript types wherever possible (numbers, strings, lists, objects, etc).

dataset.PatientName = "Name^Somebody's"
dataset.ImagePositionPatient = [0, 0, 0];

dataset.ProcedureCodeSequence = {
  CodeValue: "M2197",
  CodingSchemeDesignator: "GEIIS",
  CodeMeaning: "BWH MR PELVIS WWO CONTRAST M2197",
};

@jaykumarthaker
Copy link
Author

Indeed! dcmjs helped me to develop things very smoothly. I have been searching for JS tools available for DICOM, and those efforts ended on dcmjs. I saw your work from dcmio to dcmjs. Amazing job.. Once we have documentation in-place, it will take no time for developers to understand the APIs, even for newcomers to DICOM world.

kudos to you and dcmjs team! 🥇 👍

@pieper
Copy link
Collaborator

pieper commented Jun 21, 2019

Thanks!

By the way if you're interested in such things you might want to join us for a Project Week (there's one next week at MIT, then another in Canada in July and one in Spain in January).

@jaykumarthaker
Copy link
Author

Sounds great, very appreciative of your offer. Looking forward to meet you in London, Canada!

@pieper
Copy link
Collaborator

pieper commented Jun 21, 2019

Looking forward to meet you in London, Canada!

Oooo, too bad that's the one I'll miss, but you'll meet lots of great folks there!

@ikhwanafath
Copy link

I'd suggest something more like this:

const dcmjs = require("dcmjs");
const fs = require("fs");

const filePath = "/Users/pieper/data/public-dicom/MRHead-multiframe+seg/MRHead-multiframe.dcm"

let arrayBuffer = fs.readFileSync(filePath).buffer;

let DicomDict = dcmjs.data.DicomMessage.readFile(arrayBuffer);

const dataset = dcmjs.data.DicomMetaDictionary.naturalizeDataset(DicomDict.dict);

dataset.PatientName = "Name^Somebody's"

DicomDict.dict = dcmjs.data.DicomMetaDictionary.denaturalizeDataset(dataset);

let new_file_WriterBuffer = DicomDict.write();

fs.writeFileSync("/tmp/file.dcm", new Buffer(new_file_WriterBuffer)); 

Hi Piper, after I used your code, i got the wrong image like this.

2

As you can see, the image become red flag. How to fix it ? thanks

@ikhwanafath
Copy link

ikhwanafath commented Mar 13, 2020

3
This is the original dicom before edit tag with dcmjs.
Note : the orange color is cencored. Cause that is my face

IMG022911.zip
this's the dcm file

@satyam-qure
Copy link

I'd suggest something more like this:

const dcmjs = require("dcmjs");
const fs = require("fs");

const filePath = "/Users/pieper/data/public-dicom/MRHead-multiframe+seg/MRHead-multiframe.dcm"

let arrayBuffer = fs.readFileSync(filePath).buffer;

let DicomDict = dcmjs.data.DicomMessage.readFile(arrayBuffer);

const dataset = dcmjs.data.DicomMetaDictionary.naturalizeDataset(DicomDict.dict);

dataset.PatientName = "Name^Somebody's"

DicomDict.dict = dcmjs.data.DicomMetaDictionary.denaturalizeDataset(dataset);

let new_file_WriterBuffer = DicomDict.write();

fs.writeFileSync("/tmp/file.dcm", new Buffer(new_file_WriterBuffer)); 

I want to confirm whether this will work for all the .dcm files as I'm getting an error (First argument to DataView constructor must be an ArrayBuffer)

@pieper
Copy link
Collaborator

pieper commented Nov 3, 2023

I want to confirm whether this will work for all the .dcm files

There are so many different flavors of DICOM in the wild (some valid and some not) so it's hard to say for sure.

I'm getting an error (First argument to DataView constructor must be an ArrayBuffer)

If this is happening on data that used to work there could be a regression. If so please file a new issue. Or if you have example data that doesn't work consider making it available, filing an issue, and making a PR with a test case.

@satyam-qure
Copy link

I want to confirm whether this will work for all the .dcm files

There are so many different flavors of DICOM in the wild (some valid and some not) so it's hard to say for sure.

I'm getting an error (First argument to DataView constructor must be an ArrayBuffer)

If this is happening on data that used to work there could be a regression. If so please file a new issue. Or if you have example data that doesn't work consider making it available, filing an issue, and making a PR with a test case.

Thank you for your prompt reply. I have submitted an issue and attached the file for reference.

@pieper
Copy link
Collaborator

pieper commented Nov 3, 2023

For future reference: #370

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants