Skip to content

Instantly share code, notes, and snippets.

@pecard
Last active May 16, 2020 00:07
Show Gist options
  • Save pecard/23e9cd5fdf4b7cfbbc8c9720c945cda5 to your computer and use it in GitHub Desktop.
Save pecard/23e9cd5fdf4b7cfbbc8c9720c945cda5 to your computer and use it in GitHub Desktop.
Get Metadata (EXIF) from Smartphone photos
# Paulo E. Cardoso 2020-05-15
# Read EXIF Metadata from Photos obtained with SmarthPhone
# Initial instructions ----------------------------------
# Download ExifTool executable from https://exiftool.org/ and
# unzip the exiftool(-k).exe to Rstudio Project root folder
# Download your fotos to (this case) data-raw folder
# Assuming .jpg image files
# Install packages ----------------------------------------
kpacks <- c(
'here',
'lubridate',
'tidyverse',
'leaflet',
'readr',
'leafpop' # No need to install from source
)
new.packs <-
kpacks[!(kpacks %in% installed.packages()[, "Package"])]
if (length(new.packs))
install.packages(new.packs)
lpa <- lapply(kpacks, require, character.only = T)
lpa
stopifnot(any(unlist(lpa) == TRUE))
remove(kpacks, new.packs, lpa)
# List images from a folder (data-raw in this case) -------
files <- list.files(here::here('data-raw'),
pattern = '*.jpg', full.names = T)
# Read EXIF metadata using exiftools ----------------------
exifs <- list()
for(i in files){
ifile <- read_delim(system(
paste("exiftool(-k) -n -csv",
i, collapse=" "),
intern=TRUE
), delim = ',') %>%
select(FileName, Date=DateTimeOriginal, Long=GPSLongitude,
Lat=GPSLatitude, Alt=GPSAltitude)
exifs[[i]] <- ifile
}
exif_files <- bind_rows(exifs)
exif_files$Date <- ymd_hms(exif_files$Date)
images <- files
# Map files -----------------------------------------------
leaflet() %>%
addProviderTiles("Esri.WorldImagery") %>%
addMarkers(~ Long, ~ Lat,
label = ~as.character(FileName),
data = exif_files, group = 'exif_files') %>%
addPopupImages(images, group = 'exif_files', width = 80, height = 80)
# Export metadata to a file -------------------------------
write.table(exif_files, file = here::here('output', 'exif_files.txt'),
sep = '\t', row.names = F)
# Example of all available data ---------------------------
metadata <- read_delim(system("exiftool(-k) -n -csv IMG_5782.jpg",
intern=TRUE), delim = ',')
sort(names(metadata))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment