# How can loop through a image folder?

**URL:** <https://discourse.julialang.org/t/how-can-loop-through-a-image-folder/60906>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [May 10, 2021, 5:54pm UTC](https://discourse.julialang.org/t/how-can-loop-through-a-image-folder/60906 "2021-05-10T17:54:34Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![assia855](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/assia855/32/22845_2.png) [@assia855](https://discourse.julialang.org/u/assia855)\
**Post date:** [May 10, 2021, 5:54pm UTC](https://discourse.julialang.org/t/how-can-loop-through-a-image-folder/60906/1 "2021-05-10T17:54:34Z")

</div>

I have a image folder I would like to loop through it. Can you share with me some useful code on how can I make it work please

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 10, 2021, 6:35pm UTC](https://discourse.julialang.org/t/how-can-loop-through-a-image-folder/60906/2 "2021-05-10T18:35:52Z")

</div>

```julia
help?> readdir
search: readdir

  readdir(dir::AbstractString=pwd();
      join::Bool = false,
      sort::Bool = true,
  ) -> Vector{String}

  Return the names in the directory dir or the current working directory if not given.
(...)

```

---

<div class="post-metadata">

**Author:** ![Jake](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jake/32/46007_2.png) [@Jake](https://discourse.julialang.org/u/Jake)\
**Post date:** [May 10, 2021, 6:46pm UTC](https://discourse.julialang.org/t/how-can-loop-through-a-image-folder/60906/3 "2021-05-10T18:46:30Z")

</div>

If you would like to read a file of a certain type then

```julia
using Glob

files = readdir(glob"*.jl")
for (i, file) in enumerate(files)
           age = Second(round(time() - ctime(file)))
           println("File $file is $age seconds old")
 end

```

giving the result

```julia
File a.jl is 2361 seconds seconds old
File b.jl is 2352 seconds seconds old
File c.jl is 2370 seconds seconds old
File d.jl is 1775 seconds seconds old
File e.jl is 2352 seconds seconds old

```

---

<div class="post-metadata">

**Author:** ![assia855](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/assia855/32/22845_2.png) [@assia855](https://discourse.julialang.org/u/assia855)\
**Post date:** [May 10, 2021, 7:03pm UTC](https://discourse.julialang.org/t/how-can-loop-through-a-image-folder/60906/4 "2021-05-10T19:03:16Z")

</div>

thank you so much for your suggestions. Here’s the code that I wrote, Please can you improve it:  
DISK = glob(“SyntheticData/int\_a\_\*.fits”)

Iu = Array{Float64,3}

Ip = Array{Float64,3}

Theta = Array{Float64,3}

it = 1

for DISKS in DISK

```
Iu[:,:,it] =gain .* DISKS[:,:,1];

Ip[:,:,it] =gain .* DISKS[:,:,2];

Theta[:,:,it] =DISKS[:,:,3];

global it+=1;

```

end

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [May 10, 2021, 7:08pm UTC](https://discourse.julialang.org/t/how-can-loop-through-a-image-folder/60906/5 "2021-05-10T19:08:39Z")

</div>

Please read this pinned post: [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757)

It’s hard to guess what you’re trying to do with your code without further context, and thus hard to help you improve it. If you edit your post to follow the guidelines in the pinned post, you’re likely to get more useful suggestions.

---

<div class="post-metadata">

**Author:** ![assia855](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/assia855/32/22845_2.png) [@assia855](https://discourse.julialang.org/u/assia855)\
**Post date:** [May 10, 2021, 7:13pm UTC](https://discourse.julialang.org/t/how-can-loop-through-a-image-folder/60906/6 "2021-05-10T19:13:27Z")

</div>

I have a folder full of fits images. I would like to iterate through each image in file. I already write this code. I would like to improve it. please help.

```julia
DISK = glob("SyntheticData/int_a_*.fits")
Iu = Array{Float64,3}[]
Ip = Array{Float64,3}[]
Theta = Array{Float64,3}[]

it = 1
for DISKS in DISK
    Iu[:,:,it] =gain .* DISKS[:,:,1];
    Ip[:,:,it] =gain .* DISKS[:,:,2];
    Theta[:,:,it] =DISKS[:,:,3];
    global it+=1;
end
end

```

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [May 10, 2021, 11:31pm UTC](https://discourse.julialang.org/t/how-can-loop-through-a-image-folder/60906/7 "2021-05-10T23:31:38Z")

</div>

You’ll need to be more specific about what you’re trying to do with this chunk of code, and what needs to be improved. It seems to be broken in a few different ways right now, and without knowing what the desired output is, your audience can’t guess what you intended to happen. Pay particular attention to point (3) from the linked PSA - we don’t know the size/shape of your synthetic data.

- `glob` just returns a list of filenames - you’ll probably need something like [FITSIO.jl](https://github.com/JuliaAstro/FITSIO.jl) to actually read the files.
- Will each `int_a_*.fits` frame have the same `M x N x 3` size, or could they vary?
- Your `Iu`, `Ip`, and `Theta` arrays are empty vectors of eltype `Array{Float64,3}`, i.e. they are each able to contain one or more independent 3D arrays. It looks as if you’re trying to index into them as if each is a single 3D array of size `M x N x length(DISK)`. If that’s the case, you should initialize those arrays as `Array{Float64,3}(undef, (size(gain)..., length(DISK)))`.
- When you need both the element and index of an iterator, `enumerate` can be useful:

```julia
julia> for (i, v) in enumerate(["a", "b", "c"])
           @show i, v
       end
(i, v) = (1, "a")
(i, v) = (2, "b")
(i, v) = (3, "c")

```

---

<div class="post-metadata">

**Author:** ![assia855](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/assia855/32/22845_2.png) [@assia855](https://discourse.julialang.org/u/assia855)\
**Post date:** [May 11, 2021, 12:42am UTC](https://discourse.julialang.org/t/how-can-loop-through-a-image-folder/60906/8 "2021-05-11T00:42:41Z")

</div>

Thank you for this awesome suggestion. Regarding FITS I tried various methods but it didn’t work to read the folder. Each time I have this error:

```julia
fits.open("SyntheticData/int_a_*.fits")

```

ERROR: CFITSIO has encountered an error while processing SyntheticData/int\_a\__.fits. Error code 104: could not open the named file  
Detailed error message follows:  
failed to find or open the following file: (ffopen)  
SyntheticData/int\_a\__.fits

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [May 11, 2021, 12:52am UTC](https://discourse.julialang.org/t/how-can-loop-through-a-image-folder/60906/9 "2021-05-11T00:52:05Z")

</div>

`"SyntheticData/int_a_*.fits"` isn’t a valid filename, it’s the pattern you’re looking for with Glob.jl’s filename matching. What happens with `fits.open(DISK[1])`?

---

<div class="post-metadata">

**Author:** ![assia855](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/assia855/32/22845_2.png) [@assia855](https://discourse.julialang.org/u/assia855)\
**Post date:** [May 11, 2021, 1:02am UTC](https://discourse.julialang.org/t/how-can-loop-through-a-image-folder/60906/10 "2021-05-11T01:02:25Z")

</div>

```julia
DISK =glob("SyntheticData/int_a_*.fits")
DISK =FITS.open(DISK[1])
end

```

ERROR: LoadError: type DataType has no field open

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [May 11, 2021, 1:17am UTC](https://discourse.julialang.org/t/how-can-loop-through-a-image-folder/60906/11 "2021-05-11T01:17:15Z")

</div>

Looking at the docs for FITSIO.jl, you’ll probably want to use [`FITSIO.fitsread`](http://juliaastro.github.io/FITSIO.jl/dev/api/#FITSIO.fitsread)`(DISK[1])`.

---

<div class="post-metadata">

**Author:** ![assia855](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/assia855/32/22845_2.png) [@assia855](https://discourse.julialang.org/u/assia855)\
**Post date:** [May 11, 2021, 1:27am UTC](https://discourse.julialang.org/t/how-can-loop-through-a-image-folder/60906/12 "2021-05-11T01:27:51Z")

</div>

I would like to loop through all the fits file existing in the folder not just one.

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [May 11, 2021, 1:37am UTC](https://discourse.julialang.org/t/how-can-loop-through-a-image-folder/60906/13 "2021-05-11T01:37:44Z")

</div>

Yes, but you can do that with a `for` loop as you demonstrated in your [earlier post](https://discourse.julialang.org/t/how-can-loop-through-a-image-folder/60906/6). Once you’re getting the results you want with one image, try a for-loop, and ask if you get stuck.

---

<div class="post-metadata">

**Author:** ![assia855](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/assia855/32/22845_2.png) [@assia855](https://discourse.julialang.org/u/assia855)\
**Post date:** [May 11, 2021, 1:41am UTC](https://discourse.julialang.org/t/how-can-loop-through-a-image-folder/60906/14 "2021-05-11T01:41:02Z")

</div>

thank you. In my folder I got 324 fits file. I already generate the result that I want with one fits file. I would like now to generate the results for all the 324 fits file. Here when I’m stuck.

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [May 11, 2021, 2:45am UTC](https://discourse.julialang.org/t/how-can-loop-through-a-image-folder/60906/15 "2021-05-11T02:45:18Z")

</div>

Can you provide an example of what you’ve tried when processing multiple files? Again, a minimal working example is crucial, otherwise we have no idea of what concept got you stuck.

---

<div class="post-metadata">

**Author:** ![assia855](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/assia855/32/22845_2.png) [@assia855](https://discourse.julialang.org/u/assia855)\
**Post date:** [May 11, 2021, 11:16am UTC](https://discourse.julialang.org/t/how-can-loop-through-a-image-folder/60906/16 "2021-05-11T11:16:44Z")

</div>

Thank you for all your feedbacks and suggestions. It works now. I had problem with size mismatch of my parameters. The code I shared previously works fine.
