2017-04-17 35 views
0

我正在嘗試讀取和比較文件夾中的大量圖像文件。但我無法讀取圖像文件。使用Perl讀取和比較文件夾中的圖像

代碼片段:

use strict; 
use warnings; 
use Image::Compare; 

opendir my $ref_dir, "./" or die "Cannot open directory: $!"; 
my @ref_files = readdir $ref_dir; 
closedir $ref_dir; 

print $#ref_files; 

my($cmp) = Image::Compare->new(); 


$cmp->set_image1(
     img => './'.$ref_files[0], #one instance - reading first file in the folder 
     type => 'bmp', 
    ); 

的代碼拋出下面的錯誤 - 「無法讀取文件‘./’。圖像數據:‘無法打開./:權限被拒絕’在C: /Perl64/site/lib/Image/Compare.pm line 162。「

如果我直接提供文件名 - img =>'./image.bmp',代碼就可以正常工作。所以它不能是一個權限問題。

+0

檢查** **許可的'$ ref_files [0]'文件。 – AbhiNickz

+0

它直接將文件名作爲字符串提供。所以這不是一個許可問題。 – Maximus

回答

2

錯誤消息似乎很清楚。

無法讀取文件「./」。圖像數據:「無法打開./:權限被拒絕

你從opendir()又回到第一個文件是當前目錄( .) - Image :: Compare無法從該文件獲取想要的圖像數據並不奇怪!

也許你應該添加一個過濾器,只返回你感興趣的文件。

my @ref_files = grep { -f } readdir $ref_dir; 
+0

謝謝。這個問題現在已經解決了 – Maximus