How can loop through a image folder?

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

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.
(...)
3 Likes

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

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

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
3 Likes

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

Please read this pinned post: Please read: make it easier to help you

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.

3 Likes

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.

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

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 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> for (i, v) in enumerate(["a", "b", "c"])
           @show i, v
       end
(i, v) = (1, "a")
(i, v) = (2, "b")
(i, v) = (3, "c")
2 Likes

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:

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

1 Like

"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])?

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

ERROR: LoadError: type DataType has no field open

Looking at the docs for FITSIO.jl, you’ll probably want to use FITSIO.fitsread(DISK[1]).

1 Like

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

Yes, but you can do that with a for loop as you demonstrated in your earlier post. Once you’re getting the results you want with one image, try a for-loop, and ask if you get stuck.

1 Like

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.

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.

1 Like

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.