# Files reading issues. Can you please help?

**URL:** <https://discourse.itk.org/t/files-reading-issues-can-you-please-help/3375>\
**Category:** Algorithms\
**Tags:** simpleitk, python\
**Created:** [August 10, 2020, 5:26am UTC](https://discourse.itk.org/t/files-reading-issues-can-you-please-help/3375 "2020-08-10T05:26:12Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![blizzardboi\_2020](https://discourse.itk.org/letter_avatar_proxy/v4/letter/b/13edae/32.png) [@blizzardboi\_2020](https://discourse.itk.org/u/blizzardboi_2020)\
**Post date:** [August 10, 2020, 5:26am UTC](https://discourse.itk.org/t/files-reading-issues-can-you-please-help/3375/1 "2020-08-10T05:26:13Z")

</div>

**C:/Users/**  **Desktop/train/’**

**PX1\_0000- T2W.nii.gz + seg.nii.gz**

**PX1\_0001- T2W.nii.gz+ seg.nii.gz  
.  
.  
… PX1\_0200- T2W.nii.gz+ seg.nii**

```auto
path1='C:/Users/Desktop/train/'

def load_data(path):
  my_dir = sorted(os.listdir(path))
  for p in tqdm(my_dir):
    data1=[]
    gt=[]
    data_list = os.path.join(path+p)
    img = sitk.ReadImage(path + p + '/'+'seg.nii.gz')    
    seg = sitk.GetArrayFromImage(img)
    img = sitk.ReadImage(path + p + '/'+ 'T2W.nii.gz')
    T2W = sitk.GetArrayFromImage(img)
    data1.append([T2W])
    gt.append(seg)
  data1 = np.asarray(data1,dtype=np.uint8)
  gt = np.asarray(gt,dtype=np.uint8)
  return data1,gt

data1,gt1 = load_data(path1)

```

---

<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:** [August 10, 2020, 12:52pm UTC](https://discourse.itk.org/t/files-reading-issues-can-you-please-help/3375/2 "2020-08-10T12:52:35Z")

</div>

Hello @blizzardboi_2020,

What exactly is the issue with the file reading? You just provided code without stating the problem.

---

<div class="post-metadata">

**Author:** ![blizzardboi\_2020](https://discourse.itk.org/letter_avatar_proxy/v4/letter/b/13edae/32.png) [@blizzardboi\_2020](https://discourse.itk.org/u/blizzardboi_2020)\
**Post date:** [August 10, 2020, 2:26pm UTC](https://discourse.itk.org/t/files-reading-issues-can-you-please-help/3375/4 "2020-08-10T14:26:49Z")

</div>

Hi. The output is showing the size of the contents of 1st folder oy i.e. 19,384x384. I want to get all the shapes from all folder in a boy array. However, some files has different shape(21,384,384) and (23,384,384). Can you tell me where and how should I use resize operation for this? I want the final storage of the array to be (5,19,384,384). I read the docs but stilll finding it hard to do it. if you can make the changes, I will be grateful to you.

---

<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:** [August 10, 2020, 3:58pm UTC](https://discourse.itk.org/t/files-reading-issues-can-you-please-help/3375/5 "2020-08-10T15:58:11Z")

</div>

Hello @blizzardboi_2020,

My understanding of your request:  
You have multiple images with different sizes, and you want to force all of them to have the same size (number of pixels per dimension).

The following code will read an image and resample it to a desired size if there is a need to resample:

```auto
import SimpleITK as sitk

def read_resample(file_name, desired_size):
    img = sitk.ReadImage(file_name)
    actual_size = img.GetSize()
    if not all([a_sz == d_sz for a_sz, d_sz in zip(actual_size, desired_size)]):
      new_spacing = [(sz-1)*spc/(nsz-1) for nsz,sz,spc in zip(desired_size, actual_size, img.GetSpacing())]
      img = sitk.Resample(img, desired_size, sitk.Transform(), sitk.sitkLinear,
                          img.GetOrigin(), new_spacing, img.GetDirection())
    return img

```

If this does not solve your issue, please provide additional details so that we better understand it.

---

<div class="post-metadata">

**Author:** ![blizzardboi\_2020](https://discourse.itk.org/letter_avatar_proxy/v4/letter/b/13edae/32.png) [@blizzardboi\_2020](https://discourse.itk.org/u/blizzardboi_2020)\
**Post date:** [August 10, 2020, 4:40pm UTC](https://discourse.itk.org/t/files-reading-issues-can-you-please-help/3375/6 "2020-08-10T16:40:47Z")

</div>

But before resizing, I am unable to read all the files from 5 folders. Zivy can you please manipulate it according to the folder style(.nii.gz)? That would be great. I am facing difficulties in reading through the files.

---

<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:** [August 10, 2020, 5:09pm UTC](https://discourse.itk.org/t/files-reading-issues-can-you-please-help/3375/7 "2020-08-10T17:09:23Z")

</div>

Dear @blizzardboi_2020,

This forum is for questions with respect to ITK/SimpleITK and not a general “how to” in Python. I suggest that pursue generic Python questions in more appropriate forums.

More specifically, how to traverse a directory structure, `os.walk()` and how to create file names, `os.path.join()`. Once you are able to generate the file names specific to your directory structure you should be able to read and resample them with the function provided in the previous post.

---

<div class="post-metadata">

**Author:** ![blizzardboi\_2020](https://discourse.itk.org/letter_avatar_proxy/v4/letter/b/13edae/32.png) [@blizzardboi\_2020](https://discourse.itk.org/u/blizzardboi_2020)\
**Post date:** [August 13, 2020, 2:39pm UTC](https://discourse.itk.org/t/files-reading-issues-can-you-please-help/3375/8 "2020-08-13T14:39:25Z")

</div>

Hi! I solved my problem of reading. In this script the desired size will be an array like this right?

```auto
Path=D:/Train/....
img1=read_resample(Path, [384,384,30])

```

---

<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:** [August 13, 2020, 2:46pm UTC](https://discourse.itk.org/t/files-reading-issues-can-you-please-help/3375/9 "2020-08-13T14:46:40Z")

</div>

Yes.

---

<div class="post-metadata">

**Author:** ![blizzardboi\_2020](https://discourse.itk.org/letter_avatar_proxy/v4/letter/b/13edae/32.png) [@blizzardboi\_2020](https://discourse.itk.org/u/blizzardboi_2020)\
**Post date:** [August 17, 2020, 10:59am UTC](https://discourse.itk.org/t/files-reading-issues-can-you-please-help/3375/11 "2020-08-17T10:59:35Z")

</div>

Hi Zivy, can we normally add 2 .nii volumes of a single patient (same size) after importing each other separately? Actually some slices cover some information in Vol1, some for Vol 2.  
Vol=vol1+vol2.

---

<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:** [August 17, 2020, 12:59pm UTC](https://discourse.itk.org/t/files-reading-issues-can-you-please-help/3375/12 "2020-08-17T12:59:35Z")

</div>

Hello @blizzardboi_2020,

To add two images in SimpleITK their size, spacing, origin, and direction cosine matrices need to match. Please see [this Jupyter notebook](https://github.com/InsightSoftwareConsortium/SimpleITK-Notebooks/blob/master/Python/03_Image_Details.ipynb) section titled “Image operations”.

As you are just starting with SimpleITK I highly recommend that you invest a couple of hours and take our [new online tutorial](https://simpleitk.org/TUTORIAL/) which includes both Jupyter notebooks and videos explaining them. This will give you a solid foundation in SimpleITK.

---

<div class="post-metadata">

**Author:** ![blizzardboi\_2020](https://discourse.itk.org/letter_avatar_proxy/v4/letter/b/13edae/32.png) [@blizzardboi\_2020](https://discourse.itk.org/u/blizzardboi_2020)\
**Post date:** [August 17, 2020, 1:00pm UTC](https://discourse.itk.org/t/files-reading-issues-can-you-please-help/3375/13 "2020-08-17T13:00:05Z")

</div>

Thanks!!! I got it
