I can't load a file with several pictures via load.images

Hello everyone,

I want to load a file with several pictures but the file is only recognised as value and not data.

So tried to load only one image (see below) and I get following error:

Error in load("C:/Users/Name/Bachelor/A_muscaria_pics/Amanita_muscariaTRUE133095.jpg") : 
  bad restore file magic number (file may be corrupted) -- no data loaded
In addition: Warning messages:
1: In readChar(con, 5L, useBytes = TRUE) :
  truncating string with embedded nuls
2: file ‘Amanita_muscariaTRUE133095.jpg’ has magic number 'ÿØÿà'
  Use of save versions prior to 2 is deprecated

and when I try to plot this picture i get following error:

Error in plot.window(...) : need finite 'ylim' values
In addition: Warning messages:
1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf

I assume that this problem applies to every single one of my pictures. I am still fairly new to R and I have no clue how to solve this problem myself. I am more than happy to provide further information if it is needed.

One of the images that was supposed to be loaded:
Amanita_muscariaTRUE133095

base r doesn't have a load.images function, therefore you must be relying on a package.
Can you name the package please, and share at least a few lines of code of how you use it ?

Sure, following is a part of the code I'm using:

library("imager")
library("OpenImageR")
library("stringr")
library("foreach")
library("openxlsx")
library("here")
library("magrittr")

csvs <- list.files("mask_csv", ".csv", full.names = T)

amanita <-"A_muscaria_pics"


lITV <- list()
for(i in 1:269){
  
  print(i)
  mask <- read.csv2(csvs[i])
  im <- imager::load.image(paste0("A_muscaria_pic", str_extract_all(csvs[i], "\\d+")[[1]][2], "*.jpg",full.names = TRUE))

...

Other pictures can be loaded without a problem only the ones in this specific folder are problematic. It may be related to the way I downloaded those via a code in Rstudio.

hmmm.
Wouldnt it be more straightforward to list.files on the jps to load as images ? then you could dispense with the large paste0 and str_extract_all codes ?

In the folder as they are , can you view them with normal image viewing software to verify that they are images ? its unclear if your image example of the mushroom, represents the file as it is or before your download of it.

what is the way you downloaded those via code in Rstudio ?

First of all thank you for your replies.

I'am using the large paste0 code to pair the images to a corresponding .csv-file in the end since the picture and the csv-file have a matching ID in their name. I don't know if it's possible with the other way.

Concerning the picture: Those are normal pictures and I can open/ view them with my image viewing software. I uploaded the picture from my file.

concerning the method of download:
I used an excel file with URLs to the pictures I wanted to download and I ran a code to do so.

I right clicked your mushroom and saved it to my working directory with the name mushroom.jpg
I ran the following code and was able to see the image

library(imager)

img1 <- load.image("mushroom.jpg")

imager::display(img1)

can you do this ?

Just tried it and sadly it doesn't work

which part and how ?

I saved the image i uploaded, also named it mushroom.jpg and ran this line

img1 <- load.image("mushroom.jpg")

and i get this error

Error in wrap.url(file, load.image.internal) : File not found

where did you save it to ? where is your working directory ? getwd()
make these match by either

  • moving the file to your working directory,
  • or by changing your working directory to where the file is saved to.

Now this one worked for me. I forgot to that I wasn't using the "here"-package this time

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.