-1

I want to read 12 images at a time in R. I don't know how to do it. I am complete new to working on images in R. How can I read couple of images from a folder in my system? I am using windows10 operating system. RAM 8 gb. CORE i5 processor. GPU is Intel(R) HD Graphics 620.

I am able to read only single image in R and that image is displaying as numeric values. I tried to convert it into raster format and then tried to print image to view the image. But I am still finding the color codes in values but not the image in print.

Can anyone help me on this?

Thanks a lot.

install.packages("magick")
library(magick)
install.packages("rsvg")
install.packages("jpeg")
library(jpeg)


img <- readJPEG("C:/Users/folder/Abc.jpg", native = FALSE)
img1 <- as.raster(img, interpolate = F)
print(img1)

I want to read couple of images at a time into R console and want to view or print images.

krishna
  • 401
  • 6
  • 16

1 Answers1

0

The suggested duplicate gives you the basics for how to read in a number of files at once, but there are a few potential gotchas, and it won't help you with displaying the images.

This first bit is purely to set up the example

library(jpeg)
library(grid)

# Create a new directory and move to it
tdir <- "jpgtest"
dir.create(tdir)
setwd(tdir)

# Copy the package:jpeg test image twice, once as .jpg and once as .jpeg
# to the present working directory 
file.copy(system.file("img", "Rlogo.jpg", package="jpeg"), 
  to=c(file.path(getwd(), "test.jpg"), file.path(getwd(), "test.jpeg")))

Then we can list the files, either using a regex match, or choose them interactively, then read and store the images in a list.

# Matches any file ending in .jpg or .jpeg
(flist <- list.files(pattern="*\\.jp[e]?g$"))

# Interactive selection
flist <- file.choose()

jpglist <- lapply(flist, readJPEG)

To display the images I tend to use grid, but there are a number of alternatives.

grid.raster(jpglist[[1]], interpolate=FALSE)

Remove temporary directory

setwd("..")
unlink(tdir)

enter image description here

AkselA
  • 8,153
  • 2
  • 21
  • 34
  • I am getting "Error in rasterGrob(image, x = x, y = y, width = width, height = height, : object 'jpglist' not found" – krishna Jun 17 '19 at 03:56
  • I want to read multiple jpeg/jpg files and save into a variable. How? – krishna Jun 17 '19 at 04:05
  • @krishna: Sorry, seems like i forgot a line while copying code over. should work now. – AkselA Jun 17 '19 at 04:53
  • library(jpeg) library(grid) ##To create a folder tdir <- "jpgtest" dir.create(tdir) ##to set working directory setwd(tdir) ##to have a list of JPEG/JPG files (flist <- list.files(pattern="*\\.jp[e]?g$")) grid.raster(jpglist, interpolate = FALSE). The code is not working. – krishna Jun 17 '19 at 05:13
  • @krishna: Which part? – AkselA Jun 17 '19 at 10:46
  • grid.raster(jpglist, interpolate = FALSE) – krishna Jun 19 '19 at 12:06
  • Is that still because object `jpglist` can't be found? – AkselA Jun 19 '19 at 12:53
  • Error: unexpected symbol in "grid.raster(jpglist, interpolate = FALSE)." – krishna Jun 20 '19 at 06:03
  • Are you sure `jpglist` contains image data? Also you should use `jpglist[[1]]`, `jpglist[[2]]` etc. to access the individual images. If you want to display several at once you need to do something more advanced. – AkselA Jun 20 '19 at 08:32