list.files (consultation)

Hello, I am new to this community, I must develop a job in my laboratory. I have the following concern, I have a folder with thousands of images, each one represents a sample of dried blood stain on filter paper, I must design an R code to obtain only certain images of interest within those thousands of images, their format is * .jpg. The names of the images correspond to a monthly consecutive that changes per year, for example A012000233.jpg where the (A01 is the month), (20 is the year) (00233 consecutive monthly), what suggestions could you give me for that, it I would greatly appreciate it.

What designates the names of the files that you want to obtain?

I believe you will find the package {fs} (short for file system) relevant to the task at hand.

For example fs::dir_info() returns a data frame of files & their attributes.

This piece of code would find all png type files in my home Pictures directory, and assign them to to variable pngs; it can be then filtered easily. The file names themselves will be in pngs$path column.

pngs <- fs::dir_info("~/Pictures", recurse = T, glob = "*.png")

Many thanks to both jlacko and FJCC for their contributions. I want to apologize, because perhaps I was not very clear with my query and generated confusion. I think they both give me good options to carry out my project. I just want to leave a clearer example of what I want to do. Suppose I have 1000 images in a folder, but I want to search only 30 that are scattered in that folder, I have the list of images of interest, my intention is to automate the search for those 30 images and read them in R, both have given me good options to perform a proper search process.
It is not my intention to take away your valuable time in this, but I am very new working with R, I am learning this valuable tool that will help me a lot in the development of my work, it is important the support that you give us for a better learning

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

Hello jlacko, thanks for your proposal, it is interesting and I will try to put it into practice. I would like to know if later with this solution I can upload the images of interest?

Thanks for your interest in helping me, I'm not sure if I answered your question well. I must tell you that the files of interest can be obtained by creating a list, since I have them in a database. My interest is to automate the search process for some files contained in the folder, and I would like to load those images in R. The idea is not to search for them manually, since with them I must make presentations to clients.

What do you mean by "upload"?

Once you have a vector of file names of interest you can utilize it as you see fit.

This may involve reading them into R, image processing them (the {magick} package, interfacing to ImageMagick, is my personal favorite in this respect, but there are certain to be other tools), or simply copying them from place A to place B - for this consider fs::file_copy().

I am not sure I understand exactly what you want to do but here is a first step. If you have a list of names extracted from a data base, you can read the jpg data into a list with a simple loop. In this example, I made the list of names by hand.

library(OpenImageR)
ImageNames <- c("FirstImage.jpg", "SecondImage.jpg", "ThirdImage.jpg")
ListOfImg <- vector(mode = "list", length = length(ImageNames))
for (i in seq_along(ImageNames)) {
  ListOfImg[[i]] <- readImage(ImageNames[i])
}

I used the OpenImageR package only because I once used that to read some jpg files. I do not have enough experience with using jpg files to say whether another package might be better for your case.

You mention using the images in a presentation. In that case, you may want to just read each image with readImage() as you need it, rather than store all of them in a list as I did above.