两个文件(一个生,一个XMP)与ExifTool阅读

问题描述 投票:0回答:1

我是新来的Perl和甚至更新的版本来ExifTool和我因此很可能失去了一些东西基本相当。

我们的目标是从照片文件中读取XMP领域。看着在ExifTool网站和CPAN两个exiftool文档,我能够读取标签JPEG和XMP附属文件,都没有问题。

问题是,当我从一个原始文件,这显然不具有自定义字段,我会得到一个错误未初始化值读取。这是可以预料的。

所以,我想有一些代码,说:“如果你读从原始文件中的字段/标签,它是不存在的,看看相关的XMP文件,如果失败,则返回一个空字符串。”

因此,我试图打开ExifTool,如第二个实例:

my $exifInfo = ImageInfo($filePath);

my $exifInfoXMP = ImageInfo($filePathXMP);

但是,不断失败。如果我直接从读取XMP开始走,它工作得很好,所以我得到,我不能出同一两个时间ExifTool结构印象(这不可能是正确的,我必须是错误这里) 。下面的代码工作,但我不能“交错”的条件语句上的两个文件。我得先处理原始,然后运行第二次与该XMP一个新的处理程序。了解PERL效率如何,我的做法不可能是一个很好的(尽管它的工作)。

特别地,是我感到困惑一行。如果我删除它,没有什么工作。 (应做好标记)。

$filePath =~ s/$photoExtensions$/.XMP/i;

该行重要的做同样的从一开始走(不是我的理想的解决方案)读取XMP。

任何人都有一个想法,在那里我搞乱?

谢谢,

保罗

页眉[编辑,以显示所有选项;表明所有使用过IN QUESTION]

#!/usr/bin/perl

# load standard packages
use strict;
use warnings;
use Data::Dumper;
use File::Find;
no warnings 'File::Find';
use Image::ExifTool ':Public';

# define proxy for ExifTool
my $exifTool = new Image::ExifTool;
my $exifToolXMP = new Image::ExifTool;

# turn on immediate updates
$|=1;

# common extensions that I want to recognize
my $photoExtensions = "\.(jpg|crw|cr2|cr3|rw2|orf|raw|nef|arw|dng)";
my $imageExtensions = "\.(tiff|tif|psd|png|eps|hdr|exr|svg|gif|afphoto|pdf)";
my $videoExtensions = "\.(flv|vob|ogv|avi|mts|m2ts|mov|qt|wmv|mp4|m4p|m4v|svi|3gp|3g2)";
my $audioExtensions = "\.(aiff|aac|wav|mp3|m4a|m4p|ogg|wma)";
my $appFileExtensions = "\.(on1|cos|cof)";
my $GPSFileExtensions = "\.(gpx|kml|kmz|log)";

# start main program
main();

常规问题

sub listKeywords { 
    print "Reads and displays file information from certain tags (typically set in Photomechanic):\n";
    print "\t1. Subject\n";
    print "\t2. Hierarchical Subject\n";
    print "\t3. Supplemental Categories\n";
    print "\t4. Label Name 1\n";
    print "\t5. Label Name 2\n";
    print "\t6. Label Name 3\n";
    print "\t7. Label Name 4\n\n";

    print "List Keywords ---\n\tEnter file name (with path) --> ";
    my $filePath = <STDIN>;
    chomp $filePath;
    $filePath =~ s/\\//g;
    $filePath =~ s/\s+$//;


    ########################################################

    # COMMENT OUT THE FOLLOWING LINE AND NOTHING WORKS;
    # $filePathXMP should be defined anyway, which suggests to 
    # me that the second invocation of ImageInfo doesn't actually occur. 
    # But I don't understand why.

    $filePath =~ s/$photoExtensions$/.XMP/i;
    print "\n\n";

    my $filePathXMP = $filePath;
    $filePathXMP =~ s/$photoExtensions$/.XMP/i;  # TO FIX: filename may not have uppercase extension

    # Get Exif information from image file
    my $exifInfo = $exifTool->ImageInfo($filePath);
    #    my $exifInfoXMP = $exifToolXMP->ImageInfo($filePath =~ s/$photoExtensions$/.XMP/gi);
    print "XMP Sidecar: \[$filePathXMP\]\n\n";

    ########################################################


    # Get Specific Tag Value
    my $hierarchicalSubject = $exifTool->GetValue('HierarchicalSubject');
    my $subject = $exifTool->GetValue('Subject');
    my $supplementalCategories = $exifTool->GetValue('SupplementalCategories');
    my $labelName1 = $exifTool->GetValue('LabelName1');
    my $labelName2 = $exifTool->GetValue('LabelName2');
    my $labelName3 = $exifTool->GetValue('LabelName3');
    my $labelName4 = $exifTool->GetValue('LabelName4');

    my $exifInfo = ImageInfo($filePathXMP);
    if (not defined $hierarchicalSubject) {$hierarchicalSubject = $exifTool->GetValue('HierarchicalSubject');}
    if (not defined $hierarchicalSubject) {$hierarchicalSubject = "";}
    if (not defined $subject) {$subject = $exifTool->GetValue('Subject');}
    if (not defined $subject) {$subject = "";}
    if (not defined $supplementalCategories) {$supplementalCategories = $exifTool->GetValue('SupplementalCategories');}
    if (not defined $supplementalCategories) {$supplementalCategories = "";}
    if (not defined $labelName1) {$labelName1 = $exifTool->GetValue('LabelName1');}
    if (not defined $labelName1) {$labelName1 = "";}
    if (not defined $labelName2) {$labelName2 = $exifTool->GetValue('LabelName2');}
    if (not defined $labelName2) {$labelName2 = "";}
    if (not defined $labelName3) {$labelName3 = $exifTool->GetValue('LabelName3');}
    if (not defined $labelName3) {$labelName3 = "";}
    if (not defined $labelName4) {$labelName4 = $exifTool->GetValue('LabelName4');}
    if (not defined $labelName4) {$labelName4 = "";}

    print "Subject:\n------------------------------\n$subject\n\n";
    print "Hierarchical Subject:\n------------------------------\n$hierarchicalSubject\n\n";
    print "Supplemental Categories:\n------------------------------\n$supplementalCategories\n\n";
    print "Label Name 1:\n------------------------------\n$labelName1\n\n";
    print "Label Name 2:\n------------------------------\n$labelName2\n\n";
    print "Label Name 3:\n------------------------------\n$labelName3\n\n";
    print "Label Name 4:\n------------------------------\n$labelName4\n\n";
}
perl exiftool
1个回答
0
投票

当你的代码是不完整的,我要问:你一定要与以下行启动脚本?

use strict;
use warnings;

这两条线是不是有骚扰你,他们会保护你,你可能在你的代码作出简单的错误。


恕我直言,你sub listKeywords()真正的问题是以下行:

my $exifInfo = ImageInfo($filePathXMP);

这里有两个问题:

  1. 从之前的几行重新定义变量$exifInfo
  2. 你不使用第二图像信息的面向对象方法。

我想你打算写什么是以下行:

my $exifInfoXMP = $exifToolXMP->ImageInfo($filePathXMP);
© www.soinside.com 2019 - 2024. All rights reserved.