forked from bunchesofdonald/photohash
-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
652 additions
and
591 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[*] | ||
indent_style = tab | ||
indent_size = 2 | ||
[*.yml] | ||
indent_style = space | ||
[*.py] | ||
indent_size = 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
include README.rst | ||
include *.txt | ||
include LICENSE | ||
include *.txt | ||
include LICENSE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,26 @@ | ||
import imagehash | ||
from PIL import Image | ||
|
||
import imagehash | ||
|
||
SAVE_IMAGES = False | ||
|
||
# Load image | ||
full_image = Image.open("../tests/data/peppers.png") | ||
full_image = Image.open('../tests/data/peppers.png') | ||
width, height = full_image.size | ||
# Hash it | ||
full_hash = imagehash.crop_resistant_hash(full_image) | ||
|
||
# Crop it | ||
for x in range(5, 50, 5): | ||
start = x/100 | ||
end = 1-start | ||
start = x / 100 | ||
end = 1 - start | ||
crop_img = full_image.crop((start * width, start * height, end * width, end * height)) | ||
crop_hash = imagehash.crop_resistant_hash(crop_img) | ||
if SAVE_IMAGES: | ||
crop_img.save("crop_{}.png".format(str(x).zfill(2))) | ||
crop_img.save('crop_{}.png'.format(str(x).zfill(2))) | ||
crop_diff = full_hash.hash_diff(crop_hash) | ||
print( | ||
"Cropped {}% from each side. Hash has {} matching segments with {} total hamming distance".format( | ||
'Cropped {}% from each side. Hash has {} matching segments with {} total hamming distance'.format( | ||
x, crop_diff[0], crop_diff[1] | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,56 @@ | ||
#!/usr/bin/env python | ||
from __future__ import (absolute_import, division, print_function) | ||
from PIL import Image | ||
from __future__ import absolute_import, division, print_function | ||
|
||
import sys | ||
|
||
import numpy as np | ||
from PIL import Image | ||
|
||
import imagehash | ||
|
||
hashfuncs = [ | ||
('ahash', imagehash.average_hash), | ||
('phash', imagehash.phash), | ||
('dhash', imagehash.dhash), | ||
('whash-haar', imagehash.whash), | ||
('whash-db4', lambda img: imagehash.whash(img, mode='db4')), | ||
('colorhash', imagehash.colorhash), | ||
('ahash', imagehash.average_hash), | ||
('phash', imagehash.phash), | ||
('dhash', imagehash.dhash), | ||
('whash-haar', imagehash.whash), | ||
('whash-db4', lambda img: imagehash.whash(img, mode='db4')), | ||
('colorhash', imagehash.colorhash), | ||
] | ||
|
||
|
||
def alpharemover(image): | ||
if image.mode != 'RGBA': | ||
return image | ||
canvas = Image.new('RGBA', image.size, (255,255,255,255)) | ||
canvas.paste(image, mask=image) | ||
return canvas.convert('RGB') | ||
if image.mode != 'RGBA': | ||
return image | ||
canvas = Image.new('RGBA', image.size, (255, 255, 255, 255)) | ||
canvas.paste(image, mask=image) | ||
return canvas.convert('RGB') | ||
|
||
|
||
def image_loader(hashfunc, hash_size=8): | ||
def function(path): | ||
image = alpharemover(Image.open(path)) | ||
return hashfunc(image) | ||
return function | ||
def function(path): | ||
image = alpharemover(Image.open(path)) | ||
return hashfunc(image) | ||
return function | ||
|
||
|
||
def with_ztransform_preprocess(hashfunc, hash_size=8): | ||
def function(path): | ||
image = alpharemover(Image.open(path)) | ||
image = image.convert("L").resize((hash_size, hash_size), Image.ANTIALIAS) | ||
data = image.getdata() | ||
quantiles = np.arange(100) | ||
quantiles_values = np.percentile(data, quantiles) | ||
zdata = (np.interp(data, quantiles_values, quantiles) / 100 * 255).astype(np.uint8) | ||
image.putdata(zdata) | ||
return hashfunc(image) | ||
return function | ||
def function(path): | ||
image = alpharemover(Image.open(path)) | ||
image = image.convert('L').resize((hash_size, hash_size), Image.ANTIALIAS) | ||
data = image.getdata() | ||
quantiles = np.arange(100) | ||
quantiles_values = np.percentile(data, quantiles) | ||
zdata = (np.interp(data, quantiles_values, quantiles) / 100 * 255).astype(np.uint8) | ||
image.putdata(zdata) | ||
return hashfunc(image) | ||
return function | ||
|
||
|
||
hashfuncopeners = [(name, image_loader(func)) for name, func in hashfuncs] | ||
hashfuncopeners += [(name + '-z', with_ztransform_preprocess(func)) for name, func in hashfuncs if name != 'colorhash'] | ||
|
||
files = sys.argv[1:] | ||
for path in files: | ||
hashes = [str(hashfuncopener(path)) for name, hashfuncopener in hashfuncopeners] | ||
print(path, ' '.join(hashes)) | ||
#print(path, colorhash(path)) | ||
|
||
|
||
|
||
hashes = [str(hashfuncopener(path)) for name, hashfuncopener in hashfuncopeners] | ||
print(path, ' '.join(hashes)) | ||
# print(path, colorhash(path)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.