Import multiple images in a folder and image_info

Very (and I mean VERY) new to R and I was wondering if there was a way to automate a task I'm trying to achieve. I've read online that the Magick and Imager packages can possibly help me.

I'm trying to obtain image file data (file size, number of pixels, etc.) on a number of images held in a folder, to arrange them into a csv file, where I'll be performing some calculations on them. They are all .jpg files.
I'm really not sure if anyone is able to help me, like I said, I'm pretty new to R so I have no idea where to start. I've installed both imager and magick and have tried a few things that were posted on past posts, but nothing seems to be working/achieving what I want.

I'm trying to use the magick library to load all images into R studio so that I can get a matrix of the data I need for each image. For each image, I would like to have something similar to this:

##   format width height colorspace matte filesize density
## 1    PNG   350    350       sRGB  TRUE        0   72x72

Hi,

Welcome to the RStudio community!
Your request is something we can help with, don't worry :slight_smile:

Here is an example:

library(magick)
library(dplyr)

#Get the list of all images
allFiles = list.files(path = ".", pattern = ".jpg")

#Get the dataframe with info
allInfo = image_info(image_read(allFiles))

#Attach the file names
allInfo$fileName = allFiles

#Save
write.csv(allInfo, "allImageInfo.csv")

Make sure you replace the path names with the path to the correct folders

Hope this helps,
PJ

Hi PJ,

First of all, thanks a lot for helping out!

I followed your instructions, but the terminal returned the following errors:
I made sure to install the dplyr package.

Any explanations? Not sure what these mean. It looks like it wasn't able to open a blob? That path is not the same one as the one that is specified in the variable 'allFiles'.

Any help is greatly appreciated!

library(magick)
library(dplyr)
allFiles = list.files(path = "X:/School/Fall 2019/Thesis/Stimuli/Real Products_WorkInProgress/Dish Soap/Palmolive/IGA", pattern = "*.jpg")
allInfo = image_info(image_read(allFiles))
Error in magick_image_readpath(enc2native(path), density, depth, strip) :
Magick: UnableToOpenBlob `C:\Users\Brandon\Documents\0003500045043.jpg': No such file or directory @ error/blob.c/OpenBlob/2701
allInfo$fileName = allFiles
Error in allInfo$fileName = allFiles : object 'allInfo' not found
write.csv(allInfo, "allImageInfo.csv")
Error in is.data.frame(x) : object 'allInfo' not found

HI,

The mistake is mine :slight_smile: You need to specify that you want the full path name.

library(magick)
library(dplyr)

#Get the list of all images
allFiles = list.files(path = ".", pattern = ".jpg", full.names = T)

#Get the dataframe with info
allInfo = image_info(image_read(allFiles))

#Attach the file names
allInfo$fileName = list.files(path = ".", pattern = ".jpg")

#Save
write.csv(allInfo, "allImageInfo.csv")

PJ

Hi PJ,

This works perfectly.
Can't thank you enough, you've saved me a tonne of time.

Cheers,

RedOrm

1 Like

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