Creating table from image EXIF data

I am very new in R and still learning main concepts. This is my first message here I hope I didn't post it in the wrong place.
I am trying to create a piece of code to:

  1. Read image file names(and path) from a folder
  2. Extract selected EXIF fields of these images into a table.
  3. Save resulting CSV table to a folder (with column names, if it doesn't exist).
  4. On next run, read files from images folder, and add only the newly added files to above table, this time without column names, naturally).

I have tried something below, but no idea about possible pitfalls.
Ideally this should run whenever a new file added and, should record to a database table instead of CSV, but, this is something to start with..
I really appreciate if someone investigates the following code and indicate failure causes,
Thanks in advance,

> setwd("D:/Users/user/Pictures/2011-01-10")
> library(data.table)
> library(dplyr)
> library(exifr)
> f <- paste0(getwd(),"/","exifdata",".csv")
> if (!file.exists(f))
> {
> image_files <- list.files(path = ".", pattern=".JPG", full.names = TRUE)
> current_image_list <- read_exif(image_files, tags = c("Make"))
> write.table(current_image_list, "exifdata.csv", sep = ",", row.names=FALSE, col.names = T, append = F)
> }
> mainimagelist <- read.table("exifdata.csv", header = T, sep = ",")
> image_files <- list.files(path = ".", pattern=".JPG", full.names = TRUE)
> current_image_list <- read_exif(image_files, tags = c("Make"))
> rownames(current_image_list) <- c()
> finalimagelist <- rbind(setDT(mainimagelist), setDT(current_image_list))
> finalimagelist <- distinct(finalimagelist)
> write.table(finalimagelist, "exifdata.csv", sep = ",", row.names = F, col.names = F, append = T)

It's very cool to see a use of exifr in the wild! Mostly I just hear about things when it stops working...

I wonder whether you really need to cache the exifdata.csv file in each directory. I have done this before when there are so many images that read_exif() takes a long time to read them all, but this only applies when there are several thousand images you are working with. You could try building a list of image files using list.files(".", ".JPG", recursive = TRUE, full.names = TRUE) (or by using c() to combine a few calls to list.files(), and then use the output of read_exif() as your final image list.

If you really do need to write the exifdata.csv file in every directory with images, I would suggest using the tidyverse functions for reading and writing, rather than data.table (there are great reasons to use data.table, but I have a feeling that your data isn't big enough to make it worth it). I'm guessing you have a number of directories you're trying to read, in which case I would suggest something like the following:

#> Using ExifTool version 11.22

main_image_list_file <- "main_exifdata.csv"
directories <- c(".") # you'll need your own directories here

for(target_directory in directories) {
  current_image_list_file <- file.path(target_directory, "exifdata.csv")
  if (!file.exists(current_image_list_file)) {
    image_files <- list.files(path = target_directory, pattern=".png", full.names = TRUE)
    current_image_list <- read_exif(image_files, tags = c("Make"))
    write_csv(current_image_list,  current_image_list_file)
    write_csv(current_image_list, main_image_list_file, append = file.exists(main_image_list_file))

final_image_list <- read_csv(main_image_list_file) %>%
1 Like

Hi @paleolimbot,
Thanks for reviewing the script, as I mentioned, my final aim is storing this information in a database (or Google sheets) and since the images will arrive via a Dropbox shared folder, I am not planning to keep the CSV files along with image files (Dropbox shares will be cleaned regularly, once the images are further processed). So my short term plan is to keep things in a single CSV file for the each of the user who owns the dropbox share. Later I am hoping to switch Google sheets recording, instead of CSV, if I can find a way.
I am not a programmer and my scripting history with R is a week. Therefore I am grateful for your opinions, I am aware that I cannot see future problems. Also, the "make" field is provided for example, in reality, I am storing over 15 EXIF fields.

Sounds like you have lots of experimenting to do before you get there! All of those things are possible in R, although CSVs are certainly a lot easier than databases or google sheets.

1 Like

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.