Sunil Raperia
Sunil Raperia

Reputation: 125

For loop in R for extracting text from png files

I have some .png files in the folder, I need to read them all one by one, and concatenate the text generated from all the files i a single variable. I am using the below code:

filenames <- list.files(path = "C:/Users/tmpxk9/Documents", pattern="*.png")

for (i in seq_along(filenames)) {
  xx <- magick::image_read("filenames[i]")
  xx %>%
    magick::image_resize("2000x") %>%
    magick::image_convert(type = 'Grayscale') %>%
    tesseract::ocr() %>%
    cat()
}

It errors out with the following error:

Error in magick_image_readpath(enc2native(path), density, depth, strip) : 
  rsession.exe: UnableToOpenBlob `C:\Users\tmpxk9\Documents\filenames[i]': No such file or directory @ error/blob.c/OpenBlob/2701

I am sure there is some issue with for loop, I am working on it, but not able to figure out

Upvotes: 0

Views: 475

Answers (1)

Till
Till

Reputation: 6663

The way you are retrieving the file names, they are not including the folder they are stored in. In order to get the full paths with list.files(), you have to include the argument full.names = TRUE.

Upvotes: 1

Related Questions