reading TAG from file without newline character?

Started by reisschuessel, February 23, 2015, 09:48:49 AM

Previous topic - Next topic

reisschuessel

Hi,

im putting together a Lightroom-Plugin using zbarimg for reading text embedded in a qr-code and then using exiftool for writing to files.

The string output by zbarimg is written to a tempfile and contains one line (Lensname). The actual commandline looks like this:
zbarimg -Sqrcode.enable -q /somepath/image.jpg | sed -e \'s/QR-Code://\' > /somepath/QR-Code_String_extracted_temp.txt

Then exiftool is executed with this command:
exiftool -m -P -q -overwrite_original_in_place '-Lens<=/somepath/QR-Code_String_extracted_temp.txt' /somepath/image.jpg

The problem is that this tempfile includes a newline character at the end of its only line of text. This is then read by exiftool and written in the tag.
(This character is not generated by the sed-command, but is also the if sed is not used.)
When displaying the exif by exiftool it shows a dot (.) at the end of the corresponding tags - when viewed in Lightroom, it looks as if there is actually a linebreak.

One solution on the mac is pipeing the string from zbarimg through tr -d \'\\n\' which removes the newline.
But as i plan to give the plugin to friends with a windows machine this is no alternative (as far as i know there  is no tr equiv. on windows machines).
I'd like to keep the requirements as little as possible. ;)

Is there anything i'm not understanding correctly?
Or is there a way to strip this trailing newline chartacter by using exiftool?

Thanks!
Niels

500px: 500px.com/oejeblikket

Phil Harvey

ExifTool won't parse your input, but once it has a tag you could use the advanced formatting feature to remove the newline.  This isn't pretty, but it should do what you want:

exiftool '-Lens<=/somepath/QR-Code_String_extracted_temp.txt' -execute '-lens<${lens;tr/\n//d}' -common_args -m -P -q -overwrite_original_in_place /somepath/image.jpg

This effectively executes two commands.  The second one reformats Lens to remove the newline.

- 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 ($).

reisschuessel

#2
Thanks Phil!
Works nicely.  :)

May i ask another question?
If i want to write some additional tags like LensType and LensModel in the same step, how would the command have to be like?

-Niels

Niels

500px: 500px.com/oejeblikket

Phil Harvey

It depends on how you write them.  If it is a simple assignment, then you can add the -TAG=VALUE assignments to either command.  If you are reading from an external file, then you may have to pull the same stunt as with the Lens tag.  But if that is the case, I would suggest an external file in some other format (like JSON) to allow all of the tags to be stored in the same file (this would also be a different solution to the newline problem).

- 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 ($).

reisschuessel

My idea was to fill additional Lenstags with the same value.

exiftool -m -P -q -overwrite_original_in_place '-Lens<=/somepath/tempfile.txt' 'LensModel<=/somepath/tempfile.txt' LensType<=/somepath/tempfile.txt' /somepath/image.jpg

If i understand correctly this would mean that your suggested command has to be executed for every tag written and therefore multiplying the processing time?

I tried this line but that does not to the job.  :-[
exiftool -m -P -q -overwrite_original_in_place '-Lens<=/somepath/tempfile.txt' 'LensModel<=/somepath/tempfile.txt' LensType<=/somepath/tempfile.txt' -execute '-lens<${lens;tr/\n//d}' -common_args -m -P -q -overwrite_original_in_place /somepath/image.jpg

My Plugin reads in the filelist from Lightroom and then executes the exiftool command for every file inside a loop in LUA.
I undestand that this might not be very efficient but keeps things understandable for me. I'm not that keen in scripting...

Basically the block now looks like this:

for i, photo in ipairs(cat_photos) do
            local file = photo.path
            LrTasks.execute("'" ..Mac_exiftoolPath.. "'/exiftool '-Lens<='" ..Mac_exifTempFile.. "'' -execute '-lens<${lens;tr/\n//d}' -common_args -m -P -q -overwrite_original_in_place '" ..file.."'")
        end




Niels

500px: 500px.com/oejeblikket

Phil Harvey

Hi Niels,

You don't need to execute a different command for each tag.  However, there is a complication writing LensType because it is a lookup table, and the look up will fail if the value ends with a newline.  To get around this, you could copy the value from another tag in the 2nd half of the command.  In fact, you could do that for LensModel too.  So your command could look like this:

exiftool '-Lens<=/somepath/QR-Code_String_extracted_temp.txt' -execute '-lens<${lens;tr/\n//d}' '-lenstype<${lens;tr/\n//d}' '-lensmodel<${lens;tr/\n//d}' -common_args -m -P -q -overwrite_original_in_place /somepath/image.jpg

Why it doesn't work in your script may be related to the "\n" in your string.  Do you need to escape the backslash and use "\\n" here?  Does it work when you execute it from the command line?

- 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 ($).

reisschuessel

Great!

Now i got how this command works and modified it a bit for the windows part. The Mac part worked as you described it.
As the tempfile ends with an CRLF on Windows i used the equivalent to '-lens<${lens;tr/"\r\n"//d}' which translates to quite an ugly \"-lens<${lens;tr/\"\\r\\n\"//d}\" with all the necessary escaping. ;)

You are right about escaping the "\n" but to my surprise it does indeed work without on the mac side.
Strange enough it works too if i escape it...
But that's something i will and have to sort out by myself.

For now i am very happy with the help from you!

-Niels
Niels

500px: 500px.com/oejeblikket