func procesImage(path string, f os.FileInfo, err error) error { if f.IsDir() { return nil } log.Debugf("Processing %s", path) extension := filepath.Ext(f.Name()) if !isSupportPhotoType(strings.ToLower(extension)) { log.Warnf("%s's file type %s is unsupported", path, extension) return nil } reader := exif.New() err = reader.Open(path) if err != nil { log.Fatal(err) } str := fmt.Sprintf("%s", reader.Tags["Date and Time"]) t := f.ModTime() if len(str) == 0 { log.Warnf("Date and Time EXIF tag missing for %s", path) } else { layout := "2006:01:02 15:04:05" t, err = time.Parse(layout, str) if err != nil { log.Fatal(err) } } newDir := fmt.Sprintf("%s/%4d/%02d/%02d", destPath, t.Year(), t.Month(), t.Day()) err = os.MkdirAll(newDir, 0777) if err != nil { log.Fatal(err) } newFile := fmt.Sprintf("%s/%s", newDir, f.Name()) if mode == "move" { log.Debugf("Moving %s %s", path, newFile) err = os.Rename(path, newFile) } else { if _, err := os.Stat(newFile); err == nil { log.Warnf("Photo %s already exists", newFile) } else { log.Debugf("Copying %s %s", path, newFile) err = copyFile(path, newFile) } } if err != nil { log.Fatal(err) } return nil }
func main() { if len(os.Args) != 2 { fmt.Println("Provide a path") os.Exit(1) } path := expandPath(os.Args[1]) out, err := exec.Command("find", path, "-name", "*.JPG").Output() if err != nil { fmt.Println("Cant find any images") return } files := strings.Split(strings.TrimSpace(string(out)), "\n") exifr := exif.New() for _, path := range files { err = exifr.Open(path) x, _ := strconv.Atoi(exifr.Tags["Pixel X Dimension"]) y, _ := strconv.Atoi(exifr.Tags["Pixel Y Dimension"]) if x/y > 2.0 { fmt.Println(path) } } }
func process(path string) (string, bool) { defer func() { if r := recover(); r != nil { fmt.Fprintln(os.Stderr, path, "panic:", r) } }() file, err := os.Open(path) defer file.Close() if err != nil { fmt.Fprintln(os.Stderr, path, "error", err) } _, _, err = image.Decode(file) if err != nil { return path, false } reader := exif.New() err = reader.Open(path) if err == nil { for key, value := range reader.Tags { fmt.Printf("%s: %s\n", key, value) } } return path, true }
func getExifData(file string) (map[string]string, error) { var err error if *flagExifTool == false || *flagTryExifTool == true { ex := exif.New() err = ex.Open(file) if err == nil { return ex.Tags, nil } } if *flagExifTool == true || *flagTryExifTool == true { cmd := exec.Command("exiftool", file) var out bytes.Buffer cmd.Stdout = &out if err := cmd.Run(); err != nil { return nil, err } tags := make(map[string]string) data := strings.Trim(out.String(), " \r\n") lines := strings.Split(data, "\n") var k, v string for _, line := range lines { k = strings.TrimSpace(line[0:32]) v = strings.TrimSpace(line[33:]) tags[k] = v } return tags, nil } return nil, ErrUnknownFile }