Created
July 30, 2016 09:45
-
-
Save eko/6b0caaefeaf82f2aa202804743040292 to your computer and use it in GitHub Desktop.
Photo renamer in Golang based on EXIF format
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
package main | |
import ( | |
"flag" | |
"fmt" | |
"github.com/rwcarlsen/goexif/exif" | |
"os" | |
"path" | |
"path/filepath" | |
"strings" | |
"time" | |
) | |
var counter int = 0 | |
func visit(pathname string, f os.FileInfo, err error) error { | |
openedFile, _ := os.Open(pathname) | |
exifData, err := exif.Decode(openedFile) | |
if err != nil { | |
fmt.Printf("x Unable to load EXIF data for file: %s", pathname) | |
return nil | |
} | |
exifDatetime, _ := exifData.DateTime() | |
newFilename := fmt.Sprintf("%s%s", exifDatetime.Format("20060102030405"), path.Ext(pathname)) | |
newPathname := strings.ToLower(strings.Replace(pathname, f.Name(), newFilename, 1)) | |
os.Rename(pathname, newPathname) | |
fmt.Printf("✓ Renamed to: %s\n", newPathname) | |
counter += 1 | |
return nil | |
} | |
func main() { | |
flag.Parse() | |
directory := flag.Arg(0) | |
startTime := time.Now() | |
filepath.Walk(directory, visit) | |
endTime := time.Now() | |
fmt.Printf("\n\nʕ◔ϖ◔ʔ I successfully renamed %d photos in %s", counter, endTime.Sub(startTime)) | |
} |
Thanks for the script, very useful!
I updated it to support images with the exact same datestamp (like when continuously shooting):
https://gist.github.com/yorch/bba4249e692bf329a9cc705ec824705a
Created a repo for this script:
https://github.com/yorch/photo-renamer
Great, you're welcome :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script renames your photos in the following format (based on EXIF datetime):
YYYYMMDDHHMMSS.jpg
.Simply run the following command: