Image-Hash

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

  my $p = $ihash->phash();

  print "$a\n$b\n$p\n";



=head1 DESCRIPTION

Image::Hash allows you to calculate the average hash, difference hash and perception hash an image.

Depending on what is available on your system Image::Hash will use GD, Image::Magick or Imager to interact with your image.



=head1 CONSTRUCTOR METHODS

  my $ihash = Image::Hash->new($image [, $module ]);
  
The first argument is a scalar with a binary representation of an image.

You may also optionally specify a second argument of "GD", "ImageMagick" or "Imager" to force Image::Hash to use the specific image module when it interacts with the image.

README  view on Meta::CPAN

  print STDOUT $ihash->reducedimage();
 
 Returns the reduced image that will be used by the hash functions.
 
=head1 EXAMPLES

Please see the C<eg/> directory for further examples.

=head1 BUGS

Image::Hash support different back ends (GD, Image::Magick or Imager), but because the different back ends work slightly different they will not produce the same hash for the same image. More info is available at https://github.com/runarbu/PerlImageH...

=head1 AUTHOR

    Runar Buvik
    CPAN ID: RUNARB
    runarb@gmail.com
    http://www.runarb.com

=head1 Git

lib/Image/Hash.pm  view on Meta::CPAN

  my $p = $ihash->phash();

  print "$a\n$b\n$p\n";



=head1 DESCRIPTION

Image::Hash allows you to calculate the average hash, difference hash and perception hash an image.

Depending on what is available on your system Image::Hash will use GD, Image::Magick or Imager to interact with your image.



=head1 CONSTRUCTOR METHODS

  my $ihash = Image::Hash->new($image [, $module ]);
  
The first argument is a scalar with a binary representation of an image.

You may also optionally specify a second argument of "GD", "ImageMagick" or "Imager" to force Image::Hash to use the specific image module when it interacts with the image.

lib/Image/Hash.pm  view on Meta::CPAN

	bless( $self, $class );
	
	$self->{'image'} = shift;
	$self->{'module'} = shift;
	
	if ($self->{'module'}) {
		# Try to load the image handler the user asked for
		if ($self->{'module'} eq "GD") {
			require GD;
		}
		elsif ($self->{'module'} eq "ImageMagick" || $self->{'module'} eq "Image::Magick") {
			require Image::Magick;
			$self->{'module'} = 'ImageMagick';
		}
		elsif ($self->{'module'} eq "Imager") {
			require Imager;
		}
		else {
			croak("Unknown mudule: '" . $self->{'module'} . "'. Please use either GD, ImageMagick or Imager as module.");
		}
	}
	else {
		# Try to load GD, ImageMagic or Imager
		if (eval 'require GD') {
			$self->{'module'} = "GD";
		}
		elsif (eval 'require Image::Magick') {
			$self->{'module'} = "ImageMagick";
		}
		elsif (eval 'require Imager') {
			$self->{'module'} = "Imager";
		}
		else {
			croak("No image maudule avalibal. Can't load  GD, ImageMagic or Imager.");
		}
	}
	

lib/Image/Hash.pm  view on Meta::CPAN

		$self->{'im'} = GD::Image->new( $self->{'image'} );
		if (not defined $self->{'im'}) {
			carp("Can't make image from this value");
			return undef;
		}
		$self->{'reduse'} = \&reduse_GD;
		$self->{'pixels'} = \&pixels_GD;
		$self->{'blob'}   = \&blob_GD;
	}
	elsif ($self->{'module'} eq 'ImageMagick') {
		$self->{'im'} = Image::Magick->new();
		my $ret = $self->{'im'}->BlobToImage( $self->{'image'} );
		if ($ret == 0) {
			carp("Can't make image from this value");
			return undef;
		}
		$self->{'reduse'} = \&reduse_ImageMagick;
		$self->{'pixels'} = \&pixels_ImageMagick;
		$self->{'blob'}   = \&blob_ImageMagick;

	}

lib/Image/Hash.pm  view on Meta::CPAN


	$dest->copyResampled($self->{ $opt{'im'} },
		0, 0, 		# (destX, destY)
		0, 0, 		# (srcX,  srxY )
		$xs, $ys, 	# (destX, destY)
		$self->{ $opt{'im'} }->width, $self->{ $opt{'im'} }->height
	);
	$self->{ $opt{'im'} } = $dest;
}

# Reduse the size of an image using Image::Magick
sub reduse_ImageMagick {
	my ($self, %opt) = @_;
	$self->{ $opt{'im'} } = $self->{'im'};

    $self->{ $opt{'im'} }->Set(antialias=>'True');
    $self->{ $opt{'im'} }->Resize($opt{'geometry'});
}

# Reduse the size of an image using Imager
sub reduse_Imager {

lib/Image/Hash.pm  view on Meta::CPAN

}


# Return the image as a blob using GD
sub blob_GD {
        my ($self, %opt) = @_;

	return $self->{ $opt{'im'} }->png;
}

# Return the image as a blob using Image::Magick
sub blob_ImageMagick {
        my ($self, %opt) = @_;

	my $blobs = $self->{ $opt{'im'} }->ImageToBlob(magick => 'png');

	return $blobs;
}

# Return the image as a blob using Imager
sub blob_Imager {

lib/Image/Hash.pm  view on Meta::CPAN

					my $color = $self->{ $opt{'im'} }->getPixel($x, $y);
					my ($red, $green, $blue) = $self->{ $opt{'im'} }->rgb($color);
					my $grey = $red*0.3 + $green*0.59 + $blue*0.11;
					push(@pixels, $grey);
			}
	}
	
	return @pixels;
}

# Return the pixel values for an image when using Image::Magick
sub pixels_ImageMagick {
	my ($self, %opt) = @_;
	my ($xs, $ys) = split(/x/, $opt{'geometry'});
	
	my @pixels;
	for(my $y=0; $y<$ys;$y++) {
			for(my $x=0; $x<$xs;$x++) {
					my @pixel = $self->{ $opt{'im'} }->GetPixel(x=>$x,y=>$y,normalize => 0);
					my $grey = $pixel[0]*0.3 + $pixel[1]*0.59 + $pixel[2]*0.11;
					push(@pixels, $grey);

lib/Image/Hash.pm  view on Meta::CPAN


	$self->{'blob'}->($self, %opt );
}

=head1 EXAMPLES

Please see the C<eg/> directory for further examples.

=head1 BUGS

Image::Hash support different back ends (GD, Image::Magick or Imager), but because the different back ends work slightly different they will not produce the same hash for the same image. More info is available at https://github.com/runarbu/PerlImageH...

=head1 AUTHOR

    Runar Buvik
    CPAN ID: RUNARB
    runarb@gmail.com
    http://www.runarb.com

=head1 Git

t/002_lengths.t  view on Meta::CPAN

# t/002_lengths.t - check output lengths

use lib "lib";

use strict;
use warnings;

use Test::More tests => 28;
use File::Slurp;

my @modules =  ('GD', 'Image::Magick', 'Imager' );

BEGIN { use_ok( 'Image::Hash' ); }


# Test to see what modules are installed. Have to do it this way instead of in a loop 
# because "require GD" is not the same as "require 'GD'"
my %have;
eval { require GD; }; 
if (!$@) {$have{'GD'} = 1;}

eval { require Image::Magick};
if (!$@) {$have{'Image::Magick'} = 1;}

eval { require Imager; require Imager::File::JPEG};
if (!$@) {$have{'Imager'} = 1;}


# Load the test image
my $image = read_file( 'eg/images/FishEyeViewofAtlantis.jpg', binmode => ':raw' ) ;

# Test individual hash functions
# aHash



( run in 0.764 second using v1.01-cache-2.11-cpan-beeb90c9504 )