Perl的Imgsize不工作在循环? [英] Perl Imgsize not working in loop?

查看:134
本文介绍了Perl的Imgsize不工作在循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



 #!

我已经测试了使用perl获取图像的大小。 / usr / local / bin / perl
使用Image :: Size;

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

opendir my $ dh,。或死$ 0:opendir:$!;
($ x,$ y,$ id)= imgsize(test_image.png);
print $ x。,。$ y。\\\
;

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

$ $ p $ #我首先进入一个特定的文件夹,文件夹
#每个命名@views阵列中的一个元素
#我已经在其他地方声明

foreach $ view(@views)
{我的$文件夹= $视图;
opendir my $ dh,$ classfolder\\organizedbyviews\\ $ folder\\cropped或死$ 0:opendir:$!;
打印...... \\\
读取文件夹:$ view \\\
..... \\\
;

my $ file;
while($ file = readdir($ dh)){
#读取图片的宽度和高度
my $ w;
my $ h;
my $ id;
($ w,$ h,$ id)= imgsize($ file);

print $ file。:。$ w。,。$ h。\\\
;






$ b现在当我运行这个时,我知道没有问题与我的路径等,因为我的输出正确列出的文件夹中的图像文件,但它只是不输出他们的维度:

$ $ p $ ......
阅读文件夹:view1
......
。:,
..:,
file1.jpg:,
file2.jpg:,
file3.jpg:,
file4.jpg:,
file5.jpg:,
......
读取资料夹:view2
......
。:,
..:,
file1.jpg:,
file2.jpg:,
file3 .jpg:,
file4.jpg:,
file5.jpg:,

等等。



为什么 imgsize()函数突然不起作用?

解决方案

在文件名前添加路径:

  $ file =$ classfolder \\organizedbyviews\\ $ folder\\cropped\\。$ file; 


I've tested getting the image size using perl with the following simple code while sitting in one directory:

#!/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";

This worked fine, it gave me the correct width and height of the image. Then I incorporated it into my larger code, where I'm looping through folders and files. So the bigger code is this:

# 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";
     }
}

Now when I run this, I know there's no issue with my paths etc because my output correctly lists the image files in the folders, but it just doesn't output their dimensions:

......
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: , 

and so on.

Why is the imgsize() function suddenly not working?

解决方案

Add the path in front of the filename:

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

这篇关于Perl的Imgsize不工作在循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆