I recently got in contact with Phil Harvey who made ExifTool, this tool specifically deals with metadata formats like JPEG's IPTC and others. For me I am looking to streamline my workflow that presently has me copying text from a TXT file into a photo editing application. Instead I would like to harness its built-in ability to import corresponding XMP side-car data files to automatically do this on import and save me the time and RSI!

Comment: This technique works best with mandarin oranges, but just for good measure I tried it out on a Valencia orange as well. Simply lop off the ends, make a small incision in the side and carefully open to reveal easy-to-eat orange segments. (Or as I prefer to call it, an orange caterpillar.)

My requirements are simple, I need to convert the existing TXT files into the structured format of the XMP format, however there is a variable in that sometimes a "Comment" is not present, only the "Title" and "URL" values. I am looking to translate the following into IPTC fields: "SubjectReference", "Headline" and "Description", all as seen in the example above.

According to Mr. Harvey I would need to parse the TXT file in Perl, and call "SetNewValue()" with appropriate tag/value pairs, then call "WriteInfo()" , an example from his website:

However this seems like it would always set the "Author" to a fixed value, instead I would need "Phil Harvey" to be the text after "Title" for example, but only up until the next value to look out for e.g. "URL".

Maybe I don't understand you correctly, but I assume that, somewhere in your script you are already reading the input exif data. Then you can just take the first line after the "Title:" tag and then set that as the author.

The examples on the ExifTool site are very rigid, while I am looking for something which is more delicate (hence why I'd like to move away from an unstructured TXT file and to XMP) and believe the way to achieve this is to get Perl to look for next beginning on a new line after "Title:" and up until it comes across "URL:" on it's own line.

If I have understood correctly, your first step is to parse the txt file. It is well structured, in that it follows the pattern key \n value \n linebreak, therefore can be parsed with ease. There are numerous ways to achieve this, here is an example of parsing into a hash:

Comment: This technique works best with mandarin oranges, but just for good measure I tried it out on a Valencia orange as well. Simply lop off the ends, make a small incision in the side and carefully open to reveal easy-to-eat orange segments. (Or as I prefer to call it, an orange caterpillar.)

Output:

Code

$VAR1 = { 'Comment' => 'This technique works best with mandarin oranges, but just for good measure I tried it out on a Valencia orange as well. Simply lop off the ends, make a small incision in the side and carefully open to reveal easy-to-eat orange segments. (Or as I prefer to call it, an orange caterpillar.)', 'URL' => 'http://www.tablespoon.com/posts/youve-been-peeling-an-orange-wrong/14800c66-27f2-4715-9936-d299ec7bfa9f?utm_source=zergnet.com&utm_medium=referral&utm_campaign=zergnet_280460', 'Title' => 'You\'ve Been Peeling An Orange Wrong - Tablespoon' };

Great, thanks, it's very good of you to take the time to do this. I saved the code as an ".args" file, executing incorrectly it seems: "exiftool DIR exifgif.args" which produced an error: "Unknown file type" but it read one file, there was only one .txt image in the folder anyway.

I am looking to batch files in a directory also, everything in one pass.

I don't know enough about exiftools but from briefly reading the documentation I don't think an .args file is used in the manner you tried. It appears you are trying to use the command line interface, I am following on from the advice you were provided by Mr. Harvey, write a custom Perl script that uses Image::ExifTool.

Quote

According to Mr. Harvey I would need to parse the TXT file in Perl, and call "SetNewValue()" with appropriate tag/value pairs, then call "WriteInfo()" , an example from his website:

However this seems like it would always set the "Author" to a fixed value, instead I would need "Phil Harvey" to be the text after "Title" for example, but only up until the next value to look out for e.g. "URL".

Once you have parsed a txt file into a hash, you can use the code you were provided to set new values then write info:

With regards to batches, we don't know enough about your file system structure i.e. how txt files map to image files, therefore cannot provide complete advice / code. In unix I would probably make use of pathname expansion and run the command perl script.pl /path/to/dir/*, then @ARGV will contain all the batch filepaths, which can be iterated over. This approach gives you more flexibility over each batch, rather than having to code this flexibility in yourself:

Thank you again for your efforts. As I interpret from your code "my $filepath = '/path/to/file.txt';" points to a specific file. Mentioned at the end of your post was my workflow, I am on OS X and the files I get are paired with TXT files, so a file called "abc.gif" (of which that format cannot store IPTC metadata) is accompanied by a text file with the name "abc.gif.txt" which makes it really easy to associate the metadata with the correlating image file.

Typically I'll have a folder of 20 or so files, so 40 then, and wanted to just type a command into Terminal (OS X) and then have ExifTool spit out XMP files with the data contained in the TXT file reorganised so when I import the image files (renamed as e.g. "abc.gif.xmp") the data will simply import at the same time.

It's a unique situation I know, not many people are going to be in this situation, but that's my present workflow.

Code

$exifTool->SetNewValue(Author => $hash{Title}); $exifTool->WriteInfo('image.jpg','modified_image.jpg'); # 3rd arg should be 'XMP' if you want to create an XMP data file.

I think that your code might apply this one example I have given to many files, rather than to interpret each file differently, although I can't be sure?

I have a link to a small group of samples you will see what I mean, workflow wise: http://cl.ly/2c1I0I432B03

I was confused about the command you offered: "perl script.pl /path/to/dir/*" as I thought the code was for the ARGV file and not a perl script, even though this is a perl forum. Why would the ARGV file contain file paths, surely I could point at a directory and just have it convert everything in there.

As far as I can tell the command line interface allows Perl expressions to be applied to values but your requirements are more comprehensive. Can you paste the contents of one of the .args files that comes with the distribution, I want to confirm they don't contain Perl code. I believe os x comes with Perl already installed, if you run the command perl -v and it returns a version, this will confirm that.

Thanks for the .args example, looks like some kind of mapping schema, I doubt raw Perl code is acceptable as suspected.

Now you can check if you have the Image::ExifTool module installed:

Code

perl -MImage::ExifTool -e ';'

If its not installed I would recommend installing cpan minus to your home first since os x uses Perl internally and it may be risky to mess with it. This suggests you install using the following commands:

Now you can harness the full power of exiftool via Perl. I suggest you take a look at a beginners Perl tutorial, but you can jump straight in and play around with the scripts you have been provided. I wouldn't worry about batches for now.

Had some time to throw together a rough demo. It searches for all gifs in the same folder as the script, uses these to construct the txt and xmp filepaths, parses the txt files, then generates the xmp files.

This is all quite new to me, I have been going over various instructions and believe I understand most of it, but I cannot get cpan to install. I went to the link you referenced to and noticed that I could install Perlbrew and it would contain cpan but was not sure if the instructions on the site would work for me as it's not a good idea to install on the root:

http://perlbrew.pl

I didn't see the "perl - -l ~/perl5" command on the install instructions for Perlbrew ("\curl -L http://install.perlbrew.pl | bash") so didn't want to risk it, I just can't re-install OS X on my main machine, it's just not an option right now.

The command you had from another site produced the following: "-bash: fetch: command not found", it's not working for me.

Not really sure how to proceed, would like to know if Perlbrew is the way to go?

I don't know what it is about these command line installs but they never work first time, ever. I really what a difficult process ExifTool was to install, it was so difficult.

Also, the following command "perl -MImage::ExifTool -e ';'" produces the following error:

You already have Perl installed therefore installing Perlbrew isn't necessary. Perlbrew is useful when you want to manage multiple Perl installations and don't have root access. Using your systems Perl shouldn't be a problem.

Try replacing the fetch command with a curl one as per App::cpanminus. You should have curl installed:

I read not to install via the "curl" method, I recognise that the "perl - -l ~/perl5 App::cpanminus local::lib" command is present, which I think installs on the users Home, but not sure about using curl - I don't want to mess my system up.

Alright, my final suggestion is to install cpanm under your system Perl:

Code

cpan App::cpanminus

Or just install Image::ExifTool directly:

Code

cpan Image::ExifTool

I don't know enough about OS X to provide further advice on installing a program that retrieves content from the web like wget, fetch or curl. I'm also assuming you don't already have App::cpanminus installed.