This script sorts the images in a folder into different categories based on image background. It checks if an image has a transparent or matching color background (within a given tolerance) and sorts them accordingly. Remaining images are considered as photos with non-matching background colors.
To use this script, you need to install the following packages:
- OpenCV (cv2): for reading and processing the images
- numpy: for dealing with array manipulations
- tqdm: for displaying progress bars
You can install these packages using pip: pip install opencv-python numpy tqdm
- Place your images in a folder named 'images' within the same directory as the script.
- Update the following variables in the script according to your needs:
images_dir
: Name of the folder containing the imagesparent_output_folder
: Name of the parent output folder where sorted images will be storedmatching_folder
: Name of the folder for images with matching background colortransparent_folder
: Name of the folder for images with a transparent backgroundphoto_folder
: Name of the folder for images with non-matching background colormatching_threshold
: Minimum percentage of matching background pixels to consider an image as matchingtarget_rgb
: Tuple containing the target background color (RGB values) you want to matchtolerance_rgb
: Allows some variation in the matching background color. For example, if target_rgb is (230, 230, 230) and tolerance_rgb is (10, 10, 10), any pixel with an RGB value within the range (R: 225-235, G: 225-235, B: 225-235) will be considered a match.
- Run the script by executing the command
python sort-photos.py
- You will find the sorted images in the specified output folders.
The script creates a log file sorted_images_log.txt
with details about the sorting process, indicating the folder in which each image was moved. Each new run of the script appends a new log entry in the same file.