# Is better to use \`itk.imread(dir)\` than \`itk.imread(file\_list)\`?

**URL:** <https://discourse.itk.org/t/is-better-to-use-itk-imread-dir-than-itk-imread-file-list/7123>\
**Category:** Beginner Questions\
**Tags:** itk, python\
**Created:** [July 24, 2024, 1:55pm UTC](https://discourse.itk.org/t/is-better-to-use-itk-imread-dir-than-itk-imread-file-list/7123 "2024-07-24T13:55:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jinquan\_guan](https://discourse.itk.org/user_avatar/discourse.itk.org/jinquan_guan/32/4299_2.png) [@jinquan\_guan](https://discourse.itk.org/u/jinquan_guan)\
**Post date:** [July 24, 2024, 1:55pm UTC](https://discourse.itk.org/t/is-better-to-use-itk-imread-dir-than-itk-imread-file-list/7123/1 "2024-07-24T13:55:20Z")

</div>

I made an experiment about the `itk.imread` function, and found something interesting.

I putted `.dcm` files with the same series ID in a directory. And then convert the dicom data into nii.gz format. Here come the difference:

**Code-1: random shuffled files**

```auto
import os
import itk
import random

dir_path = "./20210718_010_NECK_T1W_mDIXON_TRA+C/"
results_path = "./results/"

filenames = os.listdir(dir_path)
random.shuffle(filenames)
filenames = [os.path.join(dir_path, f) for f in filenames]
image = itk.imread(filenames)
itk.imwrite(image, results_path + "image_shuffle.nii.gz")

```

Here is the result visualized in ITK-SNAP:

 ![image](https://discourse.itk.org/uploads/default/original/2X/1/10ddeede76595e8c3b434e6b6bf61894f8f8424c.png)

It obviously showed a chaotic scene.

But if I try to convert it by passing a `dir` parameter, it worked well.

**Code-2:**

```auto
import itk

dir_path = "/mnt/pc/Chenq_team/HuangData/MR/935294/20210718_010_NECK_T1W_mDIXON_TRA+C/"
results_path = "./results/"

image = itk.imread(dir_path)
itk.imwrite(image, results_path + "image.nii.gz")

```

In fact, I didn’t mean to bring in random to say the there is a bug in `ITK`.

Instead, I encountered a similar scenario while processing the dicom data. In order to describe my problem, I gave the above two simple examples.

So my question is: Why does `itk.imread` affected by the order of passing `file_list` parameter?

---

<div class="post-metadata">

**Author:** ![jinquan\_guan](https://discourse.itk.org/user_avatar/discourse.itk.org/jinquan_guan/32/4299_2.png) [@jinquan\_guan](https://discourse.itk.org/u/jinquan_guan)\
**Post date:** [July 24, 2024, 1:56pm UTC](https://discourse.itk.org/t/is-better-to-use-itk-imread-dir-than-itk-imread-file-list/7123/2 "2024-07-24T13:56:15Z")

</div>

And here is the normal visualization in Code-2.

 ![image](https://discourse.itk.org/uploads/default/original/2X/5/5ea282776ac816788d3d15240dcae0aa3c709754.png)

---

<div class="post-metadata">

**Author:** ![jinquan\_guan](https://discourse.itk.org/user_avatar/discourse.itk.org/jinquan_guan/32/4299_2.png) [@jinquan\_guan](https://discourse.itk.org/u/jinquan_guan)\
**Post date:** [July 24, 2024, 2:00pm UTC](https://discourse.itk.org/t/is-better-to-use-itk-imread-dir-than-itk-imread-file-list/7123/3 "2024-07-24T14:00:20Z")

</div>

What I do agree with, is that this function gives the programmer a lot of convenience and space for free play.

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [July 24, 2024, 3:19pm UTC](https://discourse.itk.org/t/is-better-to-use-itk-imread-dir-than-itk-imread-file-list/7123/4 "2024-07-24T15:19:19Z")

</div>

If you give the function a list of filenames, it uses the order in the list to construct the volume. If you give it a directory, it uses DICOM-specific rules to sort the list of files so they have the “expected” order.

---

<div class="post-metadata">

**Author:** ![jinquan\_guan](https://discourse.itk.org/user_avatar/discourse.itk.org/jinquan_guan/32/4299_2.png) [@jinquan\_guan](https://discourse.itk.org/u/jinquan_guan)\
**Post date:** [July 25, 2024, 7:11am UTC](https://discourse.itk.org/t/is-better-to-use-itk-imread-dir-than-itk-imread-file-list/7123/5 "2024-07-25T07:11:13Z")

</div>

> [@dzenanz](#):
>
> DICOM-specific rules

How can I use the rules to sort my filenames by myself? If I read a series of dicom files in a unstructured folder, it may be necessary to sort by myself.

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [July 25, 2024, 2:12pm UTC](https://discourse.itk.org/t/is-better-to-use-itk-imread-dir-than-itk-imread-file-list/7123/6 "2024-07-25T14:12:36Z")

</div>

In [this example](https://examples.itk.org/src/io/gdcm/readdicomseriesandwrite3dimage/documentation) the line `fileNames = namesGenerator.GetFileNames(seriesIdentifier)` uses DICOM-specific classes to sort the file names. You can use that for your own purposes, or sort the filenames using some other rules or heuristics, and then pass that list of filenames to `reader.SetFileNames(fileNames)`, or using more dense and simpler syntax of `itk.imread(fileNames)`.

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [July 25, 2024, 2:21pm UTC](https://discourse.itk.org/t/is-better-to-use-itk-imread-dir-than-itk-imread-file-list/7123/7 "2024-07-25T14:21:43Z")

</div>

If you want to learn the rules, take a look at the source code (e.g. [itkGDCMSeriesFileNames.cxx](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/IO/GDCM/src/itkGDCMSeriesFileNames.cxx)  
and [gdcmSerieHelper](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/ThirdParty/GDCM/src/gdcm/Source/MediaStorageAndFileFormat/gdcmSerieHelper.cxx)), or maybe something like [this](https://fuji.chofa.net/webhelp5/languages/en-us/content/html/Image_and_Series_Sorting.htm)?
