how to import a lot of images from a folder


#1

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


#2

Import them to what/where?


#3

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


#4

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:


#5

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()

#6

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


#7

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]))
}


#8

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]))
}


#9

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 )


#10

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: