Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

date created in exiftool

by flieckster (Scribe)
on Dec 04, 2018 at 13:26 UTC ( [id://1226716]=perlquestion: print w/replies, xml ) Need Help??

flieckster has asked for the wisdom of the Perl Monks concerning the following question:

Hi there, i have a 2 part question. 1, is this code, which is simply going to a known folder of .psd files reading them, and then using exiftool to pull the 'dateTimeOrginal' from the file. when i run this script as is, i get "Use of uninitialized value in concatenation (.) or string at /Users/flieckb/Desktop/StlyeSorterTUMI v2.pl line 42." for some reason this set up seems to work sometimes, and other times it doesn't, but i can't see to figure out why. once that part is set up i'd like to use that date in the commented out script below replacing $date with the exiftool 'DateTimeOriginal' date, but i can't seem to print out the "info->$_" later in the script.
#!/usr/bin/perl -w use Net::FTP; use File::Copy; use File::Basename; use POSIX qw(strftime); use Image::ExifTool 'ImageInfo'; print "#################################\n"; print "#################################\n"; print "#################################\n"; print "#################################\n"; print "#################################\n"; my $sort = "/Users/flieckb/Desktop/psds/"; my $wpf = "/Volumes/photorepos/Weekly_Product_Folders/Tumi/"; my $current = `date +%m/%d/%Y`; chomp $current; chdir( $sort ) or warn "Cant chdir to $sort $!"; my(@sort_list) = glob "*.psd"; my $sortlist = @sort_list; my $exifTool = new Image::ExifTool; chdir ($wpf) or warn "Cant chdir to $wpf $!"; foreach my $file (@sort_list){ my $prefix; my $style; my $view; my @tags = qw(DateTimeOriginal); $exifTool->Options(Duplicates => 0, DateFormat => '%m-%d-%Y'); my $info = $exifTool->ImageInfo($file, \@tags); print "$file\t"; foreach (@tags) { print "$info->{$_}\t"; } print "\n"; # # if ($file =~qr/\A([^_]+)-([^_]+)(?:_([^_]+))?\.([^\.]+)\z/) { # $prefix = $1; # $style = $2; # $view = $3; # # print "prefix $prefix\n"; # print "style $style\n"; # print "view $view\n"; # # my $movelist = "$sort$file"; # if (-d "$style") { # chdir ($style) and print "$style already exsits\n"; # } # else # { # mkdir($style, 0777)and print "$style was created\n"; # chdir ($style); # } # ## # if (-d "$date") { # print "$date already exsits\n"; # copy ($movelist, $date)or warn print "Copy $date Failed: $!\n"; # } # else # { # mkdir($date, 0777); # copy ($movelist, $date)or warn print "Copy $date Failed: $!\n"; # } # chdir ($wpf); # } # else # { # print "did not match expression = $file\n"; # }; };

Replies are listed 'Best First'.
Re: date created in exiftool
by roboticus (Chancellor) on Dec 04, 2018 at 14:12 UTC

    flieckster:

    Since line 42 is printing the value in $info->{$_} for key $_, it simply means that the key has no value for it. It may be possible that the ImageTool call is failing because it can't find the file you're specifying. Since you're changing directories between fetching the filenames and processing them, it could be that you just need to ensure you're providing a correct relative or absolute path to the file.

    I'd also suggest checking out what error conditions you can expect from Image::ExifTool to see if it's working properly. (I just tried to review the docs for it, but cpan.org is feeding me blank pages at the moment when I give it a search term.)

    Another thought: some images might not have values for the tags you're checking, so you might want to check the existance of the tag before printing anything:

    foreach (@tags) { if (! exists $info->{$_}) { print "file $file doesn't have tag $_\n"; } elsif (! defined $info->{$_}) { print "file $file has tag $_, but it has no value\n"; } else { print "$info->{$_}\t"; } }

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Hi Roboticus, thanks for the snippet. i re-wrote the code adding your added variables, and it seems to be working now, but also worked with my original code now. i'm not sure why. I can get this to print out the date's in the format i want with "$info->{$_}" but starting on line 73 where i want to use that date output, is where it fails. with this error.
      prefix TUMI style 1174451041 view tag 1174451041 was created Use of uninitialized value $_ in hash element at /var/folders/ty/jy84g +mxs4f7c_n0bhfhvs6t40000gq/T/TextWranglerRunTemp-untitled text.pl line + 73. Use of uninitialized value in string at /var/folders/ty/jy84gmxs4f7c_n +0bhfhvs6t40000gq/T/TextWranglerRunTemp-untitled text.pl line 73. Use of uninitialized value $_ in hash element at /var/folders/ty/jy84g +mxs4f7c_n0bhfhvs6t40000gq/T/TextWranglerRunTemp-untitled text.pl line + 79. Use of uninitialized value in mkdir at /var/folders/ty/jy84gmxs4f7c_n0 +bhfhvs6t40000gq/T/TextWranglerRunTemp-untitled text.pl line 79. Use of uninitialized value $_ in hash element at /var/folders/ty/jy84g +mxs4f7c_n0bhfhvs6t40000gq/T/TextWranglerRunTemp-untitled text.pl line + 80. Use of uninitialized value in scalar assignment at /System/Library/Per +l/5.18/File/Copy.pm line 110. Use of uninitialized value $to in string eq at /System/Library/Perl/5. +18/File/Copy.pm line 102. Use of uninitialized value $to in -d at /System/Library/Perl/5.18/File +/Copy.pm line 134. Use of uninitialized value $to in stat at /System/Library/Perl/5.18/Fi +le/Copy.pm line 142. Use of uninitialized value $to in open at /System/Library/Perl/5.18/Fi +le/Copy.pm line 234. Use of uninitialized value $_ in hash element at /var/folders/ty/jy84g +mxs4f7c_n0bhfhvs6t40000gq/T/TextWranglerRunTemp-untitled text.pl line + 80. Use of uninitialized value in concatenation (.) or string at /var/fold +ers/ty/jy84gmxs4f7c_n0bhfhvs6t40000gq/T/TextWranglerRunTemp-untitled +text.pl line 80.
      #!/usr/bin/perl -w use Net::FTP; use File::Copy; use File::Basename; use POSIX qw(strftime); BEGIN { unshift @INC, '/usr/bin/lib' } use Image::ExifTool 'ImageInfo'; print "#################################\n"; print "#################################\n"; print "#################################\n"; print "#################################\n"; print "#################################\n"; my $sort = "/Users/flieckb/Desktop/psds/"; my $wpf = "/Users/flieckb/Desktop/sort/"; chdir( $sort ) or warn "Cant chdir to $sort $!"; my(@sort_list) = glob "*.psd"; my $sortlist = @sort_list; my $prefix; my $style; my $view; my $exifTool = new Image::ExifTool; foreach my $file (@sort_list){ my @tags = qw(DateTimeOriginal); $exifTool->Options(Duplicates => 0, DateFormat => '%m-%d-%Y'); my $info = $exifTool->ImageInfo($file, \@tags); foreach (@tags) { print "$info->{$_}\t"; } print "\n"; # # foreach (@tags) { # if (! exists $info->{$_}) { # print "file $file doesn't have tag $_\n"; # } # elsif (! defined $info->{$_}) { # print "file $file has tag $_, but it has no value\n"; # } # else { # print "$info->{$_}\n"; # } # if ($file =~qr/\A([^_]+)-([^_]+)(?:_([^_]+))?\.([^\.]+)\z/) { # $prefix = $1; # $style = $2; # $view = $3; # # print "prefix $prefix\n"; # print "style $style\n"; # print "view $view\n"; # # my $movelist = "$sort$file"; # if (-d "$style") { # chdir ($style) and print "$style already exsits\n"; # } # else # { # mkdir($style, 0777)and print "$style was created\n"; # chdir ($style); # } # ## # if (-d "$info->{$_}") { # print "$info->{$_} already exsits\n"; # copy ($movelist, $info->{$_})or warn print "Copy $info->{$_} Failed: + $!\n"; # } # else # { # mkdir($info->{$_}, 0777); # copy ($movelist, $info->{$_})or warn print "Copy $info->{$_} Failed: + $!\n"; # } # chdir ($wpf); # } # else # { # print "did not match expression = $file\n"; # }; } # }

        There's an awful lot in your script which doesn't need to be there. Here's a stripped down version with none of the unnecessary modules, no commented-out code, indented loops and no BEGIN blocks. Oh, and it has strict as should every script you ever write.

        #!/usr/bin/env perl use strict; use warnings; use Image::ExifTool 'ImageInfo'; print "#################################\n"; my $sort = "/tmp/"; chdir $sort or die "Cant chdir to $sort $!"; my @sort_list = glob "*.jpeg"; my $exifTool = Image::ExifTool->new; my @tags = qw(DateTimeOriginal); foreach my $file (@sort_list) { $exifTool->Options(Duplicates => 0, DateFormat => '%m-%d-%Y'); my $info = $exifTool->ImageInfo($file, \@tags); foreach (@tags) { print "$file $_: $info->{$_}\t" if exists $info->{$_}; } print "\n"; }

        This works fine for a collection of jpegs I've put in /tmp for testing (I have no use for .psd files). It produces no errors and no warnings and a nice list of original dates in day-in-the-middle format (your choice, not mine).

Re: date created in exiftool
by bliako (Monsignor) on Dec 04, 2018 at 16:33 UTC

    Are you using the correct name for the key? Are you sure the key '[dD]ateTimeOriginal' exists in the metadata and has a value?

    p.s. in order to quickly see what metadata is there, there is a commandline tool called exiftool, i think installed automatically from cpan

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1226716]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-03-28 09:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found