9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/dhash.rb', line 9
def calculate(file, hash_size = 8)
image = Magick::Image.read(file).first
image = image.quantize(256, Magick::Rec601LumaColorspace, Magick::NoDitherMethod, 8)
image = image.resize!(hash_size + 1, hash_size)
difference = []
hash_size.times do |row|
hash_size.times do |col|
pixel_left = image.pixel_color(col, row).intensity
pixel_right = image.pixel_color(col + 1, row).intensity
difference << (pixel_left > pixel_right)
end
end
difference.map {|d| d ? 1 : 0 }.join('').to_i(2)
end
|