What this Code Does
This Python script is a GUI-based tool for comparing images between two folders using perceptual hashing (pHash). It identifies visually similar images, allowing the user to copy matching images to a third folder. Below is a breakdown of the main components:
YouTube link: https://youtu.be/yZfYzlNj1so
1. Comparing Images
The function compare_images
uses the Pillow
library and imagehash
to compare two images:
- The images are resized to a standard size (256×256 pixels) to normalize them for comparison.
- Perceptual hashes (pHash) are computed for both images, and the Hamming distance between the hashes is compared.
- If the distance is below a specified threshold (
max_difference
), the images are considered visually similar.
2. Finding and Copying Matches
The function find_and_copy_matching_images
:
- Iterates through all image files in two folders (
source_folder
andtarget_folder
). - Compares each image from the source folder against all images in the target folder using
compare_images
. - If a match is found, the matching image is copied to the output folder (
output_folder
). - Stops comparing a source image once a match is found, saving time.
3. Clearing the Output Folder
The function clear_output_folder
ensures the output folder is emptied before new matches are copied:
- Deletes all files and subdirectories in the folder.
4. GUI Functionality
The create_gui
function provides a graphical user interface using Tkinter:
- Input fields: Users can select two input folders (folder1 and folder2) and an output folder (folder3).
- Action Buttons: Two buttons let users:
- Copy matching images from
folder1
tofolder3
. - Copy matching images from
folder2
tofolder3
.
- Copy matching images from
- Folder Selection: The
select_folder
function opens a folder browser to choose directories. - Notifications: Users are informed of success or failure using message boxes.
5. Starting the Comparison
- From Folder1 to Folder3:
start_comparison
compares images infolder1
againstfolder2
and saves matches tofolder3
. - From Folder2 to Folder3:
start_comparison_reverse
does the same but swaps the source and target folders. - Both functions ensure
folder3
is created and cleared before saving new matches.
6. Perceptual Hashing (pHash)
- Perceptual hashing generates a hash based on the image’s appearance rather than its binary data.
- Small differences in appearance (e.g., slight color variations) are tolerated, making this method effective for detecting similar images even if they are not pixel-for-pixel identical.
7. Centering the Window
The center_window
function calculates the center position for the Tkinter window, ensuring it appears centered on the user’s screen.
Use Case
- A user can use this tool to detect and manage duplicate or similar images, for example:
- Identifying similar product images in e-commerce directories.
- Organizing photo libraries by finding near-duplicates.
Key Libraries Used
os
andshutil
: Handle file and folder operations.Pillow (PIL)
: For image processing (opening, resizing).imagehash
: For perceptual hashing.Tkinter
: For GUI development.
How to Run
- Install required libraries if not already installed:
pip install pillow imagehash
- Save the script as
compare_images_gui.py
and run it:python compare_images_gui.py
- Use the GUI to select the folders and perform the comparison.
Output
- Matching images are copied to a subfolder named
folder3
within the specified output folder. - A success message displays the number of matches found, or a no-match notification is shown if none are found.
Download compare_images_gui in zip format…
Python filename compare_images_gui.txt is inside the zip filename compare_images_gui.
Rename compare_images_gui.txt to filename: compare_images_gui.py and run the code.
# Python code starts here
Comments are closed.