Created
March 17, 2011 19:23
-
-
Save batmat/874955 to your computer and use it in GitHub Desktop.
Small Groovy script to process a pictures directory and rename them using "Date/Time original" exif field.
Useful for example if you want your photos automatically sorted by their names, as if it was sorted by the photo date. My case: useful in the Ph
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Using metadata-extractor-2.4.0-beta-1.jar from http://www.drewnoakes.com/drewnoakes.com/code/exif/sampleUsage.html | |
import com.drew.metadata.Metadata | |
import com.drew.imaging.jpeg.JpegMetadataReader | |
import com.drew.metadata.Directory | |
import com.drew.metadata.Tag | |
import java.text.*; | |
def dirToProcess = new File(".") | |
assert dirToProcess.exists() | |
DateFormat df = new SimpleDateFormat('yyyy-MM-dd_HHmmss') | |
// Note : beware that if two pics have the same time (same second) there's gonna be a problem. | |
// backup your photos before running !!! | |
dirToProcess.eachFile {jpegFile -> | |
if( jpegFile.isFile() && jpegFile.getName().toLowerCase().endsWith(".jpg") ) | |
{ | |
Metadata metadata = JpegMetadataReader.readMetadata(jpegFile); | |
Directory exifDirectory = metadata.getDirectory(Class.forName("com.drew.metadata.exif.ExifDirectory")); | |
// 36867 => Date/Time Original | |
int tagType = 36867 | |
if(exifDirectory.containsTag(tagType)) | |
{ | |
Date value = exifDirectory.getDate(36867); | |
def newName = df.format(value)+".jpg" | |
if(jpegFile.name != newName) | |
{ | |
println("$jpegFile.name renamed to $newName"); | |
jpegFile.renameTo(new File(newName)) | |
} | |
} | |
else | |
{ | |
println("No tagType 'Date/Time Original' for $jpegFile. Cannot rename. You'll likely have to do it by hand :-/") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment