Skip to content
Snippets Groups Projects
Commit e0a4e5c0 authored by Trim Bresilla's avatar Trim Bresilla
Browse files

Initial commit

parents
Branches
No related tags found
No related merge requests found
images
similar_images
unique_images
import os
import cv2
import shutil
# Define the threshold for darkness detection
darkness_threshold = 18 # You can adjust this value based on your needs
# Define the threshold for blur detection
blur_threshold = 100 # You can adjust this value based on your needs
# Get the directory where the script is located
script_directory = os.path.dirname(__file__)
# Create directories to store the blurry, non-blurry, and dark images
blurry_output_directory = os.path.join(script_directory, 'blurry_images')
non_blurry_output_directory = os.path.join(script_directory, 'non_blurry_images')
dark_output_directory = os.path.join(script_directory, 'dark_images')
# Function to empty a folder
def empty_folder(folder):
for filename in os.listdir(folder):
file_path = os.path.join(folder, filename)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
except Exception as e:
print(e)
# Empty the blurry, non-blurry, and dark image folders if they are non-empty
if os.path.exists(blurry_output_directory):
empty_folder(blurry_output_directory)
if os.path.exists(non_blurry_output_directory):
empty_folder(non_blurry_output_directory)
if os.path.exists(dark_output_directory):
empty_folder(dark_output_directory)
# Use os.walk to traverse all subdirectories
for folder, subfolders, files in os.walk(script_directory):
for file in files:
# Check if the file is an image (you can customize the extensions)
if file.lower().endswith(('.jpg', '.jpeg', '.png', '.gif', '.bmp')):
image_path = os.path.join(folder, file)
# Load the image
image = cv2.imread(image_path)
# Calculate the mean pixel value to detect darkness
brightness = cv2.mean(image)[0]
# Calculate the variance of Laplacian to detect blur
variance = cv2.Laplacian(image, cv2.CV_64F).var()
# Create the output directory based on image quality (dark, blurry, or non-dark and non-blurry)
if brightness < darkness_threshold:
# Copy the dark image to the dark images folder
print(f"Dark Image: {image_path} - Brightness: {brightness}")
if not os.path.exists(dark_output_directory):
os.makedirs(dark_output_directory)
destination_path = os.path.join(dark_output_directory, os.path.basename(image_path))
# Check if the source and destination paths are different
if image_path != destination_path:
shutil.copy2(image_path, destination_path)
elif variance < blur_threshold:
# Copy the blurry image to the blurry images folder
print(f"Blurry Image: {image_path} - Variance: {variance}")
if not os.path.exists(blurry_output_directory):
os.makedirs(blurry_output_directory)
destination_path = os.path.join(blurry_output_directory, os.path.basename(image_path))
# Check if the source and destination paths are different
if image_path != destination_path:
shutil.copy2(image_path, destination_path)
else:
# Copy the non-dark and non-blurry image to the non-blurry images folder
print(f"Non-Dark and Non-Blurry Image: {image_path}")
if not os.path.exists(non_blurry_output_directory):
os.makedirs(non_blurry_output_directory)
destination_path = os.path.join(non_blurry_output_directory, os.path.basename(image_path))
# Check if the source and destination paths are different
if image_path != destination_path:
shutil.copy2(image_path, destination_path)
# You can adjust the 'darkness_threshold', 'blur_threshold', 'blurry_output_directory', 'dark_output_directory', and 'non_blurry_output_directory' as needed
\ No newline at end of file
import os
import cv2
import shutil
from PIL import Image
from imagehash import dhash
# Define the threshold for similarity
similarity_threshold = 10 # You can adjust this value based on your needs
# Get the directory where the script is located
script_directory = os.path.dirname(__file__)
# Create a directory to store similar images
similar_output_directory = os.path.join(script_directory, 'similar_images')
# Create a directory to store unique images
unique_output_directory = os.path.join(script_directory, 'unique_images')
# Function to empty a folder
def empty_folder(folder):
for filename in os.listdir(folder):
file_path = os.path.join(folder, filename)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
except Exception as e:
print(e)
# Empty the similar and unique image folders if they are non-empty
if os.path.exists(similar_output_directory):
empty_folder(similar_output_directory)
if os.path.exists(unique_output_directory):
empty_folder(unique_output_directory)
# Dictionary to store image hashes and their paths
image_hashes = {}
# Use os.walk to traverse all subdirectories
for folder, subfolders, files in os.walk(script_directory):
for file in files:
# Check if the file is an image (you can customize the extensions)
if file.lower().endswith(('.jpg', '.jpeg', '.png', '.gif', '.bmp')):
image_path = os.path.join(folder, file)
# Load the image
image = cv2.imread(image_path)
# Convert the NumPy array to a PIL image object
pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
# Calculate the dHash for the image
image_hash = dhash(pil_image, hash_size=8)
# Check if this hash is similar to any previously seen hashes
similar = False
for existing_hash, existing_path in image_hashes.items():
hamming_distance = image_hash - existing_hash
if hamming_distance < similarity_threshold:
similar = True
print(f"Similar Image: {image_path} is similar to {existing_path}")
if not os.path.exists(similar_output_directory):
os.makedirs(similar_output_directory)
destination_path = os.path.join(similar_output_directory, os.path.basename(image_path))
shutil.copy2(image_path, destination_path)
break
# If not similar to any previously seen images, add it to the dictionary and copy it to the unique folder
if not similar:
image_hashes[image_hash] = image_path
print(f"Unique Image: {image_path}")
if not os.path.exists(unique_output_directory):
os.makedirs(unique_output_directory)
destination_path = os.path.join(unique_output_directory, os.path.basename(image_path))
shutil.copy2(image_path, destination_path)
# You can adjust the 'similarity_threshold', 'similar_output_directory', and 'unique_output_directory' as needed
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment