Perl Imgsize没有在循环中工作?

时间:2014-01-18 10:12:18

标签: perl image-processing file-io size dimensions

我已经测试过使用perl使用以下简单代码获取图像大小,同时坐在一个目录中:

#!/usr/local/bin/perl
use Image::Size;

my $x;
my $y; 
my $id;

opendir my $dh, "."  or die "$0: opendir: $!";
 ($x, $y, $id) = imgsize("test_image.png");
 print $x." , ".$y."\n";

这很好用,它给了我正确的图像宽度和高度。 然后我将它合并到我的大代码中,在那里我循环浏览文件夹和文件。 所以更大的代码就是:

# I first get into a certain folder, which consists of lots of other folders, 
# each named one of the elements in the @views array 
# which I've declared somewhere else

foreach $view(@views)
{   my $folder = $view; 
    opendir my $dh, "$classfolder\\organizedbyviews\\$folder\\cropped"  or die "$0: opendir: $!";
    print "......\nReading folder: $view \n.....\n";

    my $file;
    while ($file = readdir($dh)) {
        #read the image's width and height
        my $w;
        my $h;
        my $id;
        ($w, $h, $id) = imgsize($file);

        print $file.": ".$w.", ".$h."\n";
     }
}

现在当我运行它时,我知道我的路径等没有问题,因为我的输出正确地列出了文件夹中的图像文件,但它只是不输出它们的尺寸:

......
Reading folder: view1
......
.: ,
..: ,
file1.jpg: , 
file2.jpg: , 
file3.jpg: , 
file4.jpg: , 
file5.jpg: , 
......
Reading folder: view2
......
.: ,
..: ,
file1.jpg: , 
file2.jpg: , 
file3.jpg: , 
file4.jpg: , 
file5.jpg: , 

等等。

为什么imgsize()功能突然无效?

1 个答案:

答案 0 :(得分:2)

添加文件名前面的路径:

$file = "$classfolder\\organizedbyviews\\$folder\\cropped\\".$file;