rubyphash

phash ruby, how to access hash


I currently get to work phash (http://www.phash.org) in combination with ruby (https://github.com/toy/pHash/blob/master/lib/phash.rb).

If i use the described examples, it works.

But i want to extract only the hash - but i don't know how to do it

#!/usr/bin/env ruby

require 'rubygems'
require 'phash'

a = Phash::Image.new(ARGV[0])
b = Phash::Image.new(ARGV[1])

puts "a="
#output => #<Phash::ImageHash:0x00000002947080>
puts a.phash

puts "b="
#output same ..=> #<Phash::ImageHash:0x00000002946ef0>
puts b.compute_phash

# works as expected if ARGV[0]=ARGV[1] => 1.0
puts a % b

Solution

  • What you need is the #data method:

    > h = Phash::Image.new(p).phash
    => #<Phash::ImageHash:0x00000002515f70 @data=18314200736077351885>
    > h.class
    => Phash::ImageHash
    > h.methods
    => [:similarity, :data, :length, :nil?, :===, :=~, :!~, :eql?, :hash, :<=>, :class, :singleton_class, :clone, :dup, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :freeze, :frozen?, :to_s, :inspect, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :extend, :display, :method, :public_method, :singleton_method, :define_singleton_method, :object_id, :to_enum, :enum_for, :==, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__]
    > h.data.class
    => Bignum
    > h.data
    => 18314200736077351885
    > h.data.to_s
    => "18314200736077351885"