Copy images into folders for each Keyword in image library

Started by Bones, July 27, 2024, 11:27:35 PM

Previous topic - Next topic

Bones

I want to create a folder for a each of the Keywords (about 50) in the images in my library (~10,000)
I have looked across the forum, had a go  - got to something that can be lived with  - however suspect substantial optimisation is needed

Two issues i have got to
  • Cant get my keywords into a list to use a listitem as suggested in creating image copy for each keyword into subfolder - however ideally i would prefer just to have a folder for EVERY keyword found against an image rather then create the list - but could not see any way to achieve that - so list is good
  • Get hardlink to work to save diskspace... - also described in above link

The  commands i have tried - Windows - and results are below - any thoughts would be appreciated
When i have it working i will move it to a Bash script. The use case is to create Albums for Apple TV that supports 1 folder for Library with all sub folders being considered as "Albums"...



Basic commands to copy images that i would like to be list based
C:\temp\Srce>exiftool -r -keywords c:\temp\Srce
======== c:/temp/Srce/One/B_025.jpg
Keywords                        : D-All Good Pics
======== c:/temp/Srce/One/fishing boat.jpg
Keywords                        : D-All Good Pics
======== c:/temp/Srce/Two/NCEB000230.jpg
Keywords                        : D-All Good Pics, P-2002-04 Chris Sport
    3 directories scanned
    3 image files read

C:\temp\Srce>exiftool -r -keywords c:\temp\Dest
    1 directories scanned
    0 image files read

C:\temp\Srce>exiftool -r -if "$keywords =~ /D-All Good Pics/" -directory="c:\temp\Dest\D-All Good Pics" -o dummy/ c:\temp\Srce
    3 directories scanned
    1 directories created
    3 image files copied

C:\temp\Srce>exiftool -r -if "$keywords =~ /P-2002-04 Chris Sport/" -directory="c:\temp\Dest\P-2002-04 Chris Sport" -o dummy/ c:\temp\Srce
    3 directories scanned
    1 directories created
    2 files failed condition
    1 image files copied
    0 image files read

C:\temp\Srce>exiftool -r -keywords c:\temp\Dest
======== c:/temp/Dest/D-All Good Pics/B_025.jpg
Keywords                        : D-All Good Pics
======== c:/temp/Dest/D-All Good Pics/fishing boat.jpg
Keywords                        : D-All Good Pics
======== c:/temp/Dest/D-All Good Pics/NCEB000230.jpg
Keywords                        : D-All Good Pics, P-2002-04 Chris Sport
======== c:/temp/Dest/P-2002-04 Chris Sport/NCEB000230.jpg
Keywords                        : D-All Good Pics, P-2002-04 Chris Sport
    3 directories scanned
    4 image files read


And using hardlink - where the folders and files were not created as wanted

C:\temp2\Srce>exiftool -r -keywords c:\temp2
======== c:/temp2/Srce/One/B_025.jpg
Keywords                        : D-All Good Pics
======== c:/temp2/Srce/One/fishing boat.jpg
Keywords                        : D-All Good Pics
======== c:/temp2/Srce/Two/NCEB000230.jpg
Keywords                        : D-All Good Pics, P-2002-04 Chris Sport
    4 directories scanned
    3 image files read

C:\temp2>exiftool -r -if "$keywords =~ /D-All Good Pics/" -hardlink="c:\temp2\Dest\D-All Good Pics" -o dummy/ c:\temp2\Srce
Warning: File 'c:/temp2/Dest/D-All Good Pics' already exists - c:/temp2/Srce/One/fishing boat.jpg
Warning: File 'c:/temp2/Dest/D-All Good Pics' already exists - c:/temp2/Srce/Two/NCEB000230.jpg
    3 directories scanned
    1 directories created
    3 image files copied
    1 hard links created
    2 hard links could not be created

C:\temp2>exiftool -r -if "$keywords =~ /P-2002-04 Chris Sport/" -hardlink="c:\temp2\Dest\P-2002-04 Chris Sport" -o dummy/ c:\temp2\Srce
Error: 'dummy/NCEB000230.jpg' already exists - c:/temp2/Srce/Two/NCEB000230.jpg
    3 directories scanned
    2 files failed condition
    0 image files updated
    1 files weren't updated due to errors
    0 image files read

Phil Harvey

From what I understand, the thread you referenced should be exactly what you wanted.

However, the HardLink is a file name, not a directory name.  So you need to add back the /%f.%e like the command in the linked post.  And you don't need the -o option when writing HardLink (because this creates a hard link and doesn't move the file), but when writing Directory you need -o to copy the file instead of moving it.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Bones

#2
Thanks - that worked - as i am sure you would have anticipated  :)
I moved it to bash script (to run on the NAS) reading the "keywords" i want albums for, from a text file.

Suspect this is now a bash script question and i could not find a suitable example - so any help appreciated

I have atempted several alternatives to build the Exiftools commandline string - with no real success - the gist of the  intent is seen in
#     - this is what was intended - ie albumused for keywords "if" AND used for part of desitnation folder path
#    exiftool -r -if '$keywords =~ $album' -hardlink='/volume1/BU/Temp/Dest2/$album/%f.%e' /volume1/BU/Temp/Srce

attempts is in the example below - with the results below that


Text file 'Album List test"
D-All Good Pics
P-2002-04 Chris Sport

 

bash script file

#Albums_Links_1.sh
ls /volume1/BU/Temp/Srce
while read -r album; do
#     - this is what was intended - ie albumused for keywords "if" AND used for part of desitnation folder path
# exiftool -r -if '$keywords =~ $album' -hardlink='/volume1/BU/Temp/Dest2/$album/%f.%e' /volume1/BU/Temp/Srce
    echo " Album Name -" "$album"
    arg1="  /D-All Good Pics/"
    arg2="-hardlink='/volume1/BU/Temp/Dest80/""$album""/%f.%e' /volume1/BU/Temp/Srce"
    echo " arg1 =""$arg1" " arg2 =""$arg2"
    echo "    arg1 alternatives"
    exiftool -r -if '$keywords =~ "${arg1}"' -hardlink='/volume1/BU/Temp/Dest81/D-All Good Pics/%f.%e' /volume1/BU/Temp/Srce
    exiftool -r -if '$keywords =~ "\${arg1}"' -hardlink='/volume1/BU/Temp/Dest82/D-All Good Pics/%f.%e' /volume1/BU/Temp/Srce
    echo "    arg2 alternatives"
    exiftool -r -if '$keywords =~ /D-All Good Pics/' "${arg2}"
    exiftool -r -if '$keywords =~ /D-All Good Pics/' "\${arg2}"
    echo " album alternatives"
    exiftool -r -if '$keywords =~ /D-All Good Pics/' -hardlink='/volume1/BU/Temp/Dest83/$album/%f.%e' /volume1/BU/Temp/Srce
    exiftool -r -if '$keywords =~ /D-All Good Pics/' -hardlink='/volume1/BU/Temp/Dest84/"\${album}"/%f.%e' /volume1/BU/Temp/Srce
    echo " expected result"
    exiftool -r -if '$keywords =~ /D-All Good Pics/' -hardlink='/volume1/BU/Temp/Dest86/D-All Good Pics/%f.%e' /volume1/BU/Temp/Srce
done < "Album List test.txt"
ls -R /volume1/BU/Temp/

Result/Output
SSH@NAS49:/volume3/Media/AllOurMedia/0 Admin etc of Pictures/Dev$ bash Albums_Links_1.sh
One  Two
 Album Name - D-All Good Pics
 arg1 =  /D-All Good Pics/  arg2 =-hardlink='/volume1/BU/Temp/Dest80/D-All Good Pics/%f.%e' /volume1/BU/Temp/Srce
        arg1 alternatives
    3 directories scanned
    3 files failed condition
    0 image files read
    3 directories scanned
    3 files failed condition
    0 image files read
        arg2 alternatives
No file specified
Error: File not found - ${arg2}
 album alternatives
    3 directories scanned
    0 image files updated
    3 image files unchanged
    3 hard links created
    3 directories scanned
    0 image files updated
    3 image files unchanged
    3 hard links created
 expected result
    3 directories scanned
    0 image files updated
    3 image files unchanged
    3 hard links created
/volume1/BU/Temp/:
Dest83  Dest84  Dest86  Srce

/volume1/BU/Temp/Dest83:
'$album'

'/volume1/BU/Temp/Dest83/$album':
 B_025.jpg  'fishing boat.jpg'   NCEB000230.jpg

/volume1/BU/Temp/Dest84:
'"'

'/volume1/BU/Temp/Dest84/"':
'${album}"'

'/volume1/BU/Temp/Dest84/"/${album}"':
 B_025.jpg  'fishing boat.jpg'   NCEB000230.jpg

/volume1/BU/Temp/Dest86:
'D-All Good Pics'

'/volume1/BU/Temp/Dest86/D-All Good Pics':
 B_025.jpg  'fishing boat.jpg'   NCEB000230.jpg

/volume1/BU/Temp/Srce:
One  Two

/volume1/BU/Temp/Srce/One:
 B_025.jpg  'fishing boat.jpg'

/volume1/BU/Temp/Srce/Two:
NCEB000230.jpg

Bones

Got it solved - takes about 2 hrs to run for 30 Albums from a folder of 200 ish images - 5 mins is per pass - i am happy.

#!/bin/bash
#Albums_Links.sh - 240810 1738
Source="/volume3/Media/AllOurMedia/1 Our Pictures/Cataloged ( 3-5)"
Destination="/volume3/Media/AllOurMedia/4 AppleTV Albums/"
while read -r album; do
date=`date +%T" "%d%h%Y`
date1=`date +%Y%m%d"_"%H%M%S`
echo "@ ${date} Commence creating album for $album "
KeywordCheck="\$keywords =~ /$album/"
exiftool -r -if "$KeywordCheck" -hardlink="${Destination}""$album"'/%f.%e' "$Source"
done < "Album List.txt"