![]() My inelegant code taught me a lot, and watching it rename photos was immensely satisfying. ![]() 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. More importantly, R is about the journey as well as the destination. 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. 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). I don’t regret the time it took me to discover file.rename, for a couple of reasons. WriteJPEG(image = readJPEG(oldfile), target = newfile, quality = 1) Newfile <- paste0( "./photos_to_measure/", new) Before I found file.rename I renamed thousands - thousands! - of photos using a looping method that is more complicated and slower. 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. Whenever you want to rename a bunch of files, file.rename could save you lots of time.įile.rename is a base function. Newfile <- paste0("./photos_to_measure/", new) head(newfile, 5) We then add the file path to the old and new file names. 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. Usually one of the photos has been mislabeled, but this can also catch bigger mistakes such as missing or misplaced fish. If this isn’t TRUE, then we need to go back and work out why. Now we should have two vectors of equal length for the old and new names of photos: # Using indexing to check we're using `match` correctly Photomark <- gsub( ".JPG", "", old) head(photomark, 5) # Taking the markings of these fish so that they can be matched to their unique IDs Step 4: Import the old file names, and create a corresponding vector of new file names. Marking <- marking- 1),] # removing everything not in DTG03 Marking <- marking # first record of DTG03 # take the first instance of DTG03, and every subsequent NA until the next tank record # the problem is Filemaker doesn't export the repeating records for fish_group_ID, so we need to # finding fish in the tank "DTG03", which we are renaming ![]() # "fish_group_ID" "fish_indiv_ID" "marking" Names(marking) <- gsub( ".*:", "", names(marking)) names(marking) # after # "individual_fish main 3::fish_indiv_ID" # renaming columns to remove "::" (which filemaker uses this to distinguish related table fields) Marking <- read_excel( "Fish Records.xlsx", sheet = 1) # note that working directory is already set to our project folder (this is automatic if you use an R Project) ![]() In this example we are only renaming photos from the tank called “DTG03”. 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. 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.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |