def doComparison(photo1loc, photo2loc): original = cv2.imread(photo1loc) contrast = url_to_image(photo2loc) contrast = cv2.resize(contrast, (original.shape[1], original.shape[0])) original = cv2.cvtColor(original, cv2.COLOR_BGR2GRAY) contrast = cv2.cvtColor(contrast, cv2.COLOR_BGR2GRAY) MSE, SSIM = compareImages(original, contrast) image1 = Image.open(photo1loc) row1, col1 = dhash.dhash_row_col(image1) # print(dhash.format_hex(row1, col1)) newfile = io.BytesIO(urllib.request.urlopen(photo2loc).read()) image2 = Image.open(newfile) row2, col2 = dhash.dhash_row_col(image2) # print(dhash.format_hex(row2, col2)) num_bits_different = dhash.get_num_bits_different(dhash.dhash_int(image1), dhash.dhash_int(image2)) # print(num_bits_different) #faceCompare = face.beginImageRec(photo1loc, photo2loc) faceCompare = 0.3 return (MSE, SSIM, num_bits_different, faceCompare)
def dHash_use_package(img1, img2): image1 = Image.open(img1) image2 = Image.open(img2) row1, col1 = dhash.dhash_row_col(image1) row2, col2 = dhash.dhash_row_col(image2) a1 = int(dhash.format_hex(row1, col1), 16) a2 = int(dhash.format_hex(row2, col2), 16) result = dhash.get_num_bits_different(a1, a2) if result<=5: print('Same Picture') return result
def get_image_dhash(image_bytes): image_file = io.BytesIO(image_bytes) image = Image.open(image_file) dhash.force_pil() row, col = dhash.dhash_row_col(image) image_dhash = dhash.format_hex(row, col) return image_dhash
def get_dhash(file_path): try: image = PIL.Image.open(file_path) except OSError: return row, col = dhash.dhash_row_col(image) return dhash.format_hex(row, col)
def hashit(): count = 0 print("Start 'Picture Hashing'") verbose = False for index, file in enumerate(filelist): print("\rHashing File %d of %d - %d" % (index + 1, len(filelist), len(hashtable)),end=""); count = count + 1 if verbose: print(file) try: image = Image.open(file) except Exception as e: if verbose: print(" ... Cannot open image: %s" % file) continue try: row, col = dhash.dhash_row_col(image) except Exception as e: if verbose: print(" ... Cannot open image: %s" % file) continue pichash = dhash.format_hex(row, col) if pichash in hashtable: dst = dubdir + file.rsplit("/", 1)[1] movefile(file, dst, index) else: hashtable.update({pichash: file}) print("")
def home(): if request.method == 'POST': if 'file' not in request.files and 'text' not in request.form['text']: print('No text or file uploaded') return redirect(request.url) file = request.files['file'] text = request.form['text'].lower() print("there is text" + text) if file.filename == '' and text == '': print('No input text or file selected') return redirect(request.url) if file.filename != '': print("file was uploaded") if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) image = Image.open(file) row, col = dhash.dhash_row_col(image) uploaded_image_hash = dhash.format_hex(row, col) print(type(uploaded_image_hash)) print("Here is the hash " + str(uploaded_image_hash)) list_of_available_image_hashes = compare_images.request_image_hashes( ) # similar_images.clear() # similarity.clear() global similarity global similar_images similar_images = list() similarity = dict() similar_images.clear() for img_hash in list_of_available_image_hashes: # print("uploaded_image_hash: " + str(uploaded_image_hash)) similarity[ "index"] = compare_images.calculate_hamming_dist( uploaded_image_hash, img_hash["image_hash"]) print(similarity["index"]) if similarity["index"] < 10: similarity["image_name"] = img_hash["image_name"] similar_images.append(similarity.copy()) similarity.clear() print("these are the images to display" + str(similar_images)) return render_template('search_results.html', images=similar_images) #redirect(url_for('upload_file', filename=filename)) #return render_template('uploaded_image.html') return render_template('home_page.html')
def get_image_hash(image_file): try: image = Image.open(image_file) row, col = dhash.dhash_row_col(image, size=12) image_hash = dhash.format_hex(row, col, size=12) return image_hash except OSError: print(f"Could not read image {image_file}")
def calculate_dhashes (files): dhashes = [] for i in files: image = PIL.Image.open(i) row, col = dhash.dhash_row_col(image) print (i) print(dhash.format_hex(row, col))
def d_hash(image1, image2): """" compares two images using "Difference Hash" algorithm if two images are same it'll delete one image else two images are stored """ "" row1, col1 = dhash.dhash_row_col(image1) row2, col2 = dhash.dhash_row_col(image2) hash1 = dhash.format_hex(row1, col2) hash2 = dhash.format_hex(row2, col2) if hash1 == hash2: path1 = path + '\\' + str(index) + '.jpg' os.remove(path1)
def compute_hashes(self, ): for ad_domain, image_path in self.images.iteritems(): image = Image.open(image_path) row, col = dhash.dhash_row_col(image) hash = dhash.format_hex(row, col) if hash not in self.clusters: self.clusters[hash] = [] self.clusters[hash].append(image_path)
def put_hash(self, src, cam_id=''): imageStream = io.BytesIO(src) imageFile = Image.open(imageStream) row, col = dhash.dhash_row_col(imageFile) image_hash = dhash.format_hex(row, col) meta = dict(timestamp=time.localtime(), image_hash=image_hash) #print("Putting hash " + str(image_hash) + " for cam " + str(cam_id) + " in cache.") self.cache[cam_id] = meta
def get_phash(file_path=None): """Get the perceptual hash of the specified file.""" if not file_path: return None # print(file_path) try: img_1 = Image.open(file_path) row_1, col_1 = dhash_row_col(img_1) return format_hex(row_1, col_1) except (OSError, AttributeError) as e: print(e) return None
def generate_hash(self): images_in_db = self.request_list_of_images_in_db() print(images_in_db) for img in images_in_db: image = Image.open(IMAGE_FOLDER + "\\" + img["image_name"]) row, col = dhash.dhash_row_col(image) img_hash = dhash.format_hex(row, col) values = img_hash, img["image_id"] db_ops.image_store_migrations() print(values) db_ops.insert_operations("image_store_hash", values)
def compareImages(filename1, filename2): try: image1 = Image.open('downloads/'+filename1) image2 = Image.open('downloads/'+filename2) except: return None row1, col1 = dhash.dhash_row_col(image1) row2, col2 = dhash.dhash_row_col(image2) hash1 = dhash.format_hex(row1, col1) hash2 = dhash.format_hex(row2, col2) if hash1 == hash2: return -1 hashImage1 = HashImage(image1) hashImage2 = HashImage(image2) if(hashImage1 == hashImage2): return True else: return False
def get_phash(image_name): """ Get the perceptual hash of the given image :param image_name: name of the image file :return: the perceptual hash, empty string in case of error """ dhash.force_pil() # Force PIL try: image = Image.open(image_name).convert("RGBA") row, col = dhash.dhash_row_col(image, size=PHASH_SIZE) return row << (PHASH_SIZE * PHASH_SIZE) | col except IOError as e: logging.error(e) return ''
def compute_hashes(files): # compute hash for the images hashes = [] for index, file in enumerate(files): try: image = Image.open(file) row, col = dhash.dhash_row_col(image) img_hash = dhash.format_hex(row, col) hashes.append(img_hash) except OSError: print("Can't read this file: {} - removing from the list".format( file)) del files[index] print(hashes) return hashes
def setup(): """ Fill painting_db with paintings data contained in data.csv and compute their hashes :return: None. """ data = pd.read_csv(data_folder + "data.csv") images = [f for f in listdir(db_folder) if isfile(join(db_folder, f))] # Fill painting_db list with paintings infos for file_name in images: row, col = dhash.dhash_row_col(Image.open(db_folder + file_name)) painting = Painting(file_name, data[data["Image"] == file_name]["Title"].item(), data[data["Image"] == file_name]["Author"].item(), data[data["Image"] == file_name]["Room"].item(), int(dhash.format_hex(row, col), 16)) painting_db.append(painting)
def hashThreaded(workinglist, pbar): global hash_keys global duplicates for index, filename in enumerate(workinglist): if os.path.isfile('wallpapers/' + filename): try: image = Image.open( 'wallpapers/' + filename) #try to open the image, if it fails, delete it row, col = dhash.dhash_row_col(image) filehash = dhash.format_hex(row, col) if filehash not in hash_keys: hash_keys[filehash] = index else: duplicates.append((filename, hash_keys[filehash])) except: os.remove('wallpapers/' + filename) pbar.update(1)
def difhashmtrx(self, file_dirs, unq_files): print("\nForming Dhash (Size = 8) Matrix...") dif_hash_mtrx = [[] for x in range(len(self.directories))] c1 = 0 unhashed = [] for clas in file_dirs: for image_path in clas: try: image = Image.open(image_path).convert( "RGBA" ) # All palette images with transparency expressed in bytes should converted to RGBA images row, col = dhash.dhash_row_col(image) image_hash = dhash.format_hex(row, col) dif_hash_mtrx[c1].append(image_hash) except: unhashed.append(image_path) print(image_path, " - file type is unhashable.") c1 += 1 print(len(unhashed), " images ignored.") return dif_hash_mtrx
def get_milking_return_data(log_id): log_path = os.path.join(config.MAIN_LOG_PATH, config.SEHUNTER_LOGS_DIR, "%s.log" % (log_id, )) with open(log_path) as f: screenshot_path = None home_url = None downloaded_file = False for line in f: if "The screenshot of loaded home page" in line: screenshot_path = line.strip().rsplit(' ', 1)[1] if "Home URL: " in line: home_url = line.strip().rsplit(' ', 1)[1] if "Downloaded a file: " in line: downloaded_file = True if screenshot_path: image = Image.open(screenshot_path) row, col = dhash.dhash_row_col(image) screenshot_hash = dhash.format_hex(row, col) else: screenshot_hash = None return screenshot_hash, home_url, downloaded_file
def rename(d): for root, dirs, files in os.walk(d): for f in tqdm(files): if f.endswith('.jpg'): img_fpath = os.path.join(root, f) try: im = Image.open(img_fpath) row, col = dhash.dhash_row_col(im) # 计算16进制hash hash1 = dhash.format_hex(row, col) # 把hash转换为string类型 except IOError as e: print(e) if f == (str(hash1) + '.jpg'): pass else: os.rename(img_fpath, os.path.join(root, str(hash1) + '.jpg')) possible_ano_folder = ['annotation', 'annotations'] i = 0 p = os.path.join(os.path.dirname(root), 'Annotations') while not os.path.exists(p): p = os.path.join(os.path.dirname(root), possible_ano_folder[i]) i += 1 if i > len(possible_ano_folder) - 1: raise ValueError( "Unknown annotaiton folder name, all possible folder name tried." ) try: os.rename(os.path.join(p, f[:-4] + '.xml'), os.path.join(p, str(hash1) + '.xml')) except OSError as e: print(e)
def match_painting(img): """ Compute hash bit differences between img and paintings in painting_db :param img: input image to use. :return: Matching painting (the one having less differences with img and below threshold) or None if all differences are above treshold. """ threshold = 20 img_row, img_col = dhash.dhash_row_col(img) img_hash = dhash.format_hex(img_row, img_col) img_hash = int(img_hash, 16) differences = [] # Check difference between img and painting_db for painting in painting_db: differences.append(dhash.get_num_bits_different(img_hash, painting.hash)) if min(differences) < threshold: return painting_db[differences.index(min(differences))] else: return None
def get_hash(image): row, col = dhash.dhash_row_col(PIL.Image.fromarray(image)) return dhash.format_hex(row, col)
def getImgHash(img): row, col = dhash.dhash_row_col(img) mHash = dhash.format_hex(row, col) mHash = '0x' + mHash return int(mHash, 16)
# Original image1 = download_doc(doc1) # Duplicate image2 = image1.filter(ImageFilter.BLUR) # # Detection! # In[5]: dhash.force_pil() size = 8 row, col = dhash.dhash_row_col(image1) hash1 = dhash.format_hex(row, col,size=size) print(hash1) row, col = dhash.dhash_row_col(image2) hash2 = dhash.format_hex(row, col,size=size) print(hash2) num_bits_different = hamming2(hash1, hash2) print(num_bits_different) print('{} {} out of {} ({:.1f}%)'.format( num_bits_different, 'bit differs' if num_bits_different == 1 else 'bits differ', size * size * 2, 100 * num_bits_different / (size * size * 2))) # # Using documents which looks close, however they are different!
def image_difference_hash(path): if not path.suffix.endswith(tuple(Const.imgs)): return "" with Image.open(path) as img: row, col = dhash.dhash_row_col(img) return dhash.format_hex(row, col)
def get_image_hash(self, src): image = Image.open(src) row, col = dhash.dhash_row_col(image) return dhash.format_hex(row, col)
def get_image_hash(self, src): imageStream = io.BytesIO(src) imageFile = Image.open(imageStream) row, col = dhash.dhash_row_col(imageFile) return dhash.format_hex(row, col)
def get_image_hash(image_path): image = Image.open(image_path) row, col = dhash.dhash_row_col(image) hash_str = dhash.format_hex(row, col) return hash_str
def generate_hash(file_name, split_path): image = Image.open(split_path + "/" + file_name) row, col = dhash.dhash_row_col(image) hash = int(dhash.format_hex(row, col), 16) return hash