Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract the date of creation of a photo using R?

Tags:

r

I have more than 800 pictures where I need to extract the date of creation. But if I use the file.mtime function, it's not working. It's just the date that I modified it. The funny thing is that iPhoto (a program on mac to manage photos) is able to detect the date of creation properly.

This is the code I was using.

my.path = "~/Desktop/cool_path_here"
vec.jpg = list.files(path = my.path)
lapply(paste(my.path,vec.jpg,sep = "/"),FUN =  file.mtime)

Is there a function that could extract the date of creation of the photo and not the one that is shown in the "get info" menu on mac.

e.g.: (yesterday was May 18th)

enter image description here

and in iPhoto (found March 16th):

enter image description here

R is finding:

2017-05-19 15:08:29
like image 553
M. Beausoleil Avatar asked Oct 19 '25 22:10

M. Beausoleil


2 Answers

Use the exif package to extract the metadata:

library(exif)
read_exif(paste(my.path,vec.jpg[1], sep="/"))$origin_timestamp

is returning "2017:03:16 08:47:48" which is the what I was looking for!

like image 152
M. Beausoleil Avatar answered Oct 22 '25 13:10

M. Beausoleil


As per ?file.info, mtime is the modification time, ctime is the "last status change" time, and atime is the last "access" time. According to here, the POSIX standard (which Mac OS follows) does not include a creation time among the standard properties of a file.

file.info(my.path)$atime may be better. That didn't change in this test:

DF = data.frame(a = 1:10, b = 10:1)
tmp = tempfile()
write.table(DF, tmp)
Sys.time()
# [1] "2017-05-19 15:03:46 EDT"
file.info(tmp)[ , c('mtime', 'ctime', 'atime')]
#                                                mtime               ctime               atime
# /tmp/Rtmpzpi8p6/file29e932565c62 2017-05-19 15:03:46 2017-05-19 15:03:46 2017-05-19 15:03:46
DF$c = 11:20
Sys.sleep(10)
write.table(DF, tmp)
file.info(tmp)[ , c('mtime', 'ctime', 'atime')]
#                                                mtime               ctime               atime
# /tmp/Rtmpzpi8p6/file29e932565c62 2017-05-19 15:03:56 2017-05-19 15:03:56 2017-05-19 15:03:4

BTW, use the full.names argument to list.files instead of using paste and sep='/'.

like image 22
MichaelChirico Avatar answered Oct 22 '25 11:10

MichaelChirico