Replacing spaces in keywords with underscore

Started by brunos, March 10, 2020, 11:26:56 AM

Previous topic - Next topic

brunos

Hi all!

I need an advice, if possible! Have a number of multi-word keywords (XMP subject) in JPGs, but I need to replace spaces between words with underscores (because of Picasa 3 problem with multiword tags - the suggestions stop on first space between words, but work seamlessly if there are underscores instead of spaces).

So. I'm using the following command in ExifFtool 11.91

exiftool "-xmp-dc:subject<${xmp-dc:subject;s/\s/_/g;}" -ext jpg .

Before running it, I have the following situation with test keywords in a test image: I've used Picasa 3 to add three tags to the test image, specifying them at once as

test sx,test dx,aa bb cc

I carefully avoided leading and trailing spaces while typing tags.


BEFORE CHANGE

XMP RAW Photoshop shows the XMP structure as:
         <dc:subject>
            <rdf:Bag>
               <rdf:li>aa bb cc</rdf:li>
               <rdf:li>test dx</rdf:li>
               <rdf:li>test sx</rdf:li>
            </rdf:Bag>
         </dc:subject>

PHOTOSHOP BASIC > SUBJECT field shows the keywords as:
aa bb cc; test dx; test sx

Please notice the leading spaces in keywords 2 and 3.

EXIFTOOL shows as:
C:\Users\Bruno\Desktop\tmpfolder>exiftool -s -keywords img_8209.jpg
Keywords                        : aa bb cc, test dx, test sx

C:\Users\Bruno\Desktop\tmpfolder>exiftool -s -subject img_8209.jpg
Subject                         : aa bb cc, test dx, test sx

Then I run the command. In the tmpfolder there's only one test image:
exiftool "-xmp-dc:subject<${xmp-dc:subject;s/\s/_/g;}" -ext jpg .

AFTER CHANGE
The inner spaces in tags are correctly replaced with the underscores, BUT:

PHOTOSHOP RAW shows the flattened structure, different of the original one: also, leading spaces are added to the keyword 2 and 3
         <dc:subject>
            <rdf:Bag>
               <rdf:li>aa_bb_cc,_test_dx,_test_sx</rdf:li>
            </rdf:Bag>
         </dc:subject>

PHOTOSHOP BASIC > SUBJECT shows quoted string with the leading spaces in keyword 2 and 3 (before the change, the string was not quoted)
"aa_bb_cc,_test_dx,_test_sx"

EXIFTOOL shows keywords unchanged, but this is probably normal as the command updated only the subject tag
C:\Users\Bruno\Desktop\tmpfolder>exiftool -s -keywords img_8209.jpg
Keywords                        : aa bb cc, test dx, test sx

EXIFTOOL shows subject changed, with leading spaces in keyword 2 and 3
C:\Users\Bruno\Desktop\tmpfolder>exiftool -s -subject img_8209.jpg
Subject                         : aa_bb_cc,_test_dx,_test_sx

Now the questions:
1/ am I doing something wrong, causing structure to be flattened and leading spaces to be added, or I should ignore it and still be on the safe side?
2/ should I force the update also the keywords tag (as Picasa obviously does) and not change only the subject tag or it is not important as apparently the data is read from XMP:Subject?

Thank you for your replies!

Kindest regards
Bruno

Phil Harvey

#1
Hi Bruno,

Your post was long, so I didn't read the whole thing, but this command won't work properly:

Quoteexiftool "-xmp-dc:subject<${xmp-dc:subject;s/\s/_/g;}" -ext jpg .

You either need to add a -sep option

exiftool "-xmp-dc:subject<${xmp-dc:subject;s/\s/_/g;}" -sep "##" -ext jpg .

or better yet, use the -api filter option:

exiftool -tagsfromfile @ -xmp-dc:subject -api filter="s/\s/_/g" -ext jpg .

- Phil

Edit: Read FAQ 17 if you want more details about this.
...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 ($).

StarGeek

Also, your keywords don't contain leading spaces as is shown in the raw XMP.  The spaces are part of the interface to show that the keywords are separate. 
"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

brunos


brunos

perfect, just tested it, the XMP structure is not flattened anymore! Thanks!!!

I update also the keywords tag in the same command, with

exiftool -tagsfromfile @ -xmp-dc:subject -api filter="s/\s/_/g" -keywords -ext jpg .

It looks like everything works fine!

Thanks again!