# Read image and metada from folder full of dicom image

**URL:** <https://discourse.itk.org/t/read-image-and-metada-from-folder-full-of-dicom-image/2841>\
**Category:** Beginner Questions\
**Tags:** sitk\
**Created:** [March 17, 2020, 1:18pm UTC](https://discourse.itk.org/t/read-image-and-metada-from-folder-full-of-dicom-image/2841 "2020-03-17T13:18:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![drcjaudet](https://discourse.itk.org/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@drcjaudet](https://discourse.itk.org/u/drcjaudet)\
**Post date:** [March 17, 2020, 1:18pm UTC](https://discourse.itk.org/t/read-image-and-metada-from-folder-full-of-dicom-image/2841/1 "2020-03-17T13:18:00Z")

</div>

Hello high tea key,  
i am trying to read image and metadata from a folder with the following code.  
It succeed to open the image but find no metadata and i sure they are. How can i access to them?

```python
from _main_import vtk, qt, ctk, slicer
from math import *
import numpy as np
from vtk.util import numpy_support
import SimpleITK as sitk
import sitkUtils as su
import math

data_directory = "D:/travail/M2-2020/199200661/test"
series_IDs = sitk.ImageSeriesReader.GetGDCMSeriesIDs(data_directory)

if not series_IDs:
    print("ERROR:""+data_directory+"\" does not contain DICOM series.")
    sys.exit(1)

for i,series_ID in enumerate(series_IDs):
    series_file_names = sitk.ImageSeriesReader.GetGDCMSeriesFileNames(data_directory, series_ID,useSeriesDetails=False) #useSeriesDetails ?
    print(series_file_names)
    series_reader = sitk.ImageSeriesReader()
    series_reader.SetMetaDataDictionaryArrayUpdate(True)
    series_reader.SetLoadPrivateTagsOn(True)
    series_reader.SetFileNames(series_file_names)
    try:
        img = series_reader.Execute()
        #sitk.Show( img, "Dicom Series" )
        print(img.GetSpacing())
        print(img.GetSize())
        su.PushToSlicer(img, "image_"+series_ID,1)
        for k in series_reader.GetMetaDataKeys(0):
            v = series_reader.GetMetaData(0,k)
            print("({0}) = = \"{1}\"".format(k,v))
    except RuntimeError:
        print ("--> Fundamental error in image layer, skipping...")

```

---

<div class="post-metadata">

**Author:** ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)\
**Post date:** [March 17, 2020, 4:13pm UTC](https://discourse.itk.org/t/read-image-and-metada-from-folder-full-of-dicom-image/2841/2 "2020-03-17T16:13:04Z")

</div>

Hello @drcjaudet,

Your code looks correct, configuring the ImageSeriesReader to load all tags and accessing the tags for the first slice. Have you tried accessing the keys for another slice?

Can you provide the data that causes the issue? Without access to the data it is hard to guess what is causing the issue (given that the code looks fine).

---

<div class="post-metadata">

**Author:** ![drcjaudet](https://discourse.itk.org/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@drcjaudet](https://discourse.itk.org/u/drcjaudet)\
**Post date:** [March 19, 2020, 12:11pm UTC](https://discourse.itk.org/t/read-image-and-metada-from-folder-full-of-dicom-image/2841/3 "2020-03-19T12:11:46Z")

</div>

Hello Zivy,  
i find another way by using:

```python
import dicom
img_metadata=dicom.read_file(series_file_names[0]) #importation des metadatas 
img_metadata.Modality #for example.

```

Thank a lot,  
See you Cyril

---

<div class="post-metadata">

**Author:** ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)\
**Post date:** [March 19, 2020, 1:16pm UTC](https://discourse.itk.org/t/read-image-and-metada-from-folder-full-of-dicom-image/2841/4 "2020-03-19T13:16:55Z")

</div>

Hello @drcjaudet,

Happy to see that you resolved your issue.

One point to keep in mind, in the SimpleITK implementation using the series reader you have access to the meta-data from all slices. This may not be the case when you read a whole image. Likely you don’t care about this, but the underlying reader will most often consolidate the information from all slices in some manner and not retain the per slice information.

---

<div class="post-metadata">

**Author:** ![drcjaudet](https://discourse.itk.org/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@drcjaudet](https://discourse.itk.org/u/drcjaudet)\
**Post date:** [March 19, 2020, 3:20pm UTC](https://discourse.itk.org/t/read-image-and-metada-from-folder-full-of-dicom-image/2841/5 "2020-03-19T15:20:56Z")

</div>

Indeed but for my purpose with PET images it is even better to access to the per slice information so you can compute the SUV value not per exam but per slice.😉  
Have a nice day,  
Cyril
