【问题标题】:Perl Imgsize not working in loop?Perl Imgsize 不能循环工作?
【发布时间】:2014-02-07 18:35:27
【问题描述】:

我已经在一个目录中使用以下简单代码测试了使用 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()函数突然不起作用了?

【问题讨论】:

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


    【解决方案1】:

    在文件名前添加路径:

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

    【讨论】:

    猜你喜欢
    • 2016-12-21
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-10
    • 2017-12-12
    • 1970-01-01
    相关资源
    最近更新 更多