Skip to content

Instantly share code, notes, and snippets.

@einaros
Created August 17, 2018 14:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save einaros/a9f4d0d5f0f7f69cb70b0f005fc9ae29 to your computer and use it in GitHub Desktop.
Save einaros/a9f4d0d5f0f7f69cb70b0f005fc9ae29 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import imagehash
import argparse
from PIL import Image
def main():
parser = argparse.ArgumentParser()
parser.add_argument('path', type=str, nargs='+', help='')
parser.add_argument('-a', '--algorithm', dest='algorithm', default='phash')
parser.add_argument('-i', '--int', dest='int', action='store_true')
parser.add_argument('-b', '--bit', dest='bit', action='store_true')
args = parser.parse_args()
paths = args.path
algo = args.algorithm.lower()
if len(paths) == 1:
path = paths[0]
if algo == 'phash':
result = imagehash.phash(Image.open(path))
elif algo == 'dhash':
result = imagehash.dhash(Image.open(path))
elif algo == 'whash':
result = imagehash.whash(Image.open(path))
elif algo == 'avg':
result = imagehash.average_hash(Image.open(path))
else:
path_a, path_b, *_ = paths
if algo == 'phash':
result = imagehash.phash(Image.open(path_a)) - imagehash.phash(Image.open(path_b))
elif algo == 'dhash':
result = imagehash.dhash(Image.open(path_a)) - imagehash.dhash(Image.open(path_b))
elif algo == 'whash':
result = imagehash.whash(Image.open(path_a)) - imagehash.whash(Image.open(path_b))
elif algo == 'avg':
result = imagehash.average_hash(Image.open(path_a)) - imagehash.average_hash(Image.open(path_b))
if args.bit:
print('{0:b}'.format(int(str(result), 16)))
elif args.int:
print(int(str(result), 16))
else:
print(result)
if __name__ == '__main__':
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment