Setting the Same Tag in Multiple Images to Unique Values

Started by dbqandersons, October 31, 2024, 12:20:47 PM

Previous topic - Next topic

dbqandersons

Hi Everyone,

Long time listener, first time caller.

I have a couple of scripts (Powershell for Windows and bash for MacOS) that assign a random 32 character string to the ImageUniqueID tag in a directory (and sub-directories) full of images.  Images that already have a valid value (32 characters using a-f and 0-9) assigned to ImageUniqueID are ignored, but newly added images that have no ImageUniqueID or a camera-assigned value are assigned a randomly generated 32 character value.

I'm looking to increase my scripts' efficiency by using a single exiftool call for all of the images being processed (if possible).  Assigning the same ImageUniqueID tag value to all of the images would be a piece of cake with a -recurse option; I actually use the -recurse option earlier in my scripts to reset the data for images edited on Samsung phones.  However, I can't wrap my head around how to attach a unique ImageUniqueID value for each image other than the way I'm doing it now.

This is an excerpt from the MacOS (bash) version. CURRENT_IMAGE_BASE_DIR is a top level directory selected by the user. IMAGES_TO_PROCESS is the output from a simple find command against that directory.

exiftool -m -q -q -recurse -overwrite_original_in_place -if '$otherimagestart and $make eq "samsung"' -exif:all= -tagsfromfile @ -all:all -unsafe -ext jpg -ext jpeg -ext gif -ext png ${CURRENT_IMAGE_BASE_DIR}

for IMAGE in $IMAGES_TO_PROCESS
do
    # Generate a new ID
    NEW_IDSTRING=`openssl rand -hex 16`
    # Add the new ID to the image's metadata only if another properly formatted ID doesn't exist
    exiftool -m -q -q -overwrite_original_in_place -if '$imageuniqueid !~ /^[a-f0-9]{32}$$/' -imageuniqueid=${NEW_IDSTRING} ${IMAGE}
done


While this does the job, it does the job slowly (especially on the Powershell/Windows side).  I'd like to think there's a way to speed this up with the -stay_open option or something like that so exiftool doesn't get called once per image, but I can't find it.

Any help would be greatly appreciated.

Thanks,

Bill

StarGeek

You could use exiftool's built in unique ID

exiftool -if 'not $imageuniqueid' '-imageuniqueid<NewGUID' /path/to/files/

You can see the details of the NewGUID under the notes section of its listing on the Extra Tags page.

If you want to continue to use 32 random characters, then maybe this. Filename is just used as a container to generate the random characters
'-imageuniqueid<${Filename;$_=join('', map { (0..9, 'a'..'f')[rand 16] } 1..32)}'
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

dbqandersons

For me, I think the newguid will be the way to go. Guess I should have dug further into the docs.

If I drop the dashes from the output with the -n flag, the new IDs will look on the surface just like ones I've already made for existing images, satisfying my occasionally OCD brain.

I'll work this into my scripts and I feel I'll see a pretty hefty performance uptick.

Thanks @StarGeek!


dbqandersons

Running through 2450 images has dropped from over 1/2 hour down to about 3 1/2 minutes.

Thanks again, @StarGeek!

Cheers,

Bill

dbqandersons

In case anyone else is looking to do the insanity I'm doing, here's the command I landed on:

exiftool -if '$otherimagestart and $make eq "samsung"' -exif:all= -tagsfromfile @ -all:all -unsafe -execute -if '$imageuniqueid !~ /^[A-F0-9]{32}$$/' '-imageuniqueid<NewGUID' -common_args -m -q -q -progress -n -recurse -ext jpg -ext jpeg -ext gif -ext png ${CURRENT_IMAGE_BASE_DIR}

As mentioned in my previous reply, this command runs in almost literally 1/10th of the time of my old stuff.

Cheers!

Bill

StarGeek

"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype