Adding new dicom tag to a dicom series

84 views
Skip to first unread message

Gergely Orsi

unread,
Apr 12, 2020, 2:07:27 PM4/12/20
to pydicom
Dear all,

I've searched the forum for possible solutions, but neither worked for me somehow. 

So what I want to do is to add a dicom tag to all dicom files in a directory. 

The files have no extension, but it doesn't matter for pydicom as far as I now.

So the 0008,0080 tag (Institution Name) is missing somehow from a series and I want to add it. 

I tried:

from glob import glob
from pydicom import dcmread
datasets = [dcmread(fn) for fn in glob("*")]
for ds in datasets:


/*works until here*/

ds.InstitutionName = "OKTATAS"   /*doesn't work, ok I figured out as this tag is not existing, this approach might not work */


ds.add_new(0008,0080,Institution Name, "OKTATAS") /* Doesn't work either */


Could someone help me solving this problem ?

Thank you in advance. 

Gergő

Darcy Mason

unread,
Apr 12, 2020, 2:11:31 PM4/12/20
to pydicom
Can you clarify what "doesn't work" means?  Do you get an error message?  The first form should work fine.  The second (add_new) doesn't look right as shown because the tag shouldn't be two separate parameters, and needs to be hex, e.g. 0x00080080.

Secondly, did you use ds.save_as() after the modification?  The changes won't automatically be written back to file otherwise.

-Darcy

Gergely Orsi

unread,
Apr 12, 2020, 2:22:14 PM4/12/20
to pydicom


Thanks for the quick reply

This is what I got:


>>> from glob import glob
>>> from pydicom import dcmread
>>> datasets = [dcmread(fn) for fn in glob("*")]
>>> for ds in datasets:
... ds.InstitutionName = "OKTATAS"
  File "<stdin>", line 2
    ds.InstitutionName = "OKTATAS"
    ^
IndentationError: expected an indented block

Darcy Mason

unread,
Apr 12, 2020, 2:41:36 PM4/12/20
to pydicom
Well, that's not a pydicom issue, just basic python -- statements inside a `for` loop, `if` block, etc. need to be indented a fixed number of spaces (four spaces by convention).

Gergely Orsi

unread,
Apr 12, 2020, 3:02:20 PM4/12/20
to pydicom
Good. Thanks.

I haven't used Python before. So I'm at:


from glob import glob
from pydicom import dcmread
datasets = [dcmread(fn) for fn in glob("*")]
for ds in datasets:
    ds.InstitutionName = "OKTATAS"
    ds_save()


it gives no error, but nothing happens and the tag wasn't written into the dicoms. 

What did I miss?

Gergely Orsi

unread,
Apr 12, 2020, 3:03:36 PM4/12/20
to pydicom
I'm guessing that the for loop hasn't been executed yet. 
How can I close it to be executed?

Darcy Mason

unread,
Apr 12, 2020, 4:07:25 PM4/12/20
to pydicom
I'd guess that the datasets list is empty.  Your glob("*") will only pick up files in the "current directory", which may not be where you wish to be.

I'd try:

filenames = glob("*")
print(filenames)
datasets
= [dcmread(filename) for filename in filenames]


and then adjust the path etc. and confirm you have filenames.

Gergely Orsi

unread,
Apr 12, 2020, 4:14:52 PM4/12/20
to pydicom
It seems ok, yet it doesn't work:


>>> filenames = glob("*")
>>> print(filenames)
['O0000001', 'O0000002', 'O0000003', 'O0000004', 'O0000005', 'O0000006', 'O0000007', 'O0000008', 'O0000009', 'O0000010', 'O0000011', 'O0000012', 'O0000013', 'O0000014', 'O0000015', 'O0000016', 'O0000017', 'O0000018', 'O0000019', 'O0000020', 'O0000021', 'O0000022', 'O0000023', 'O0000024', 'O0000025', 'O0000026', 'O0000027', 'O0000028', 'O0000029', 'O0000030', 'O0000031', 'O0000032', 'O0000033', 'O0000034', 'O0000035', 'O0000036', 'O0000037', 'O0000038', 'O0000039', 'O0000040', 'O0000041', 'O0000042', 'O0000043', 'O0000044', 'O0000045', 'O0000046', 'O0000047', 'O0000048', 'O0000049', 'O0000050', 'O0000051', 'O0000052', 'O0000053', 'O0000054', 'O0000055', 'O0000056', 'O0000057', 'O0000058', 'O0000059', 'O0000060', 'O0000061', 'O0000062', 'O0000063', 'O0000064', 'O0000065', 'O0000066', 'O0000067', 'O0000068', 'O0000069', 'O0000070', 'O0000071', 'O0000072', 'O0000073', 'O0000074', 'O0000075']
>>> datasets = [dcmread(filename) for filename in filenames]
>>> for ds in datasets:
...     ds.InstitutionName = "OKTATAS"
...     ds.save()
... print("OK")
  File "<stdin>", line 4
    print("OK")
    ^
SyntaxError: invalid syntax

James Kerns

unread,
Apr 13, 2020, 9:42:06 AM4/13/20
to pydicom
Gergely, 
    You have a very simple syntax error (as stated in the stacktrace) and it has nothing to do with pydicom. Since you're rusty on Python I would suggest you use a "linter" to make sure your file is valid Python syntax. All basic IDEs come with one such as VSCode or PyCharm. You can also use an online linter such as this one: http://www.pythonlinter.com/. Also, you may try using the "Highlight code syntax" here in the forum to highlight your code so that the indentations and other formatting show up. Or you can create a gist that will also keep the code formatting. 

I can also tell you right now that ".save()" is not valid. I won't spell it out for you, but you should read the getting started guide or the writing files section for the correct save method. 

Gergely Orsi

unread,
Apr 13, 2020, 1:46:54 PM4/13/20
to pydicom
Thanks. I'm good now.

This was my first encounter with Python.

Quite different from C++, I can tell. 
Reply all
Reply to author
Forward
0 new messages