I-DEEL: Inter-Disciplinary Ecology and Evolution Lab
  • Home
  • People
  • Research
  • Publications
  • Open Science
    • Registrations
    • Registered Reports
    • Published Protocols
    • Preprints
    • EDI
    • Other
  • Opportunities
  • Links
  • Blog

How to Rename Photos in R

10/11/2018

0 Comments

 
by Rose O'Dea

Measuring fish. In my short research career this is the activity I’ve done the most. ​Whether measuring mosquitofish or zebrafish, the method has been:
  1. Photograph fish next to a scale
  2. Rename photo with the ID of the fish and the date
  3. Measure photo using ImageJ

Here I share my tricks to renaming photos using R.
​
Step 1: Put a single image of the fish you want to rename in a folder.We usually take two photos of each fish and choose the best one (before first copying all the photos to a back-up location, just in case).

Step 2: ​Make the order of fish, or their names, correspond to their identity. Sometimes the fish are photographed in a pre-determined order, in which case fish names are simply assigned sequentially. More often we photograph all fish from a single tank in a random order, and then use their unique coloured markings to assign their names. In this case we needed to rename the photos to match the marking of each fish, and these file names could then be linked to the name of the fish stored in our records.
Picture
Step 3: Generate a file with the name of each fishTo link the marking of the fish to their records, we save a file that contains three pieces of information: the tank ID, the fish ID, and the marking of each fish. The file is exported from our master records, and requires a bit of manipulation so that we only have the records of the marked fish in our particular tanks. In this example we are only renaming photos from the tank called “DTG03”.

# note that working directory is already set to our project folder (this is automatic if you use an R Project)
# importing excel document
library(readxl)
marking <- read_excel("Fish Records.xlsx", sheet = 1)
# renaming columns to remove "::" (which filemaker uses this to distinguish related table fields)
names(marking) # before

## [1] "fish_group_ID"
## [2] "individual_fish main 3::fish_indiv_ID"
## [3] "individual_fish main 3::marking"

names(marking) <- gsub(".*:", "", names(marking)) names(marking) # after
## [1] "fish_group_ID" "fish_indiv_ID" "marking"

# finding fish in the tank "DTG03", which we are renaming
# the problem is Filemaker doesn't export the repeating records for fish_group_ID, so we need to
# take the first instance of DTG03, and every subsequent NA until the next tank record

marking <- marking[which((marking$fish_group_ID == "DTG03") == T):nrow(marking),] # first record of DTG03
marking <- marking[1:(which(!is.na(marking$fish_group_ID)==T)[2]-1),]
# removing everything not in DTG03

Step 4: Import the old file names, and create a corresponding vector of new file names.

old <- list.files("./original_DTG03_15_03_17") head(old, 5)

## character(0)

# Taking the markings of these fish so that they can be matched to their unique IDs
photomark <- gsub(".JPG", "", old) head(photomark, 5)

## character(0)

# Using indexing to check we're using `match` correctly
marking$marking[match(photomark, marking$marking)] == photomark # all match

## logical(0)

# saving JPEG names of new photos
new <- paste0("15_03_17_", marking$fish_indiv_ID[match(photomark, marking$marking)], ".JPG")
head(new, 5)

## [1] "15_03_17_.JPG"

Now we should have two vectors of equal length for the old and new names of photos:

length(old) == length(new)

## [1] FALSE


If this isn’t TRUE, then we need to go back and work out why. Usually one of the photos has been mislabeled, but this can also catch bigger mistakes such as missing or misplaced fish.


Step 5: Rename photos.First we will create an empty destination folder where we want the photos to go: in this case it’s called photos_to_measure. We then add the file path to the old and new file names.

oldfile <- paste0("./original_DTG03_15_03_17/", old) head(oldfile, 5)

## [1] "./original_DTG03_15_03_17/"

newfile <- paste0("./photos_to_measure/", new) head(newfile, 5)

## [1] "./photos_to_measure/15_03_17_.JPG"

file.rename(from = oldfile, to = newfile)

​## [1] TRUE
Picture
​Note that this function file.rename will transfer the photos from the old folder into the new folder, leaving nothing behind (i.e., something’s gone wrong if the old folder isn’t empty):
Picture

​Not just for fish photos!

Whenever you want to rename a bunch of files, file.rename could save you lots of time.


R is found in tRial and eRRoR

file.rename is a base function. Whenever I discover that something I’ve been doing in R for ages can be more easily done using a simple base function, I’m alternately amazed and infuriated. Before I found file.rename I renamed thousands - thousands! - of photos using a looping method that is more complicated and slower.

library(jpeg)
oldfile <- paste0("./original_DTG03_15_03_17 copy/", old)
newfile <- paste0("./photos_to_measure/", new)
for(i in 1:length(newfile)){
    writeJPEG(image = readJPEG(oldfile[i]), target = newfile[i], quality = 1)
}


I don’t regret the time it took me to discover file.rename, for a couple of reasons. The loop allows you to specify the file size of the renamed image by specifying quality on a scale form 0 to 1 (see ?writeJPEG). In our database we sometimes store photos of important fish, and to keep the file size lower it’s useful to export these photos with quality = 0.3. More importantly, R is about the journey as well as the destination. That small piece of code I wrote in 2016 - which took at least an hour of trial and error! - was the first loop I ever wrote and used regularly. My inelegant code taught me a lot, and watching it rename photos was immensely satisfying. Still, we need to pick our R battles, and I hope file.rename will leave you more time for your own.
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    Author

    Posts are written by our group members and guests.

    Archives

    February 2023
    January 2023
    December 2022
    November 2022
    October 2022
    September 2022
    August 2022
    July 2022
    June 2022
    May 2022
    April 2022
    March 2022
    February 2022
    January 2022
    December 2021
    November 2021
    October 2021
    September 2021
    August 2021
    July 2021
    June 2021
    April 2021
    March 2021
    February 2021
    January 2021
    December 2020
    November 2020
    August 2020
    July 2020
    June 2020
    April 2020
    December 2019
    November 2019
    October 2019
    September 2019
    June 2019
    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    July 2018
    June 2018
    May 2018
    March 2018
    January 2018
    December 2017
    October 2017
    September 2017
    July 2017
    June 2017
    May 2017
    April 2017
    March 2017
    January 2017
    October 2016
    August 2016
    July 2016
    June 2016
    May 2016
    March 2016

    Categories

    All

    RSS Feed

HOME
PEOPLE
RESEARCH
PUBLICATIONS
OPEN SCIENCE
OPPORTUNITIES
LINKS
BLOG

Created by Losia Lagisz, last modified on June 24, 2015