how to import a lot of images from a folder

hi guys ,
Do you have any idea how to import a lot of images from a folder in my desktop ??

Import them to what/where?

2 Likes

Well i have a folder that contains 144 images and i want to import it in RStudio .

I'm not sure exactly what you mean by "in RStudio." Like, to run a script on all of them?
You can use the load.image() function, and apply it across a list of files, in the same way that you might for files of any other type. See, for example, the StackOverflow thread below:

You can read in a list of .gif filenames from the relative data directory like this...

filenames <- list.files(path = "data", pattern="*.gif")

Similarly you can read the files names into a Tibble by leveraging your operating systems file picker, like this...

library(tidyverse)
library(fs)
files_table <- as_tibble(path_rel(choose.files()))

If you have the filenames in a tibble, you can leverage the purrr package to iterate over each file...

files_table %>% 
  filter(str_detect(path_ext(value), 
                    fixed("csv", ignore_case = TRUE))) %>% 
  mutate(data = map(value, read_csv)) %>%
  unnest()

What do you want to do with the images? Do you have an example of code, even just pseudocode, we could work with?

i've used this to import one image :
library(imager)

im <- load.image("C:/Users/ELHassen/Desktop/ter/anatTest1/T_0_low_0000.jpg")
plot(im)

but for all the images in my folder anatTest1, i've tried this but it does work
Folder <- "C:/Users/ELHassen/Desktop/ter/anatTest1"
files <- list.files(path = Folder, pattern = "*.jpg" )
for (i in seq_along(files)) {
assign(paste("T_0_low_000", i, sep = "."),load.image(files[i]))
}

i've used this to import one image :
library(imager)

im <- load.image("C:/Users/ELHassen/Desktop/ter/anatTest1/T_0_low_0000.jpg")
plot(im)

but for all the images in my folder anatTest1, i've tried this but it does work
Folder <- "C:/Users/ELHassen/Desktop/ter/anatTest1"
files <- list.files(path = Folder, pattern = "*.jpg" )
for (i in seq_along(files)) {
assign(paste("T_0_low_000", i, sep = "."),load.image(files[i]))
}

1 Like

Thanks for the help guys i've found it :hugs:
files <- list.files(path = Folder, pattern = "*.jpg", full.names=TRUE)
all_im <- lapply(files, load.image )

1 Like

You may also like the magick package if doing stuff with those images. I used it to make a gif of all the .png files in a folder:

Final:

3 Likes

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