The trouble is the line in your images. They won't create the same hash.
Now I wrote following script that check each two GIF files in a folder in very high speed!
import cv2
import numpy as np
import os
from concurrent.futures import ProcessPoolExecutor, as_completed
import keyboard
def calculate_similarity_percentage(file1, file2):
# Read the GIF files
gif1 = cv2.VideoCapture(file1)
gif2 = cv2.VideoCapture(file2)
# Read the first frame of each GIF
_, frame1 = gif1.read()
_, frame2 = gif2.read()
# Resize frames to a common size
frame1 = cv2.resize(frame1, (640, 480))
frame2 = cv2.resize(frame2, (640, 480))
# Convert frames to grayscale
gray1 = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)
# Apply Gaussian blur to the frames
blurred1 = cv2.GaussianBlur(gray1, (15, 15), 0)
blurred2 = cv2.GaussianBlur(gray2, (15, 15), 0)
# Find the absolute difference between the two blurred frames
diff = cv2.absdiff(blurred1, blurred2)
# Threshold the difference image
_, thresholded = cv2.threshold(diff, 30, 255, cv2.THRESH_BINARY)
# Calculate the difference percentage
total_pixels = thresholded.size
non_zero_pixels = cv2.countNonZero(thresholded)
difference_percentage = (non_zero_pixels / total_pixels) * 100
# Calculate the similarity percentage
similarity_percentage = 100 - difference_percentage
return similarity_percentage
def compare_files_chunk(files_chunk, directory):
results = []
for i in range(1, len(files_chunk)):
file_path1 = os.path.join(directory, files_chunk[i - 1])
file_path2 = os.path.join(directory, files_chunk[i])
similarity_percentage = calculate_similarity_percentage(file_path1, file_path2)
results.append(f"Comparison between {files_chunk[i - 1]} and {files_chunk[i]}: {similarity_percentage:.2f}%")
return results
def compare_all_files(directory, output_file):
files = sorted([f for f in os.listdir(directory) if f.lower().endswith('.gif')])
num_files = len(files)
chunk_size = min(num_files, os.cpu_count() * 4) # Adjust the chunk size based on your system's capabilities
with open(output_file, 'w') as f_out, ProcessPoolExecutor() as executor:
futures = []
for i in range(0, num_files, chunk_size):
files_chunk = files[i:i + chunk_size]
future = executor.submit(compare_files_chunk, files_chunk, directory)
futures.append(future)
for future in as_completed(futures):
results = future.result()
f_out.write('\n'.join(results) + '\n')
# Check for 'F6' key press to stop the process
if keyboard.is_pressed('F6'):
print("Process stopped by user.")
return
def main():
directory = r'E:\Desktop\Armies\L1816_2'
output_file = r'E:\Desktop\Armies\comparison_results.txt'
try:
compare_all_files(directory, output_file)
print("Comparison completed. Results saved in:", output_file)
except Exception as e:
print(f"Error: {str(e)}")
if __name__ == "__main__":
main()