News:

2023-08-10 - ExifTool version 12.65 released

Main Menu

Age Calculation

Started by Hakkki, February 27, 2016, 07:00:59 PM

Previous topic - Next topic

Hakkki

Hi Members,

I want to rename my archive based on the age of my little girl. DDMMYYYY
It would be enough if I could add the calculated values as ExifID Tags.

Could you please help me?

Hakkki

Hi Community,
Now I am able to run the code as follows. Everything is fine.
But I think I discovered a bug in Exiftool ?!


my %people = (
    'Ela'  => '2013:10:28 00:00:00',
);

%Image::ExifTool::UserDefined = (
    'Image::ExifTool::Composite' => {
        MyAges => {
            Require => {
                0 => 'EXIF:DateTimeOriginal',
            },
            ValueConv => sub {
                my $val = shift;
                my ($name, @ages);
                foreach $name ('Ela') {
                    require Time::Local;
                    my $t = TimeLocal(reverse split /[ :]/, $$val[0]);
                    my $b = TimeLocal(reverse split /[ :]/, $people{$name});
                    my $d = ($t - $b) / (24 * 60 * 60);
                        my $yr = sprintf("%02d",int($d / 365));
                        my $mon = sprintf("%02d",int(($d - 365 * $yr) / 30));
my $day = sprintf("%02d",int($d - 365 * $yr)-(30 * $mon));
push @ages, "${yr}${mon}${day}";
                    }
return @ages ? \@ages : undef;
            },
        },
    },
);
1;  #end


For some images I get the following error:
"Month "12" out of range 0..11 at Image/Exiftool.pm line 4893"

Please; I am out of knowledge now  :'(

Hayo Baan

The Time::lLocal function you are using is counting months, not from 1 to 12, but from 0 to 11, just have your code subtract one from and you'll be fine. You'll need some more perl wizardry here, and if you can't find out how to do this, let me know and I'll show you (not at my computer right now so can't really work this out now).
Hayo Baan – Photography
Web: www.hayobaan.nl

Hakkki

Dear Hayo,

sorry for the late response.
I was not able to handle this. Is there another alternative for Time::Local in Perl?

I really appreciate your help.
Thanks in advance

Phil Harvey

There is no alternative but all you have to do is subtract one from the month after splitting the date into components.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Hakkki

Hi Phil,

sorry if I'm not be really capable to do that.
But adding a -1 to the attached line does not work proper (false calculation) and still gives the same error for the other files.

my $mn = sprintf("%02d",int(($d - 365 * $yr) / 30)-1);

Phil Harvey

I don't have time to test this right now, but I think this is what you need:

my %people = (
    'Ela'  => '2013:10:28 00:00:00',
);

%Image::ExifTool::UserDefined = (
    'Image::ExifTool::Composite' => {
        MyAges => {
            Require => {
                0 => 'EXIF:DateTimeOriginal',
            },
            ValueConv => sub {
                my $val = shift;
                my ($name, @ages);
                foreach $name ('Ela') {
                    require Time::Local;
                    my @t1 = split /[ :]/, $$val[0];
                    my @t2 = split /[ :]/, $people{$name};
                    $t1[0] -= 1900; $t1[1] -= 1;
                    $t2[0] -= 1900; $t2[1] -= 1;
                    my $t = TimeLocal(reverse @t1);
                    my $b = TimeLocal(reverse @t2);
                    my $d = ($t - $b) / (24 * 60 * 60);
                    my $yr = sprintf("%02d",int($d / 365));
                    my $mon = sprintf("%02d",int(($d - 365 * $yr) / 30));
                    my $day = sprintf("%02d",int($d - 365 * $yr)-(30 * $mon));
                    push @ages, "${yr}${mon}${day}";
                }
                return @ages ? \@ages : undef;
            },
        },
    },
);
1;  #end


Here I have also subtracted 1900 from the year because this is what timelocal wants.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Hakkki

Thanks for your help.
But it does not work.

Now I have a problem with the value for days. For former non problematic files it adds 1 day.
The month calculation for the former problematic files is now right, but even false for the days.
Input Exif:Date = 12.24 -> 2 month 4 days
Input Exif:Date = 12.31 -> 2 month 4 days