0

I am attempting to do some image recognition on pokemon images and I have downloaded a file folder with many images of pokemon. I have library EBImage fro the BiocManager package and used it to create an image into a csv using the code below. But, is there a way to automate this with a for loop maybe and call in all of the images in the folder?

install.packages("BiocManager")
BiocManager::install("EBImage")
library("EBImage")
img <- readImage("c:/Users/hlop5/Downloads/images/pikachu.png") 

img <- channel(img,"grey")

display(img, method = "raster")

write.csv(t(img), "sample.csv",row.names = FALSE)

sample <- read_csv("C:/sample.csv")
  • Have a look at the `list.files()` function in R. [This](https://stackoverflow.com/questions/67244641/how-to-combine-path-and-variable-in-readr-read-csv-list-files-for-loop) question may help. – maydin May 14 '21 at 21:57
  • @maydin thanks. I'll definitely check this out. – user13494526 May 14 '21 at 23:29

1 Answers1

0

You can try :

filenames <- list.files('C:/Users/hlop5/Downloads/images/', 
                         pattern = '\\.png', full.names = TRUE)

apply_function <- function(file) {
  img <- readImage(file) 
  img <- channel(img,"grey")
  write.csv(data.frame(t(img)), 
            paste0(sub('png$', 'csv', basename(file))),row.names = FALSE)
}
lapply(filenames, apply_function)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213