CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Parsing data from TXT file into XMP format with ExifTool

 

First page Previous page 1 2 Next page Last page  View All


teddoff
Novice

May 9, 2015, 8:33 PM

Post #1 of 38 (15001 views)
Parsing data from TXT file into XMP format with ExifTool Can't Post

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!

Presently three sets of data exist in a text file:


Code
Title: 
You've Been Peeling An Orange Wrong - Tablespoon

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

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



Outputting the data in XMP structures it like thus:


Code
<?xpacket begin='' id=''?> 
<x:xmpmeta xmlns:x='adobe:ns:meta/' x:xmptk='XMP toolkit 2.9-9, framework 1.6'>
<rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#' xmlns:iX='http://ns.adobe.com/iX/1.0/'>
<rdf:Description rdf:about='' xmlns:Iptc4xmpCore='http://iptc.org/std/Iptc4xmpCore/1.0/xmlns/'>
<Iptc4xmpCore:SubjectReference>http://www.tablespoon.com/posts/youve-been-peeling-an-orange-wrong/14800c66-27f2-4715-9936-d299ec7bfa9f?utm_source=zergnet.com&amp;utm_medium=referral&amp;utm_campaign=zergnet_280460</Iptc4xmpCore:SubjectReference>
<Iptc4xmpCore:SubjectCode>
<rdf:Bag>
<rdf:li>http://www.tablespoon.com/posts/youve-been-peeling-an-orange-wrong/14800c66-27f2-4715-9936-d299ec7bfa9f?utm_source=zergnet.com&amp;utm_medium=referral&amp;utm_campaign=zergnet_280460</rdf:li>
</rdf:Bag>
</Iptc4xmpCore:SubjectCode>
</rdf:Description>
<rdf:Description rdf:about='' xmlns:photoshop='http://ns.adobe.com/photoshop/1.0/'>
<photoshop:Headline>You&apos;ve Been Peeling An Orange Wrong - Tablespoon</photoshop:Headline>
</rdf:Description>
<rdf:Description rdf:about='' xmlns:dc='http://purl.org/dc/elements/1.1/'>
<dc:description><rdf:Alt><rdf:li xml:lang='x-default'>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.)</rdf:li></rdf:Alt></dc:description>
</rdf:Description>
<rdf:Description rdf:about='' xmlns:photomechanic='http://ns.camerabits.com/photomechanic/1.0/'>
</rdf:Description>
<rdf:Description rdf:about='' xmlns:xap='http://ns.adobe.com/xap/1.0/'>
<xap:Rating>0</xap:Rating>
</rdf:Description>
</rdf:RDF>
</x:xmpmeta>
<?xpacket end='w'?>



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:


Code
use Image::ExifTool; 
my $exifTool = new Image::ExifTool;
$exifTool->SetNewValue(Author => 'Phil Harvey');
$exifTool->WriteInfo('image.jpg','modified_image.jpg');


ref: http://owl.phy.queensu.ca/~phil/exiftool/ExifTool.html


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".

This of which I do not know how to do, can anybody advise please.


Laurent_R
Veteran / Moderator

May 10, 2015, 3:07 AM

Post #2 of 38 (14964 views)
Re: [teddoff] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

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.


teddoff
Novice

May 10, 2015, 7:17 AM

Post #3 of 38 (14946 views)
Re: [Laurent_R] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

No, what I am after is converting the TXT files data into XMP ones. But the data from "Title" would go into the IPTC:Headline field and so on.


Laurent_R
Veteran / Moderator

May 10, 2015, 11:37 PM

Post #4 of 38 (14828 views)
Re: [teddoff] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

Alright, then you probably have to read the exif data, find the line immediately after the title tag and correct the XML.


teddoff
Novice

May 11, 2015, 9:43 PM

Post #5 of 38 (14810 views)
Re: [Laurent_R] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

Not being a coder of any sort, I actually don't know how to do that. But thanks for the info, will look into it..


Laurent_R
Veteran / Moderator

May 11, 2015, 11:14 PM

Post #6 of 38 (14808 views)
Re: [teddoff] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

Well, show the code that you have presently, we should be able to tell you what to add.


teddoff
Novice

May 12, 2015, 6:10 AM

Post #7 of 38 (14803 views)
Re: [Laurent_R] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

I don't actually have any. I was first and foremost trying to get information about what would need to be done.

I actually can't code.


Laurent_R
Veteran / Moderator

May 12, 2015, 10:17 AM

Post #8 of 38 (14798 views)
Re: [teddoff] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

But you ran some script to know what the ExifTool module is producing, didn't you? If so, show what you ran, even if it is far from what you ultimately need.


teddoff
Novice

May 13, 2015, 7:35 PM

Post #9 of 38 (14781 views)
Re: [Laurent_R] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

No, the XMP an export from the photo management application I am using, the other file is just a text file.


teddoff
Novice

May 14, 2015, 4:20 PM

Post #10 of 38 (14767 views)
Re: [teddoff] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

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.

So the line "SetNewValue" option would be a little more involved.


Zhris
Enthusiast

May 14, 2015, 7:41 PM

Post #11 of 38 (14760 views)
Re: [teddoff] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

Hi,

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:


Code
use strict; 
use warnings;
use Data::Dumper;

my %hash = map s/[\s:]*$//r, grep /\S+/, <DATA>;

print Dumper \%hash;

__DATA__

Title:
You've Been Peeling An Orange Wrong - Tablespoon

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

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'
};


CHris


(This post was edited by Zhris on May 14, 2015, 7:44 PM)


teddoff
Novice

May 14, 2015, 9:26 PM

Post #12 of 38 (14748 views)
Re: [Zhris] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

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.

Oh dear, not even able to figure out how to execute ExifTool!


Zhris
Enthusiast

May 15, 2015, 12:33 PM

Post #13 of 38 (14728 views)
Re: [teddoff] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

Hi,

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:

Code

use Image::ExifTool;
my $exifTool = new Image::ExifTool;
$exifTool->SetNewValue(Author => 'Phil Harvey');
$exifTool->WriteInfo('image.jpg','modified_image.jpg');


ref: http://owl.phy.queensu.ca/~phil/exiftool/ExifTool.html


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:


Code
use strict;  
use warnings;
use Image::ExifTool;

my $filepath = '/path/to/file.txt';
open my $filehandle, '<', $filepath or die "cannot open '$filepath': $!";
my %hash = map s/[\s:]*$//r, grep /\S+/, <$filehandle>;
close $filehandle;

my $exifTool = new Image::ExifTool;
$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.


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:


Code
for my $filepath ( @ARGV ) 
{
...
}


Chris


(This post was edited by Zhris on May 15, 2015, 1:09 PM)


teddoff
Novice

May 15, 2015, 5:58 PM

Post #14 of 38 (14716 views)
Re: [Zhris] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

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.

Apologies if I am confusing the situation further.


(This post was edited by teddoff on May 15, 2015, 6:01 PM)


Zhris
Enthusiast

May 15, 2015, 7:10 PM

Post #15 of 38 (14709 views)
Re: [teddoff] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

Hi,

Do you have Perl installed on your system and have ran a Perl script before?

Chris


teddoff
Novice

May 15, 2015, 9:03 PM

Post #16 of 38 (14704 views)
Re: [Zhris] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

No, I'll have to look into it.


teddoff
Novice

May 16, 2015, 5:43 AM

Post #17 of 38 (14695 views)
Re: [teddoff] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

I just thought I could feed the Perl script into ExifTool, I didn't know it was going to encompass a new discipline.


Zhris
Enthusiast

May 16, 2015, 11:23 AM

Post #18 of 38 (14685 views)
Re: [teddoff] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

Hi,

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.

Chris


teddoff
Novice

May 16, 2015, 12:44 PM

Post #19 of 38 (14683 views)
Re: [Zhris] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

It does: "This is perl, v5.8.9 built for darwin-2level"

ARGS example:


Code
# 
# File: xmp2iptc.args
#
# Description: Tag name translations for converting from XMP to IPTC
#
# Usage: exiftool -tagsFromFile src.jpg -@ xmp2iptc.args dst.jpg
#
# Author: 2005/05/14 - P. Harvey
#
# References: http://www.iptc.org/IPTC4XMP/
# http://www.iptc.org/IIM/
# http://www.adobe.com/products/xmp/pdfs/xmpspec.pdf
#
# Notes: The following tags have the same names in both IPTC and XMP,
# so they are covered by the first argument: Category, City,
# Credit, DateCreated, Headline, Source, SupplementalCategories
# and Urgency.
#
-XMP:* > IPTC:*
-XMP-dc:Title > IPTC:Headline
-EXIF:XPTitle > IPTC:Headline
# end



(This post was edited by teddoff on May 16, 2015, 1:04 PM)


Zhris
Enthusiast

May 16, 2015, 1:34 PM

Post #20 of 38 (14677 views)
Re: [teddoff] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

Hi,

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:


Code
fetch -o- http://cpanmin.us | perl - -l ~/perl5 App::cpanminus local::lib 
eval `perl -I ~/perl5/lib/perl5 -Mlocal::lib`
echo 'eval `perl -I ~/perl5/lib/perl5 -Mlocal::lib`' >> ~/.profile


Then you can install Image::ExifTool using:


Code
cpanm Image::ExifTool


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.

Chris


Zhris
Enthusiast

May 16, 2015, 3:20 PM

Post #21 of 38 (14672 views)
Re: [teddoff] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

Hi,

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.

http://demo.massweb.co.uk/image/exiftool/exiftool.pl

Which loosely uses the following code (I used a slightly modified version to work in browser):


Code
use strict;   
use warnings;
use Image::ExifTool;

for my $filepath_gif ( <*.gif> )
{
my $filepath_txt = $filepath_gif . '.txt';
my $filepath_xmp = $filepath_gif . '.xmp';

open my $filehandle_txt, '<', $filepath_txt or die "cannot open '$filepath_txt': $!";
my %hash = map { my$s=$_;$s=~s/[\s:]*$//;$s } grep /\S+/, <$filehandle_txt>;
close $filehandle_txt;

my $exifTool = new Image::ExifTool;
$exifTool->SetNewValue(Author => $hash{Title}) or die;
$exifTool->WriteInfo( undef, $filepath_xmp, 'XMP' );
}


Chris


teddoff
Novice

May 17, 2015, 11:13 AM

Post #22 of 38 (14646 views)
Re: [Zhris] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

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:


Code
Can't locate Image/ExifTool.pm in @INC (@INC contains: /opt/local/lib/perl5/site_perl/5.8.9/darwin-2level /opt/local/lib/perl5/site_perl/5.8.9 /opt/local/lib/perl5/site_perl /opt/local/lib/perl5/vendor_perl/5.8.9/darwin-2level /opt/local/lib/perl5/vendor_perl/5.8.9 /opt/local/lib/perl5/vendor_perl /opt/local/lib/perl5/5.8.9/darwin-2level /opt/local/lib/perl5/5.8.9 .). 
BEGIN failed--compilation aborted.


Any advice please?


(This post was edited by teddoff on May 17, 2015, 11:16 AM)


Zhris
Enthusiast

May 18, 2015, 7:30 AM

Post #23 of 38 (14624 views)
Re: [teddoff] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

Hi,

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:


Code
curl -L https://cpanmin.us | perl - -l ~/perl5 App::cpanminus local::lib


Chris


teddoff
Novice

May 18, 2015, 5:46 PM

Post #24 of 38 (14612 views)
Re: [Zhris] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

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.


Zhris
Enthusiast

May 18, 2015, 7:03 PM

Post #25 of 38 (14608 views)
Re: [teddoff] Parsing data from TXT file into XMP format with ExifTool [In reply to] Can't Post

Hi,

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.

Chris


(This post was edited by Zhris on May 18, 2015, 7:05 PM)

First page Previous page 1 2 Next page Last page  View All
 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives