Maximus
Maximus

Reputation: 143

Read and compare images in folder using Perl

I am trying to read and compare large number of image files in a folder. But I am not able to read the image file.

Code snippet :

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',
    );

The code is throwing the following error - "Unable to read image data from file './.': 'Could not open ./.: Permission denied' at C:/Perl64/site/lib/Image/Compare.pm line 162. "

The code works fine if I provide the file name directly - img => './image.bmp'. So it can't be a permission issue.

Upvotes: 0

Views: 107

Answers (1)

Dave Cross
Dave Cross

Reputation: 69274

The error message seems pretty clear.

Unable to read image data from file './.': 'Could not open ./.: Permission denied

The first file you're getting back from opendir() is the current directory (.) - and it's no surprise that Image::Compare can't get the image data that it wants from that file!

Perhaps you should add a filter to only return the files you're interested in.

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

Upvotes: 2

Related Questions