本文整理匯總了Python中imagehash.dhash方法的典型用法代碼示例。如果您正苦於以下問題:Python imagehash.dhash方法的具體用法?Python imagehash.dhash怎麽用?Python imagehash.dhash使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類imagehash
的用法示例。
在下文中一共展示了imagehash.dhash方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: import imagehash [as 別名]
# 或者: from imagehash import dhash [as 別名]
def __init__(self,
use_hash = 'dhash',
hash_size = 15,
patch_size = 2,
max_patches = 512,
):
if use_hash == 'dhash':
self.hash_func = imagehash.dhash
elif use_hash == 'phash':
self.hash_func = imagehash.phash
else:
self.hash_func = use_hash
self.hash_size = int(hash_size)
self.patch_size = int(patch_size)
self.max_patches = int(max_patches)
示例2: run
# 需要導入模塊: import imagehash [as 別名]
# 或者: from imagehash import dhash [as 別名]
def run(self):
date_path = self.search['date_path']
files = sorted(os.listdir('data/%s/media' % date_path))
hashes = {}
matches = []
g = nx.Graph()
update_block_size = get_block_size(len(files), 5)
for i in range(len(files)):
f = files[i]
fn = 'data/%s/media/%s' % (date_path, f)
ahash = imagehash.average_hash(Image.open(fn))
dhash = imagehash.dhash(Image.open(fn))
phash = imagehash.phash(Image.open(fn))
hashes[f] = {'ahash': ahash, 'dhash': dhash, 'phash': phash}
for j in range(0, i):
f2name = files[j]
f2 = hashes[f2name]
sumhash = sum([ahash - f2['ahash'],
dhash - f2['dhash'],
phash - f2['phash']])
# FIXME: 40 is a hard-coded arbitrary (eyeballed) threshold
if sumhash <= 40:
matches.append([f, files[j],
ahash - f2['ahash'],
dhash - f2['dhash'],
phash - f2['phash'],
sumhash])
g.add_edge(f, f2name)
if i % update_block_size == 0:
self.update_job(
date_path=self.search['date_path'],
status="STARTED: %s - %s/%s" %
(self.task_family, i, len(files))
)
with self.output().open('w') as fp_graph:
components = list(nx.connected_components(g))
# Note: sets are not JSON serializable
d = []
for s in components:
d.append(list(s))
json.dump(d, fp_graph, indent=2)
示例3: getImageHash
# 需要導入模塊: import imagehash [as 別名]
# 或者: from imagehash import dhash [as 別名]
def getImageHash(img):
io = Image.open(img)
hash1 = imagehash.average_hash(io)
hash2 = imagehash.phash(io)
hash3 = imagehash.dhash(io)
return hash1, hash2, hash3
示例4: deduplicate_images
# 需要導入模塊: import imagehash [as 別名]
# 或者: from imagehash import dhash [as 別名]
def deduplicate_images(self, userpath, hashfunc=imagehash.average_hash):
"""
Remove duplicate images from a path
:userpath: path of the image files
:hashfunc: type of image hashing method
"""
def is_image(filename):
img_ext = [".jpg", ".png", ".gif", ".bmp", ".gif"]
f = filename.lower()
return any(f.endswith(ext) for ext in img_ext)
"""
Available hashs functions:
ahash: Average hash
phash: Perceptual hash
dhash: Difference hash
whash-haar: Haar wavelet hash
whash-db4: Daubechies wavelet hash
"""
dd_img_set = []
image_filenames = [os.path.join(userpath, path) for path in os.listdir(userpath) if is_image(path)]
images = {}
for img in sorted(image_filenames):
hash = hashfunc(Image.open(img))
images[hash] = images.get(hash, []) + [img]
for k, img_list in six.iteritems(images):
dd_img_set.append(os.path.basename(img_list[0]))
dd_img_set.sort()
return dd_img_set
示例5: run
# 需要導入模塊: import imagehash [as 別名]
# 或者: from imagehash import dhash [as 別名]
def run(self):
"""Creates a new key in the report dict for
the deuplicated screenshots.
"""
self.key = "deduplicated_shots"
shots = []
hashmethod = "whash-db4"
if hashmethod == 'ahash':
hashfunc = imagehash.average_hash
elif hashmethod == 'phash':
hashfunc = imagehash.phash
elif hashmethod == 'dhash':
hashfunc = imagehash.dhash
elif hashmethod == 'whash-haar':
hashfunc = imagehash.whash
elif hashmethod == 'whash-db4':
hashfunc = lambda img: imagehash.whash(img, mode='db4')
shots_path = os.path.join(self.analysis_path, "shots")
if os.path.exists(shots_path):
screenshots = self.deduplicate_images(userpath=shots_path, hashfunc=hashfunc)
for screenshot in screenshots:
shots.append(screenshot.replace(".jpg",""))
return shots
示例6: compare_imgs
# 需要導入模塊: import imagehash [as 別名]
# 或者: from imagehash import dhash [as 別名]
def compare_imgs(img, truth_filename, do_assert=True):
"""
PROTIP: run the following to re-generate the test images:
REGENERATE_TEST_IMAGES=1 pytest mujoco_py/tests/test_modder.py
Note: do this in Docker so that images will work for testing.
"""
assert isinstance(truth_filename, str)
truth_filename = join(TEST_ASSET_DIR_PATH, truth_filename)
if os.getenv('REGENERATE_TEST_IMAGES'):
if exists(truth_filename):
pre_path, ext = splitext(truth_filename)
backup_path = "%s_old%s" % (pre_path, ext)
move(truth_filename, backup_path)
save_test_image(truth_filename, img)
return 0
true_img = np.asarray(Image.open(truth_filename))
assert img.shape == true_img.shape
hash0 = imagehash.dhash(Image.fromarray(img))
hash1 = imagehash.dhash(Image.fromarray(true_img))
diff = np.sum(hash0.hash != hash1.hash)
if diff != 0:
# If the assert fails, the best way to investigate is to run
# pytest for the particular test. For example,
#
# pytest -k test_something_something path/to/test.py
save_test_image("/tmp/img.png", img)
save_test_image("/tmp/true_img.png", true_img)
save_test_image("/tmp/diff_img.png", img - true_img)
if do_assert:
assert diff <= 1
return diff