def compare_image(self, imagepath1, imagepath2): img1 = Image.open(imagepath1) img2 = Image.open(imagepath2) ''' return imagehash.dhash(img1) - imagehash.dhash(img2) return imagehash.average_hash(img1) - imagehash.average_hash(img2) return imagehash.phash(img1) - imagehash.phash(img2) return imagehash.whash(img1) - imagehash.whash(img2) ''' return imagehash.dhash_vertical(img1) - imagehash.dhash_vertical(img2)
def hash_picture(self, curr_picture: picture_class.Picture): try: if self.conf.ALGO == configuration.ALGO_TYPE.A_HASH: # Average target_hash = imagehash.average_hash( Image.open(curr_picture.path)) elif self.conf.ALGO == configuration.ALGO_TYPE.P_HASH: # Perception target_hash = imagehash.phash(Image.open(curr_picture.path)) elif self.conf.ALGO == configuration.ALGO_TYPE.P_HASH_SIMPLE: # Perception - simple target_hash = imagehash.phash_simple( Image.open(curr_picture.path)) elif self.conf.ALGO == configuration.ALGO_TYPE.D_HASH: # D target_hash = imagehash.dhash(Image.open(curr_picture.path)) elif self.conf.ALGO == configuration.ALGO_TYPE.D_HASH_VERTICAL: # D-vertical target_hash = imagehash.dhash_vertical( Image.open(curr_picture.path)) elif self.conf.ALGO == configuration.ALGO_TYPE.W_HASH: # Wavelet target_hash = imagehash.whash(Image.open(curr_picture.path)) else: raise Exception('IMAGEHASH WRAPPER : HASH_CHOICE NOT CORRECT') # TO NORMALIZE : https://fullstackml.com/wavelet-image-hash-in-python-3504fdd282b5 curr_picture.hash = target_hash except Exception as e: self.logger.error("Error during hashing : " + str(e)) return curr_picture
def hash_func(self, x): ''''Hash one image and return hash''' x = self.process_for_hash(x) if self.hash_name == "AverageHash": hash_value = imagehash.average_hash(x, hash_size=8, mean=np.mean) elif self.hash_name == "Phash": hash_value = imagehash.phash(x, hash_size=8, highfreq_factor=4) elif self.hash_name == "PhashSimple": hash_value = imagehash.phash_simple(x, hash_size=8, highfreq_factor=4) elif self.hash_name == "DhashH": hash_value = imagehash.dhash(x) elif self.hash_name == "DhashV": hash_value = imagehash.dhash_vertical(x) elif self.hash_name == "Whash": hash_value = imagehash.whash(x, hash_size=8, image_scale=None, mode='haar', remove_max_haar_ll=True) elif self.hash_name == "ColorHash": hash_value = imagehash.colorhash(x, binbits=3) elif self.hash_name == "CropResistantHash": # does not work yet hash_value = imagehash.crop_resistant_hash(x, hash_func=None, limit_segments=None, segment_threshold=128, min_segment_size=500, segmentation_image_size=300 ) else: raise NotImplementedError(f"Hash Name -- {self.hash_name} -- Unknown") return str(hash_value)
def db_add_image(file_name: str) -> bool: image = Image.open(file_name) return db_add(file_name, str(imagehash.average_hash(image)), str(imagehash.phash(image)), str(imagehash.phash_simple(image)), str(imagehash.dhash(image)), str(imagehash.dhash_vertical(image)), str(imagehash.whash(image)), str(imagehash.colorhash(image)))
def frame_perceptive_hash(frame): im = Image.fromarray(frame) ah = imhash.average_hash(im).hash.astype(float) ph = imhash.phash(im).hash.astype(float) wh = imhash.whash(im).hash.astype(float) dh_h = imhash.dhash(im).hash.astype(float) dh_v = imhash.dhash_vertical(im).hash.astype(float) return (ah, ph, wh, dh_h, dh_v)
def Hashing(filename): phash = int(str(imagehash.phash(Image.open(filename))),16) ahash = int(str(imagehash.average_hash(Image.open(filename))),16) phashimple = int(str(imagehash.phash_simple(Image.open(filename))),16) dhash = int(str(imagehash.dhash(Image.open(filename))),16) dhashv = int(str(imagehash.dhash_vertical(Image.open(filename))),16) wash = int(str(imagehash.whash(Image.open(filename))),16) return phash,ahash,phashimple,dhash,dhashv,wash
def getHash(img): normal = Image.open(img).convert('L') crop = normal.crop((25, 37, 195, 150)) ahash = str(imagehash.average_hash(crop)) phash = str(imagehash.phash(crop)) psimplehash = str(imagehash.phash_simple(crop)) dhash = str(imagehash.dhash(crop)) vertdhash = str(imagehash.dhash_vertical(crop)) whash = str(imagehash.whash(crop)) return ahash, phash, psimplehash, dhash, vertdhash, whash
def getHash(img): normal = Image.open(img).convert('L') crop=normal.crop((25,37,195,150)) ahash = str(imagehash.average_hash(crop)) phash = str(imagehash.phash(crop)) psimplehash = str(imagehash.phash_simple(crop)) dhash = str(imagehash.dhash(crop)) vertdhash = str(imagehash.dhash_vertical(crop)) whash = str(imagehash.whash(crop)) return ahash,phash,psimplehash,dhash,vertdhash,whash
def getHash(img): size = 223, 310 normal = Image.open(img).convert('L') normal = normal.resize(size, Image.ANTIALIAS) crop = normal.crop((25, 37, 195, 150)) ahash = str(imagehash.average_hash(crop)) phash = str(imagehash.phash(crop)) psimplehash = str(imagehash.phash_simple(crop)) dhash = str(imagehash.dhash(crop)) vertdhash = str(imagehash.dhash_vertical(crop)) whash = str(imagehash.whash(crop)) return ahash, phash, psimplehash, phash, vertdhash, whash
def image_hash(image_file="../gif/test/img/0.png", hash_size=8): im = Image.open(image_file) a_hash_value = imagehash.average_hash(im, hash_size) d_hash_value = imagehash.dhash(im, hash_size) d_hash_value_h = imagehash.dhash_vertical(im, hash_size) p_hash_value = imagehash.phash(im, hash_size) p_hash_value_s = imagehash.phash_simple(im, hash_size) w_hash_value = imagehash.whash(im, hash_size) return [ a_hash_value, d_hash_value, d_hash_value_h, p_hash_value, p_hash_value_s, w_hash_value ]
def getHash(img): size = 223,310 normal = Image.open(img).convert('L') normal = normal.resize(size, Image.ANTIALIAS) crop=normal.crop((25,37,195,150)) ahash = str(imagehash.average_hash(crop)) phash = str(imagehash.phash(crop)) psimplehash = str(imagehash.phash_simple(crop)) dhash = str(imagehash.dhash(crop)) vertdhash = str(imagehash.dhash_vertical(crop)) whash = str(imagehash.whash(crop)) return ahash,phash,psimplehash,phash,vertdhash,whash
def hash_picture(self, curr_picture): """ Hash a picture and returns the hash value :param curr_picture: the picture to hash :return: the hashed version of the picture """ answer = {} self.logger.info("Hashing picture ... ") # Convert bytes in PIL image pil_picture = Image.open(io.BytesIO(curr_picture)) self.logger.debug( f"Picture converted to PIL Image {type(pil_picture)}") # DEBUG # pil_picture.save('/home/user/Desktop/debug_pil.bmp') try: # Note : @image must be a PIL instance. if self.fe_conf.A_HASH.get("is_enabled", False): self.logger.debug("A-HASH ... ") answer["A_HASH"] = self.check_null_hash( imagehash.average_hash(pil_picture)) if self.fe_conf.P_HASH.get("is_enabled", False): self.logger.debug("P_HASH ... ") answer["P_HASH"] = self.check_null_hash( imagehash.phash(pil_picture)) if self.fe_conf.P_HASH_SIMPLE.get("is_enabled", False): self.logger.debug("P_HASH_SIMPLE ... ") answer["P_HASH_SIMPLE"] = self.check_null_hash( imagehash.phash_simple(pil_picture)) if self.fe_conf.D_HASH.get("is_enabled", False): self.logger.debug("D_HASH ... ") answer["D_HASH"] = self.check_null_hash( imagehash.dhash(pil_picture)) if self.fe_conf.D_HASH_VERTICAL.get("is_enabled", False): self.logger.debug("D_HASH_VERTICAL ... ") answer["D_HASH_VERTICAL"] = self.check_null_hash( imagehash.dhash_vertical(pil_picture)) if self.fe_conf.W_HASH.get("is_enabled", False): self.logger.debug("W_HASH ... ") answer["W_HASH"] = self.check_null_hash( imagehash.whash(pil_picture)) if self.fe_conf.TLSH.get("is_enabled", False): self.logger.debug("TLSH ... ") answer["TLSH"] = self.check_null_hash(tlsh.hash(curr_picture)) except Exception as e: self.logger.error("Error during hashing : " + str(e)) return answer
def set_image_hashes(post: Post, hash_size: int = 16) -> Post: log.debug('%s - Hashing image post %s', os.getpid(), post.post_id) try: img = generate_img_by_url(post.url) except ImageConversioinException as e: raise try: dhash_h = imagehash.dhash(img, hash_size=hash_size) dhash_v = imagehash.dhash_vertical(img, hash_size=hash_size) ahash = imagehash.average_hash(img, hash_size=hash_size) post.dhash_h = str(dhash_h) post.dhash_v = str(dhash_v) post.ahash = str(ahash) except Exception as e: # TODO: Specific exception log.exception('Error creating hash', exc_info=True) raise return post
def get_image_hashes(url: Text, hash_size: int = 16) -> Dict: result = { 'dhash_h': None, 'dhash_v': None, 'ahash': None, } log.debug('Hashing image %s', url) img = generate_img_by_url(url) try: dhash_h = imagehash.dhash(img, hash_size=hash_size) dhash_v = imagehash.dhash_vertical(img, hash_size=hash_size) ahash = imagehash.average_hash(img, hash_size=hash_size) result['dhash_h'] = str(dhash_h) result['dhash_v'] = str(dhash_v) result['ahash'] = str(ahash) except Exception as e: # TODO: Specific exception log.exception('Error creating hash', exc_info=True) raise return result
def calculateHash(hashAlgorithmString, imageHashDict, imagePath, image, hashSizeInt): if hashAlgorithmString == 'dhash': imageHashDict[imagePath] = dhash(image, hash_size=hashSizeInt) elif hashAlgorithmString == 'dhash_vertical': imageHashDict[imagePath] = dhash_vertical(image, hash_size=hashSizeInt) elif hashAlgorithmString == 'ahash': imageHashDict[imagePath] = average_hash(image, hash_size=hashSizeInt) elif hashAlgorithmString == 'phash': imageHashDict[imagePath] = phash(image, hash_size=hashSizeInt) elif hashAlgorithmString == 'phash_simple': imageHashDict[imagePath] = phash_simple(image, hash_size=hashSizeInt) elif hashAlgorithmString == 'whash_haar': imageHashDict[imagePath] = whash(image, hash_size=hashSizeInt) elif hashAlgorithmString == 'whash_db4': imageHashDict[imagePath] = whash(image, hash_size=hashSizeInt, mode='db4') elif hashAlgorithmString == None: imageHashDict[imagePath] = phash(image, hash_size=hashSizeInt) return imageHashDict
def dhash_v(self, img): return imagehash.dhash_vertical(img, hash_size=self.hash_size)
def hash_image(image, algorithm=None): """ Hashes a given image image: Can be an URL, a path, a base64 encoded string or a PIL.Image.Image instance Erina Project — 2020\n © Anime no Sekai """ result = None has_url = False url = None log("ErinaHash", "Hashing an image...") # Needs to be a PIL instance if isfile(str(image)): image = Image.open(image) elif isinstance(image, Image.Image): image = image else: try: if base64.b64decode(str(image), validate=True): image = Image.open(BytesIO(base64.b64decode(str(image)))) else: raise ValueError("b64decode returned an empty string") except: try: url = image image = Image.open( BytesIO(requests.get(str(image)).content) ) # Open the downloaded image as a PIL Image instance has_url = True except: return HashingError( "INVALID_IMAGE_TYPE", "We couldn't convert the given image to a PIL.Image.Image instance" ) if algorithm is None: algorithm = str(config.Hash.algorithm) algorithm = str(algorithm).lower().replace(" ", "") if algorithm in ['ahash', 'a', 'averagehash', 'average']: result = imagehash.average_hash(image) elif algorithm in ['chash', 'c']: result = imagehash.colorhash(image) elif algorithm in ['dhash', 'd']: result = imagehash.dhash(image) elif algorithm in ['phash', 'p', 'perceptual', 'perceptualhash']: result = imagehash.phash(image) elif algorithm in ['wHash', 'w']: result = imagehash.whash(image) else: algorithm = algorithm.replace("_", "") if algorithm in [ 'dhashvertical', 'dvertical', 'dvert', 'verticald', 'verticaldhash' ]: result = imagehash.dhash_vertical(image) elif algorithm in [ 'phashsimple', 'psimple', 'perceptualsimple', 'simpleperceptual', 'simplep', 'simplephash', 'simpleperceptualhas' ]: result = imagehash.phash_simple(image) else: return HashingError( "INVALID_ALGORITHM", "We couldn't determine the hashing algorithm you wanted to use." ) if has_url: return HashObject(result, image, url) else: return HashObject(result, image)