Home > Software design >  How to load jpeg picture from directory
How to load jpeg picture from directory

Time:12-28

I'm trying to load and plot a picture from that path :

C:\Users\Rayane_2\Desktop\Data\PCB-DATASET-master\PCB-DATASET-master\01_missing_hole_01.jpeg

I tried :

library(imager)
file <- system.file('C:\Users\Rayane_2\Desktop\Data\PCB-DATASET-master\PCB-DATASET-master\01_missing_hole_01.jpeg',package='imager')
im <- load.image(file)
im # file not found

Example of correct run provided by the package :

library(imager)
file <- system.file('extdata/parrots.png',package='imager')
#system.file gives the full path for a file that ships with a R package
#if you already have the full path to the file you want to load just run:
#im <- load.image("/somedirectory/myfile.png")
im <- load.image(file)
plot(im) #Parrots!

Thank you for your help !

CodePudding user response:

  1. Backslashes as directory delimiters must be escaped, you should have seen the error

    Error: '\U' used without hex digits in character string starting ""C:\U"
    

    Escape it with another backslash, as in

    'C:\\Users\\Rayane_2\\Desktop\\Data\\PCB-DATASET-master\\PCB-DATASET-master\\01_missing_hole_01.jpeg'
    

    Even on windows, though, one can use forward-slashes, so this also works:

    'C:/Users/Rayane_2/Desktop/Data/PCB-DATASET-master/PCB-DATASET-master/01_missing_hole_01.jpeg'
    
  2. system.file only finds files within packages. From ?system.file:

    Description
    
        Finds the full file names of files in packages etc.
    
    
    Arguments
        ...: character vectors, specifying subdirectory and file(s) within
             some package.  The default, none, returns the root of the
             package.  Wildcards are not supported.
    
    

    This means that all paths provided in the ... arguments need to be relative. One such example is what you put in your question,

    system.file('extdata/parrots.png',package='imager')
    

    If you look at the file structure of the installed package (perhaps C:/Users/Rayane_2/R/win_library/4.1/imager), you'll see directories named Meta, R, data, doc, help, html, and (not found in every package) extdata. In that directory must be parrots.png. If a file is found within the specified package's installation directory, then the full (absolute) path of the file you seek is returned.

    The value of system.file is that you may not know the full path. This is a good method when (1) doing something programmatically where other users will be using your code; (2) you have multiple library paths in .libPaths() and don't know which one contains the package, and you don't want to check all of them yourself; or (3) you want shorter and more self-documenting code.

    If you already know the full path of a file, then system.file doesn't help.

    Bottom line, system.file is the wrong function for this.

  3. Just load the file directly.

    library(imager)
    im <- load.image('C:/Users/Rayane_2/Desktop/Data/PCB-DATASET-master/PCB-DATASET-master/01_missing_hole_01.jpeg')
    

CodePudding user response:

Use one of these.

For more info see ?Quotes, ?file.path, ?Sys.getenv, ?path.expand. The path.expand example will depend on how your home directory is set but typically it has been set to C:\Users\yourname\Documents .

file.path("C:", "Users", "Rayane_2", "Desktop", "Data", "PCB-DATASET-master",
  "PCB-DATASET-master", "01_missing_hole_01.jpeg")

file.path(Sys.getenv("USERPROFILE"), "Desktop", "Data", "PCB-DATASET-master",
  "PCB-DATASET-master", "01_missing_hole_01.jpeg")

r"{C:\Users\Rayane_2\Desktop\Data\PCB-DATASET-master\PCB-DATASET-master\01_missing_hole_01.jpeg}"

"C:\\Users\\Rayane_2\\Desktop\\Data\\PCB-DATASET-master\\PCB-DATASET-master\\01_missing_hole_01.jpeg"

# this depends on how your home variable has been set but the
#   setting is often such that this works
path.expand("~\\..\\Desktop\\Data\\PCB-DATASET-master\\PCB-DATASET-master\\01_missing_hole_01.jpeg")

"C:/Users/Rayane_2/Desktop/Data/PCB-DATASET-master/PCB-DATASET-master/01_missing_hole_01.jpeg"

# after entering this navigate to file. This will display the path
#   to the file and you can then copy and paste it from the 
#   R console into your code.
file.choose()
  • Related