Adding multiple items to a dicom file

Skip to first unread message

Ali Durmaz

Sep 10, 2020, 7:01:04 PM9/10/20
to pydicom
Hi everybody,

I am quite new not only to pydicom but also dicom in general. I was attempting to save multiple non-standard metadata instances to multiple data elements in a dicom-file within a for-loop. Therefore I tried  the following:

block = ds.private_block(0x000b, "My company 001", create=True)
for i, md in enumerate(md_dict):
     block.add_new(print(hex(i)), "SH", md_dict(md))  

where md_dict is a python dictionary of Metadata instances that I want to add. This was not working (error that NoneType (print(hex(i)) can not be compared to integer) and I am rather certain that this not the way to approach it. The problem was that I don't know how to set the tag element numbers within a loop.

But it did not quite help.

Hope somebody can steer me in the right direction.

Thanks in advance for your efforts.

Best regards
Ali Durmaz

Darcy Mason

Sep 10, 2020, 7:11:16 PM9/10/20
to pydicom
I'm guessing you want something more like:

for i, val in enumerate(md_dict.values()):
     block.add_new(i, "SH", val)

It's hard to tell exactly, though, without seeing what md_dict looks like.

In your original code there were several problems: a print function call pretty much stands on its own and would never be used inside a function call; dictionaries are accessed by a key using square brackets, not round ones. Also, you would not enumerate over a dict; you could use md_dict.items() to get both a key and value, but here it looks like you do not need the key, so my code suggest using md_dict.values().

Ali Durmaz

Sep 11, 2020, 7:55:34 AM9/11/20
to pydicom
Hi Darcy, 

thanks for your quick reply.

using i instead of print(hex(i)) solved the Problem. I thought that a number in hexadecimal formatting was required as the first argument in the function call for "add_new". Therefore, I tried to use the print command.

Thanks for pointing out the best practice. The syntax error was owed to me typing the code snippet from scratch quickly. sorry! 

Your help is very appreciated.

Best regards
Ali Durmaz
Reply all
Reply to author
0 new messages