def verify(userImgSrc, targetImgSrc): user_img = Image.open(userImgSrc) target_img = Image.open(targetImgSrc) user_hash = imagehash.average_hash(user_img) target_hash = imagehash.average_hash(target_img) result = True if user_hash-target_hash <= DIFFERENCE_BAR else False return result
def image_descriptor(image_path, prior=None): mtime = os.path.getmtime(image_path) ctime = os.path.getctime(image_path) if not prior or (not prior.get('modified')): img = Image.open(image_path) result = {'width': img.size[0], 'height': img.size[1], 'created': mtime, 'modified': ctime, # TODO: if results too bad, change hash sizes for more precission? 'aHash': str(imagehash.average_hash(img)), 'pHash': str(imagehash.phash(img)), 'dHash': str(imagehash.dhash(img)), } return result changed = prior['modified'] < mtime img = Image.open(image_path) if changed or not prior["width"]: prior["width"] = img.size[0] if changed or not prior["height"]: prior["height"] = img.size[1] if changed or not prior["aHash"]: prior["aHash"] = str(imagehash.average_hash(img)) if changed or not prior["pHash"]: prior["pHash"] = str(imagehash.phash(img)) if changed or not prior["dHash"]: prior["dHash"] = str(imagehash.dhash(img)) return prior
def compare_images_hash(file1, file2): hash1 = imagehash.average_hash(Image.open(file1)) hash2 = imagehash.average_hash(Image.open(file2)) # measure by feature diffhash = hash1 - hash2 samehash = (hash1 == hash2) return diffhash, samehash
def hash_value(img_fn, htype): img = Image.open(img_fn) if htype == 'a': hval = imagehash.average_hash(img) elif htype == 'p': hval = imagehash.phash(img) elif htype == 'd': hval = imagehash.dhash(img) elif htype == 'w': hval = imagehash.whash(img) else: hval = imagehash.average_hash(img) return hval
def TestCompare(self):#test hashing compasions i1 = grab() i2 = i1.copy() print("Change screen") time.sleep(15) i3 = grab() hash1 = imagehash.average_hash(i1) hash2 = imagehash.average_hash(i2) hash3 = imagehash.average_hash(i3) print("image 1 vs image 2") print(hash1 == hash2) print("image 1 vs image 3") print(hash1 == hash3)
def save_image_info(directory, the_image, filename, file_ext): """Create/update Image object from the image in filename""" the_image.filesize = os.stat(filename).st_size the_image.save() if file_ext not in imagine_core.IMAGE_EXTENSIONS_RAW: try: image = PILImage.open(filename) the_image.image_hash = imagehash.average_hash(image) the_image.width = image.size[0] the_image.height = image.size[1] # Done with image info for now, save the_image.save() except IOError: logger.error('IOError opening ' + filename) if file_ext == 'jpg': save_jpg_exif(the_image, filename) elif file_ext == 'cr2': save_cr2_exif(the_image, filename) else: logger.warning('No supported extension found')
def run(self): files = sorted(os.listdir('data/%s/media' % self.date_path)) hashes = {} matches = [] g = nx.Graph() for i in range(len(files)): f = files[i] fn = 'data/%s/media/%s' % (self.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']]) if sumhash <= 40: matches.append([f, files[j], ahash - f2['ahash'], dhash - f2['dhash'], phash - f2['phash'], sumhash]) g.add_edge(f, f2name) 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)) logging.debug(' - = - = - = GRAPH HERE - = - = - = -') logging.debug(d) json.dump(d, fp_graph, indent=2)
def getHash(imgPath): """Get the hash of an image, and catch exceptions if the image file is corrupted.""" try: return imagehash.average_hash(Image.open(imgPath)) except: return None
def get_image_metadata(config, request): ''' Handle request for an image. ''' try: url = request.GET['url'] except KeyError: raise aiohttp.web.HTTPBadRequest(reason='"url" argument is required.') tor_config = config['Tor'] socks_proxy = SOCKSConnector(tor_config['ip'], int(tor_config['port'])) response = yield from aiohttp.get(url, connector=socks_proxy) content_type = response.headers['Content-type'] if not content_type.startswith('image/'): reason = 'Requested a non-image resource ({}).'.format(content_type) raise aiohttp.web.HTTPBadRequest(reason=reason) image_data = yield from response.read() image_file = io.BytesIO(image_data) image = Image.open(image_file) extra = dict() if content_type in ('image/jpeg', 'image/tiff'): for name, tag in exifread.process_file(io.BytesIO(image_data)).items(): if name.startswith('Image') or name.startswith('MakerNote'): if isinstance(tag.values, (int, str)): extra[name] = tag.values elif isinstance(tag.values, list): if len(tag.values) > 0 and isinstance(tag.values[0], int): extra[name] = tag.values else: extra[name] = ','.join(map(str,tag.values)) else: extra[name] = str(tag) metadata = { 'content_type': content_type, 'extra': extra, 'format': image.format, 'hashes': { 'ahash': str(imagehash.average_hash(image)), 'dhash': str(imagehash.dhash(image)), 'md5': hashlib.md5(image_data).hexdigest(), 'phash': str(imagehash.phash(image)), 'sha1': hashlib.sha1(image_data).hexdigest(), 'sha256': hashlib.sha256(image_data).hexdigest(), }, 'last_modified': response.headers.get('Last-modified', None), 'resolution': { 'width': image.width, 'height': image.height, }, 'size': len(image_data), } return aiohttp.web.Response( headers={'Content-type': 'application/json; charset=utf8'}, body=json.dumps(metadata).encode('utf8'), )
def run(self, task): image = str2image(task.get_file_data) self.results["imghash"]["a_hash"] = str(imagehash.average_hash(image)) self.results["imghash"]["p_hash"] = str(imagehash.phash(image)) self.results["imghash"]["d_hash"] = str(imagehash.dhash(image)) return self.results
def create_from_image(cls, img, url, facebook=None, okcupid=None): cls.objects.create( ahash = imagehash.average_hash(img), phash = imagehash.phash(img), dhash = imagehash.dhash(img), url = url, facebook = facebook, okcupid = okcupid)
def main(): f_hashes = [] files = [ f for f in listdir(PATH) if isfile(join(PATH, f))] for f in files: image_hash = imagehash.average_hash(Image.open(PATH + f)) if image_hash in f_hashes: remove(PATH + f) else: f_hashes.append(image_hash)
def getImageHash(self, driver): """ returns an image hash of the url passed """ #take a screen shot of the browser driver.save_screenshot("screenie.png") #get and return the image hash image_hash = imagehash.average_hash(Image.open('screenie.png')) return image_hash
def checkImage(url): buffer = [] try: data = StringIO(urllib2.urlopen(url).read()) hash = imagehash.average_hash(Image.open(data)) # print hash,url buffer.append((str(hash), url.strip())) data.close() except urllib2.URLError, e: print e
def test_stored_hashes(): img = retrieve_example() hash = imagehash.average_hash(img) stored_hex = str(hash) print('checking if stringified hash is the same') otherhash = imagehash.hex_to_hash(stored_hex) print(otherhash) print(hash == otherhash) assert hash == otherhash assert hash - otherhash == 0
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 get_imagehashes(fp: Fileish, size=FINGERPRINT_SIZE) -> Dict[str, imagehash.ImageHash]: """Calculate perceptual hashes for comparison of identical images""" try: img = pil_image(fp) thumb = img.resize((size, size), PIL.Image.BILINEAR).convert('L') return dict( ahash=imagehash.average_hash(thumb), phash=imagehash.phash(thumb), whash=imagehash.whash(thumb), dhash=imagehash.dhash(thumb), ) except OSError: # corrupt image file probably return {}
def run(self, task): self.task = task image = str2image(task.get_file_data) # Calculate hash. self.results["imghash"]["a_hash"] = str(imagehash.average_hash(image, hash_size=self.HASH_SIZE)) self.results["imghash"]["p_hash"] = str(imagehash.phash(image, hash_size=self.HASH_SIZE)) self.results["imghash"]["d_hash"] = str(imagehash.dhash(image, hash_size=self.HASH_SIZE)) # Get similar images. self.results["similar"]["a_hash"] = self.get_similar_images(self.results["imghash"]["a_hash"], imagehash.average_hash) self.results["similar"]["p_hash"] = self.get_similar_images(self.results["imghash"]["p_hash"], imagehash.phash) self.results["similar"]["d_hash"] = self.get_similar_images(self.results["imghash"]["d_hash"], imagehash.dhash) return self.results
def main(name): master = Tk() master.title(name) photo = ImageTk.PhotoImage(file=name) hsh = imagehash.average_hash(Image.open(name)) canvas = Canvas(master, width=photo.width(), height=photo.height()) canvas.create_image(photo.width() / 2, photo.height() / 2, image=photo) canvas.pack() msg = Message(master, text=str(hsh) + ' ' + name + ' (%d X %d)' % (photo.width(), photo.height()), width=photo.width()) msg.pack() master.bind('<Escape>', lambda x: master.quit()) master.mainloop()
def run(): signatures = get_pickled_signatures() import csv hashes_file = open('image_hashes.csv', 'w') columns = ['image_id', 'script_dhash', 'ahash', 'dhash', 'phash', 'signature'] csv_writer = csv.DictWriter(hashes_file, fieldnames=columns) csv_writer.writeheader() t0 = time() for zip_counter in range(0, 10): filename = '../input/Images_%d.zip' % zip_counter print 'processing %s...' % filename imgzipfile = zipfile.ZipFile(filename) namelist = imgzipfile.namelist() for name in tqdm(namelist): if not name.endswith('.jpg'): continue filename = name.split('/')[-1] img_id = filename[:-4] try: imgdata = imgzipfile.read(name) if len(imgdata) == 0: print '%s is empty' % img_id continue stream = io.BytesIO(imgdata) img = Image.open(stream) ahash = imagehash.average_hash(img) dhash = imagehash.dhash(img) phash = imagehash.phash(img) script_dhash = extract_dhash(img) csv_writer.writerow({'image_id': img_id, 'script_dhash': script_dhash, 'ahash': str(ahash), 'dhash': str(dhash), 'phash': str(phash), 'signature': signatures[int(img_id)]}) except: print 'error with ' + img_id hashes_file.flush() hashes_file.close() print 'took %0.5fm' % ((time() - t0) / 60)
def getImageHash(self, url, call_phantom=True): if call_phantom: self.setUpGetter(url) #get and return the image hash # if no image exists, then the page failed to load if not os.path.isfile("{}-screenie.jpeg".format(self.id)): return -1 try: image_hash = imagehash.average_hash(Image.open('{}-screenie.jpeg'.format(self.id))) self.image_hash = image_hash except Exception as e: print(e) finally: # always remove the old image even if an exception was thrown os.remove('{}-screenie.jpeg'.format(self.id)) #test = True return self.image_hash
def trumpify(filename): inp = filename original = Image.open(inp).convert('RGBA') rects = face_rects(inp) width, height = original.size for overlay_fname in ['hair.png']: overlay = Image.open(overlay_fname).convert('RGBA') for rect in rects: original = overlay_alpha_png(original, overlay, rect) #overlay_alpha_png(original, Image.open('logo.png').convert('RGBA'), (width - 225,height - 50,200,100)) width, height = original.size hashval = imagehash.average_hash(original) print hashval original.save('outs/' + str(hashval) + '.png') return str(hashval) + '.png'
def main(): conn = boto.sqs.connect_to_region("us-east-1") queue_url = "https://sqs.us-east-1.amazonaws.com/452649417432/awseb-e-tmrnmgpqzh-stack-NewSignupQueue-MN33H2HK79KL" if os.path.exists("pass.txt"): with open ("pass.txt", "r") as password_file: password = password_file.readline() else: sys.exit(0) q = Queue(conn, queue_url) while 1: rs = q.get_messages(1) if len(rs)!=0: m = rs[0] data = json.loads(m.get_body()) if 'photoId' in data and 'url' in data: photoid = data['photoId'] print photoid url = data['url'] urllib.urlretrieve(url, "tmp.jpg") hash_value = imagehash.average_hash(Image.open('tmp.jpg')) print(hash_value) cnx = MySQLdb.connect(user='******', passwd = password, host = 'tweetmap.crsarl5br9bw.us-east-1.rds.amazonaws.com', db='tweet') cursor = cnx.cursor() add_hash = ("UPDATE testPhoto SET hash = %s WHERE id = %s") data_hash = (hash_value, photoid) cursor.execute(add_hash, data_hash) cnx.commit() print "insert into db" cursor.close() cnx.close() else: print "no key: photoId and url" q.delete_message(m)
def _insert_meta(self, data_store, comic_id): j = self._get_xkcd_json(comic_id) hash_avg = '' hash_d = '' hash_p = '' if not j: return if j.get('img'): file_name = '/tmp/' + get_random_file_name() try: self.myopener.retrieve(j.get('img'), file_name) hash_avg = imagehash.average_hash(Image.open(file_name)) hash_d = imagehash.dhash(Image.open(file_name)) hash_p = imagehash.phash(Image.open(file_name)) except: pass finally: os.remove(file_name) data_store.insert_xkcd_meta(comic_id, json.dumps(j), str(hash_avg), str(hash_d), str(hash_p))
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)
def parse_item(self, response): item = SearchItem() item['URL'] = response.url title = '' soup = bs(response.body, 'lxml') try: title = soup.title.string except(e): print e item['Title'] = title vis = [] try: texts = soup.findAll(text=True) for i in texts: if i.parent.name in ['style', 'script', '[document]', 'head', 'title']: pass else: i = re.sub('(<!--.*?-->)', '', i, flags=re.MULTILINE) i = re.sub("\s+", ' ', i) vis.append(i) except(e): print e item['Content'] = ' '.join(vis) imgs = [] images = soup.find_all('img', src=True) for i in images: URL = urlparse.urljoin(response.url, i['src']) IMAGE = URL.rsplit('/', 1)[1] EXT = URL.rsplit('.', 1)[1] urllib.urlretrieve(URL, r"/home/sam/Desktop/imagestore/" + IMAGE) hashval = imagehash.average_hash(Image.open(r"/home/sam/Desktop/imagestore/" + IMAGE)) data = [URL, IMAGE, str(hashval)] imgs.append(' :: '.join(data)) item['Images'] = ' || '.join(imgs) return item
def one_step(image): """ One step for hash compute. image: dict """ image = image try: open_image = Image.open(image['path']) #calc imagehash image['aHash'] = imagehash.average_hash(open_image).__str__() image['dHash'] = imagehash.dhash(open_image).__str__() image['pHash'] = imagehash.phash(open_image).__str__() open_image.close() #calc md5sum image['md5sum'] = os.popen('md5sum ' + image['path']).read().split(' ')[0] os.remove(os.path.abspath(image['path'])) return '{0}, {1}, {2}, {3}, {4}\n'.format( image['img_id'], image['aHash'], image['dHash'], image['pHash'], image['md5sum'] ) except: print('Битое изображение: {0}, индекс {1}'.format( image['path'], image['index'] )) #delete file os.remove(os.path.abspath(image['path'])) return None
""" Automatically pressing the matchmaking 'accept' button """ import sys from time import sleep import pyautogui from PIL import Image from imagehash import average_hash from fuzzywuzzy import process SCREEN_WIDTH, SCREEN_HEIGHT = pyautogui.size( ) # width and height of your screen button = average_hash( Image.open('button.png')) # initialise the image of the button BUTTON_WIDTH = 534 # width of the button of the screen BUTTON_HEIGHT = 106 # height of the button of the screen pick_screen = average_hash( Image.open('all_pick.png')) # initialise the image of the pick screen CUTOFF = 5 # allowed difference between the initial image of the button and the one on the screen COOLDOWN = 1 # how often the program checks if the button appeared def auto_accept(): """ Compares a cropped screenshot and the image of the button """ sleep(COOLDOWN) screenshot = average_hash(
def get_hash(req): return imagehash.average_hash(Image.open(BytesIO(req.content)))
async def get_image(url, available_projections, lon, lat, zoom, session, messages): """ Download image (tms tile for coordinate lon,lat on level zoom and calculate image hash Parameters ---------- url : str available_projections : collection lon : float lat : float zoom : int session : ClientSession messages : list Returns ------- ImageHash or None """ tile = list(mercantile.tiles(lon, lat, lon, lat, zooms=zoom))[0] bounds = list(mercantile.bounds(tile)) proj = None if 'EPSG:4326' in available_projections: proj = 'EPSG:4326' elif 'EPSG:3857' in available_projections: proj = 'EPSG:3857' else: for proj in available_projections: try: CRS.from_string(proj) except: continue break if proj is None: messages.append("No projection left: {}".format(available_projections)) return None crs_from = CRS.from_string("epsg:4326") crs_to = CRS.from_string(proj) if not proj == 'EPSG:4326': transformer = Transformer.from_crs(crs_from, crs_to, always_xy=True) bounds = list(transformer.transform(bounds[0], bounds[1])) + \ list(transformer.transform(bounds[2], bounds[3])) # WMS < 1.3.0 assumes x,y coordinate ordering. # WMS 1.3.0 expects coordinate ordering defined in CRS. if crs_to.axis_info[0].direction == 'north' and '=1.3.0' in url: bbox = ",".join(map(str, [bounds[1], bounds[0], bounds[3], bounds[2]])) else: bbox = ",".join(map(str, bounds)) formatted_url = url.format(proj=proj, width=512, height=512, bbox=bbox) messages.append("Image URL: {}".format(formatted_url)) for i in range(3): try: # Downloag image async with session.request(method="GET", url=formatted_url, ssl=False) as response: data = await response.read() img = Image.open(io.BytesIO(data)) img_hash = imagehash.average_hash(img) messages.append("ImageHash: {}".format(img_hash)) return img_hash except Exception as e: messages.append("Could not download image in try {}: {}".format( i, str(e))) await asyncio.sleep(5) return None
def test_average_hash_length(): for hash_size in [8, 20]: print('checking if hash_size=%d is respected' % hash_size) hash = imagehash.average_hash(retrieve_example(), hash_size=hash_size) assert hash.hash.size == hash_size**2, hash.hash.shape
s = int(s) ss = make_ss_time(s) with tempfile.NamedTemporaryFile('wb', suffix='_vthumb.jpg', delete=False) as f: thumbnail_path = os.path.abspath(f.name) try: generate_thumbnail(fpath, thumbnail_path, ss, max_thumbnail_height) thumbnail_paths.append(thumbnail_path) except Exception: pass best_thumbnail_path = None if len(thumbnail_paths) > 0: ahashes = [] for thumbnail_path in thumbnail_paths: image = PIL.Image.open(thumbnail_path) ahashes.append(imagehash.average_hash(image)) distances = [] for i, ahash_i in enumerate(ahashes): distances.append((np.mean([ahash_i-ahash_j for ahash_j in ahashes]), i)) distances.sort() mean_i = distances[0][1] best_thumbnail_path = thumbnail_paths[mean_i] for i, thumbnail_path in enumerate(thumbnail_paths): if i != mean_i: os.remove(thumbnail_path) thumbnail_width = None thumbnail_height = None thumbnail_size = None
def process_image(fpath, max_thumbnail_height=400, run_hashing=True): with open(fpath, 'rb') as f: fdata = f.read() sha_hash = hashlib.sha256(fdata).digest() image = PIL.Image.open(io.BytesIO(fdata)) hashes = {} hashes['average'] = ' ' * 16 hashes['perceptual'] = ' ' * 16 hashes['difference'] = ' ' * 16 if run_hashing: try: hashes['average'] = str(imagehash.average_hash(image)) except Exception: pass try: hashes['perceptual'] = str(imagehash.phash(image)) except Exception: pass try: hashes['difference'] = str(imagehash.dhash(image)) except Exception: pass exif_data = image.getexif() exif_dict = {} for tag_id, data in exif_data.items(): # get the tag name, instead of human unreadable tag id tag = PIL.ExifTags.TAGS.get(tag_id, tag_id) if tag == 'GPSInfo': continue try: if isinstance(data, bytes): data = data.decode() exif_dict[tag] = data except Exception: pass # This seems dumb, but image._getexif() returns different information than image.getexif() raw_exif_data = {} if image._getexif() is not None: for tag, value in image._getexif().items(): try: raw_exif_data[TAGS.get(tag, tag)] = value except Exception: pass geotags = {} if 'GPSInfo' in raw_exif_data: for tag, value in raw_exif_data['GPSInfo'].items(): try: geotags[GPSTAGS.get(tag, tag)] = value except Exception: pass lat = None lon = None try: lat = get_decimal_from_dms(geotags['GPSLatitude'], geotags['GPSLatitudeRef']) except KeyError: pass try: lon = get_decimal_from_dms(geotags['GPSLongitude'], geotags['GPSLongitudeRef']) except KeyError: pass if lat == 0 and lon == 0: lat = None lon = None date_time = set([value for key, value in exif_dict.items() if isinstance(key, str) and key.startswith('DateTime')]) if len(date_time) == 0: date_time = None else: date_time = datetime.datetime.strptime(list(date_time)[0], '%Y:%m:%d %H:%M:%S') if date_time is None and 'GPSDateStamp' in geotags and 'GPSTimeStamp' in geotags: try: year, month, day = geotags['GPSDateStamp'].split(':') date_time = datetime.datetime(year=year, month=month, day=day, hour=geotags['GPSTimeStamp'][0], minute=geotags['GPSTimeStamp'][1], second=geotags['GPSTimeStamp'][2]) except Exception: pass if date_time is None and 'GPSDateStamp' in geotags: try: year, month, day = geotags['GPSDateStamp'].split(':') date_time = datetime.datetime(year=year, month=month, day=day) except Exception: pass if date_time is None: try: extension_length = len(fpath.split('.')[-1]) + 1 date_time = datetime.datetime.strptime(os.path.basename(fpath)[:-extension_length], "%Y-%m-%d %H.%M.%S") except Exception: pass if date_time is None: try: extension_length = len(fpath.split('.')[-1]) + 1 date_time = dateutil_parser.parse(os.path.basename(fpath)[:-extension_length]) except Exception: pass max_size = (min(max_thumbnail_height*3, image.height), max_thumbnail_height) thumbnail = PIL.ImageOps.exif_transpose(image).copy() width = thumbnail.width height = thumbnail.height thumbnail.thumbnail(max_size) with tempfile.NamedTemporaryFile('wb', suffix='_ithumb.jpg', delete=False) as f: thumbnail_path = os.path.abspath(f.name) try: thumbnail.save(f, format='JPEG') except OSError: thumbnail.convert('RGB').save(f, format='JPEG') # fr_image = face_recognition.load_image_file(io.BytesIO(fdata)) # face_locations = face_recognition.face_locations(fr_image) # face_encodings = face_recognition.face_encodings(fr_image, num_jitters=3) return { 'lat': lat, 'lon': lon, 'height': height, 'width': width, 'size': sys.getsizeof(fdata), 'sha256_hash': sha_hash, 'creation_time': date_time, 'media_type': 'image', 'thumbnail': { 'thumbnail_path': thumbnail_path, 'thumbnail_width': thumbnail.width, 'thumbnail_height': thumbnail.height, 'thumbnail_size': os.path.getsize(thumbnail_path), }, 'image_only': { 'hashes': hashes, } }
def is_similar_a_hash(self): return imagehash.average_hash(self.image1) == imagehash.average_hash(self.image2)
def hamming_diff_a_hash(self): return abs(imagehash.average_hash(self.image1) - imagehash.average_hash(self.image2))
from PIL import Image import imagehash import time start_time = time.time() a = imagehash.average_hash( Image.open( r"C:\Users\johnsonvu\Desktop\IrisRecognition\png folder\osoba20\osoba20a.png" )) b = imagehash.average_hash( Image.open( r"C:\Users\johnsonvu\Desktop\IrisRecognition\png folder\osoba20\osoba20a_pic_resized_BW.png" )) c = imagehash.average_hash( Image.open( r"C:\Users\johnsonvu\Desktop\IrisRecognition\png folder\osoba20\osoba20a_vertflip.png" )) print(a) print(b) print(c) print("Difference in average_hash for same photo", a - a) print("Difference in average_hash for print attack", a - b) print("Difference in average_hash for vertical flip attack", a - c) # average_hash_list = [] # average_hash_list.append(imagehash.average_hash(Image.open(r"C:\Users\johnsonvu\Desktop\IrisRecognition\png folder\osoba20\osoba20.png"))) # average_hash_list.append(imagehash.average_hash(Image.open(r"C:\Users\johnsonvu\Desktop\IrisRecognition\png folder\osoba20\osoba20a.png"))) # average_hash_list.append(imagehash.average_hash(Image.open(r"C:\Users\johnsonvu\Desktop\IrisRecognition\png folder\osoba20\osoba20b.png"))) # average_hash_list.append(imagehash.average_hash(Image.open(r"C:\Users\johnsonvu\Desktop\IrisRecognition\png folder\osoba20\osoba20c.png"))) # average_hash_list.append(imagehash.average_hash(Image.open(r"C:\Users\johnsonvu\Desktop\IrisRecognition\png folder\osoba20\osoba20d.png"))) # average_hash_attack_list = []
def get_hash(self, image): file_name = self.IMGFOLDER + image img = PIL.Image.open(file_name) return imagehash.average_hash(img)
ks[file2] = file print('End %s' % file) subfolders = [folder for folder in os.listdir( path) if os.path.isdir(os.path.join(path, folder))] for file in subfolders: print('Start %s' % file) path2 = os.path.join(path, file) subfolders2 = [folder for folder in os.listdir( path2) if os.path.isdir(os.path.join(path2, folder))] for file2 in subfolders2: print('Start %s' % file2) path3 = os.path.join(path2, file2) subfolders3 = [folder for folder in os.listdir( path3) if os.path.join(path3, folder).endswith('.JPG')] for file3 in subfolders3: path4 = os.path.join(path3, file3) m = cv2.imread(path4, 1) if m is not None: img = Image.fromarray(m) ihash = ih.average_hash(img) if ik.has_key(ihash) and ik[ihash][0] != file: logging.error('######### Error Has Same Image: %s == %s' % (path4, ik[ihash])) ik[ihash] = [file, file2, file3] else: logging.error('Bad Image: %s' % path4) print('End %s' % file) a = np.array(ik) a.save('my_hash.npy')
haystackPaths = list(paths.list_images(IMGDIR)) print(haystackPaths) # init a hash dataframe haystack = pd.DataFrame(columns=['file', 'phash', 'ahash', 'dhash', 'whash']) # time the hashing operation start = time.time() for f in haystackPaths: image = Image.open(f) # imageHash = imagehash.phash(image) p = imagehash.phash(image, hash_size=32) a = imagehash.average_hash(image, hash_size=32) d = imagehash.dhash(image, hash_size=32) w = imagehash.whash(image, hash_size=32) haystack = haystack.append ({'file':f, 'phash':p, 'ahash':a, 'dhash':d,'whash':w }, ignore_index=True) # print (haystack.head()) # print (p, imageHash) # haystack[imageHash] = p # show timing for hashing haystack images, then start computing the # hashes for needle images print("[INFO] processed {} images in {:.2f} seconds".format( len(haystack), time.time() - start)) print("[INFO] computing hashes for needles...")
from PIL import Image import imagehash import sys hash = imagehash.average_hash(Image.open('D:\pythonspace\learnpy\h1.jpg')) print(hash) otherhash = imagehash.average_hash(Image.open('D:\pythonspace\learnpy\h2.jpg')) print(otherhash)
def compute_ahash(im): """Compute average hash using ImageHash library :param im: Numpy.ndarray :returns: Imagehash.ImageHash """ return imagehash.average_hash(ensure_pil(im_pil))