0

I have downloaded the latest R package and am using RStudio and am trying to convert a pgm image into a csv file and am using a readImage function. Although any time I do
img <- readImage(file) where file is the filepath

I get

Error in readImage(file) : could not find function "readImage"

Is there some other pack I need to download or am I using it wrong?

  • 1
    There are literally thousands of R packages, and only a few come bundled with R. None of these include a function called `readImage`. There is a package available called `OpenImageR` that has a function called `readImage`, though I don't know if it handles pgm format. To try it, you need to do `install.packages("OpenImageR")` then, once it is installed, `library(OpenImageR)`. Then try your code again. – Allan Cameron Feb 05 '22 at 12:51
  • In fact, I have just tried it, and it does not suport pgm – Allan Cameron Feb 05 '22 at 12:55
  • Try https://stackoverflow.com/q/12826051/4752675 – G5W Feb 05 '22 at 13:03
  • @AllanCameron Is there perhaps another function that you would know that can handle reading pgm files? I have tried read.pnm but i get a ```Error in file(file, open = "rb") : invalid 'description' argument``` – Iluvatar546 Feb 05 '22 at 13:05
  • @Iluvatar546 yes, check out my answer. As with many image manipulation tasks, the `magick` package is very useful here. – Allan Cameron Feb 05 '22 at 13:11

1 Answers1

3

You can use the magick package to read pgm files.

First, you need to do:

install.packages("magick")

Now you call

library(magick)

In my case, I have a pgm file in my R home directory, so I make the file path with:

file <- path.expand("~/cat.pgm")

Now I can read the image and convert it into a matrix of RGB strings by doing:

img <- image_read(file)
ras <- as.raster(img)
mat <- as.matrix(ras)

To write this to csv format, I can do:

write.csv(mat, "cat.csv", row.names = FALSE)

So now I have the image saved as a csv file. To read this back in, and prove it works, I can do:

cat_csv <- read.csv("cat.csv")
cat_ras <- as.raster(as.matrix(cat_csv))
plot(cat_ras)

Note though that the csv file is very large - 9MB, which is one of the reasons why it is rarely a good idea to store an image as csv.

Created on 2022-02-05 by the reprex package (v2.0.1)

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • this is great thanks! Although it doesn't seem to recognise my file paths, I'm getting ```Error in magick_image_readpath(path, density, depth, strip, defines) : rsession.exe: UnableToOpenBlob `C:\Desktop\file_1.pgm': No such file or directory @ error/blob.c/OpenBlob/2924``` I'm only converting very small images and I need to use matrix manipulation so a csv is alright for what I'm using it for – Iluvatar546 Feb 05 '22 at 13:32
  • @Iluvatar546 that file doesn't exist. Try forward slashes in the file path, as backslashes are escape characters in R strings : `file <- "C:/Desktop/file_1.pgm"` and try the code again. (Obviously ensure the file is definitely there, spelled correctly etc too) – Allan Cameron Feb 05 '22 at 13:39