我编写了一个程序来比较两个文件夹(每个文件有1000个文件)的图像文件和一些逻辑(see this SO question)。
执行它时,成功比较直到900张图像,但它会产生类似Use of uninitialized value within @tfiles2 in concatenation (.) or string at C:\dropbox\Image_Compare\image_magick.pl line 55
(#3)的错误。
然后我得到一个像Perl Command Line Interpreter has stopped working
这样的弹出错误,所以我关闭了程序。
我的代码如下:
#!/usr/bin/perl
use Image::Magick;
no warnings 'uninitialized';
use warnings;
use diagnostics;
#use strict;
use List::Util qw(first);
my $directory1="C:/dropbox/Image_Compare/folder1";
opendir(DIR, $directory1) or die "couldn't open $directory1: $!\n";
my @files1 = grep { (!/^\./) && -f "$directory1/$_" } readdir(DIR);
closedir DIR;
print @files1;
print 'end of files1';
my $directory2="C:/dropbox/Image_Compare/folder2";
opendir(DIR, $directory2) or die "couldn't open $directory2: $!\n";
my @files2= grep { (!/^\./) && -f "$directory2/$_" } readdir(DIR);
closedir DIR;
print @files2;
print 'end of files2';
print $files1[0];
foreach my $fils2 (@files2)
{
$g1 = Image::Magick->new;
$g2 = Image::Magick->new;
$temp1 = $g1->Read( filename=>"C:/dropbox/Image_Compare/folder1/".$files1[0]."");
$temp1 = $g2->Read( filename=>"C:/dropbox/Image_Compare/folder2/".$fils2."");
$g3 = $g1->Compare( image=>$g2, metric=>'AE' ); # compare
$error1 = $g3->Get( 'error' );
#print $error1;
if ($error1 == '0')
{
print "Matching image is:";
print $fils2 . "\n";
my $tdirectory2="C:/dropbox/Image_Compare/folder2";
opendir(DIR, $tdirectory2) or die "couldn't open $directory2: $!\n";
my @tfiles2 = grep { (!/^\./) && -f "$tdirectory2/$_" } readdir(DIR);
closedir DIR;
#my $index = firstidx { $_ eq'"' .$fils2.'"' } @tfiles2;
my $index = first { $tfiles2[$_] eq $fils2} 0..$#tfiles2;
#print $fils2;
print $index;
my $i=0;
foreach my $fils1 (@files1)
{
print 'ganesh';
print $files1[$i];
print $tfiles2[$index];
print 'gowtham'; print "<br />";
#print @tfiles2;
$g4 = Image::Magick->new;
$g5 = Image::Magick->new;
$temp2 = $g4->Read( filename=>"C:/dropbox/Image_Compare/folder1/".$files1[$i]."");
$temp2 = $g5->Read( filename=>"C:/dropbox/Image_Compare/folder2/".$tfiles2[$index]."");
$g6 = $g4->Compare( image=>$g5, metric=>'AE' ); # compare
$error2 = $g6->Get( 'error' );
$i++;
$index++;
if ($error2 == '0') {}
else {print "Image not matching:"; print $tfiles2[$index]; last;}
#if ($i == '800') {last;}
}
last
}
}
任何人都可以帮忙,我在做错了。
文件夹1文件名:0025.bmp到1051.bmp;
文件夹2文件名:0000.bmp到1008.bmp;
由于 内甚
答案 0 :(得分:2)
我不知道哪条违规行,但其中一条很可能是候选人:
$temp2 = $g5->Read( filename=>"C:/dropbox/Image_Compare/folder2/".$tfiles2[$index]."");
或
else {print "Image not matching:"; print $tfiles2[$index]; last;}
请注意,无论是否在数组边界内,都要增加$index
。你没有检查条件$index > $#tfiles
,它应该打破循环。
您可能想断言两个输入数组都包含&gt;&gt; 900个元素,打印长度如print "length: ", scalar @array, "\n";
。
您可以通过测试数组中元素的定义来测试undefined
错误实际发生的索引:
if (not defined $tfiles[$index] or not defined $files1[$i]) {
die "There was an undefined element at index=$index, i=$i";
}
但是,$i
和$index
之间的偏移是不变的(如my answer中所述),因此您不必实际携带两个变量。
一个简单的比较器子程序可以使您的代码更具可读性,从而有助于调试(参见procedural programming)。
# return true if matching, false otherwise.
sub compare_images {
my ($file1, $file2) = @_;
my $image1 = Image::Magick->new;
$image1->Read(filename => $file1);
my $image2 = Image::Magick->new;
$image2->Read(filename => $file2);
my $result = $image1->Compare(image => $image2, metric => 'AE')->Get('error');
# free memory
undef $image1;
undef $image2;
return 0 == $result;
}
称为
my $image_root = "C:/dropbox/Image_Compare";
my ($folder1, $folder2) = qw(folder1 folder2);
unless (compare_images("$image_root/$folder1/$files1[$i]",
"$image_root/$folder2/$tfiles[$index]")) {
print "Images not matching at index=$index, i=$i\n";
print "filename: $tfiles[$index]\n";
last;
}
你可以阅读你的目录,如
sub get_images_from_dir {
my ($dirname) = @_;
-d $dirname or die qq(The path "$dirname" doesn't point to a directory!);
opendir my $dir => $dirname or die qq(Can't open "$dirname": $!);
my @files = grep {!/^\./ and -f "$dirname/$_"} readdir $dir;
closedir $dir;
unless (@files) { die qq(There were no interesting files in "$dirname".) }
return @files;
}
这些步骤使代码更具可读性,并且可以轻松插入支票。