|
| 1 | +#!E:\网络与新媒体\github\pythonlearning\Scripts\python.exe |
| 2 | +from __future__ import absolute_import, division, print_function |
| 3 | + |
| 4 | +from PIL import Image |
| 5 | + |
| 6 | +import imagehash |
| 7 | + |
| 8 | +""" |
| 9 | +Demo of hashing |
| 10 | +""" |
| 11 | + |
| 12 | + |
| 13 | +def find_similar_images(userpaths, hashfunc=imagehash.average_hash): |
| 14 | + def is_image(filename): |
| 15 | + f = filename.lower() |
| 16 | + return f.endswith('.png') or f.endswith('.jpg') or \ |
| 17 | + f.endswith('.jpeg') or f.endswith('.bmp') or \ |
| 18 | + f.endswith('.gif') or '.jpg' in f or f.endswith('.svg') |
| 19 | + |
| 20 | + image_filenames = [] |
| 21 | + for userpath in userpaths: |
| 22 | + image_filenames += [os.path.join(userpath, path) for path in os.listdir(userpath) if is_image(path)] |
| 23 | + images = {} |
| 24 | + for img in sorted(image_filenames): |
| 25 | + try: |
| 26 | + hash = hashfunc(Image.open(img)) |
| 27 | + except Exception as e: |
| 28 | + print('Problem:', e, 'with', img) |
| 29 | + continue |
| 30 | + if hash in images: |
| 31 | + print(img, ' already exists as', ' '.join(images[hash])) |
| 32 | + if 'dupPictures' in img: |
| 33 | + print('rm -v', img) |
| 34 | + images[hash] = images.get(hash, []) + [img] |
| 35 | + |
| 36 | + # for k, img_list in six.iteritems(images): |
| 37 | + # if len(img_list) > 1: |
| 38 | + # print(" ".join(img_list)) |
| 39 | + |
| 40 | + |
| 41 | +if __name__ == '__main__': # noqa: C901 |
| 42 | + import os |
| 43 | + import sys |
| 44 | + |
| 45 | + def usage(): |
| 46 | + sys.stderr.write("""SYNOPSIS: %s [ahash|phash|dhash|...] [<directory>] |
| 47 | +
|
| 48 | +Identifies similar images in the directory. |
| 49 | +
|
| 50 | +Method: |
| 51 | + ahash: Average hash |
| 52 | + phash: Perceptual hash |
| 53 | + dhash: Difference hash |
| 54 | + whash-haar: Haar wavelet hash |
| 55 | + whash-db4: Daubechies wavelet hash |
| 56 | + colorhash: HSV color hash |
| 57 | + crop-resistant: Crop-resistant hash |
| 58 | +
|
| 59 | +(C) Johannes Buchner, 2013-2017 |
| 60 | +""" % sys.argv[0]) |
| 61 | + sys.exit(1) |
| 62 | + |
| 63 | + hashmethod = sys.argv[1] if len(sys.argv) > 1 else usage() |
| 64 | + if hashmethod == 'ahash': |
| 65 | + hashfunc = imagehash.average_hash |
| 66 | + elif hashmethod == 'phash': |
| 67 | + hashfunc = imagehash.phash |
| 68 | + elif hashmethod == 'dhash': |
| 69 | + hashfunc = imagehash.dhash |
| 70 | + elif hashmethod == 'whash-haar': |
| 71 | + hashfunc = imagehash.whash |
| 72 | + elif hashmethod == 'whash-db4': |
| 73 | + def hashfunc(img): |
| 74 | + return imagehash.whash(img, mode='db4') |
| 75 | + elif hashmethod == 'colorhash': |
| 76 | + hashfunc = imagehash.colorhash |
| 77 | + elif hashmethod == 'crop-resistant': |
| 78 | + hashfunc = imagehash.crop_resistant_hash |
| 79 | + else: |
| 80 | + usage() |
| 81 | + userpaths = sys.argv[2:] if len(sys.argv) > 2 else '.' |
| 82 | + find_similar_images(userpaths=userpaths, hashfunc=hashfunc) |
0 commit comments