<reponame>ToxicFrazzles/Image-Deduplicator<filename>main.py
from pathlib import Path
import getopt, sys
import PIL
import imagededuplicator

this_dir = Path(__file__).parent
if __name__ == "__main__":
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'dri:')
    except getopt.GetoptError:
        print("Usage: main.py -i <input_directory> [-d <database_path> -r]")
        exit()
    input_dir = None
    database_dir = this_dir / "db.sqlite3"
    recursive = False
    for o, a in opts:
        if o == "-i":
            input_dir = Path(a)
            if not input_dir.is_dir():
                print("Provided input directory is not a directory...")
                print(input_dir)
                exit()
        elif o == "-d":
            database_dir = Path(a)
        elif o == "-r":
            recursive = True

    db = imagededuplicator.Database(database_dir)
    db.create_all_tables()
    print(f"We already have {db.get_image_count()} images in the database")
    next_dirs = [input_dir]
    while True:
        try:
            next_dir = next_dirs.pop()
        except IndexError:
            break
        print(f"Looking into {next_dir}")
        for item in next_dir.iterdir():
            if item.is_dir() and recursive:
                next_dirs.append(item)
                continue
            elif item.is_dir():
                continue
            img = imagededuplicator.Image(item)
            if not img.is_image():
                continue
            results = db.query_image(img, "dhash")
            if results and results[0][1].file_path == img.file_path:
                # It's the same image...
                pass
            elif results and results[0][0] < 2:
                print(f"{img.file_path} is very similar to {results[0][1].file_path}")
            else:
                db.insert_image(img)
                print("Added image to database")

from pathlib import Path
import getopt, sys
import PIL
import imagededuplicator

this_dir = Path(__file__).parent
if __name__ == "__main__":
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'dri:')
    except getopt.GetoptError:
        print("Usage: main.py -i <input_directory> [-d <database_path> -r]")
        exit()
    input_dir = None
    database_dir = this_dir / "db.sqlite3"
    recursive = False
    for o, a in opts:
        if o == "-i":
            input_dir = Path(a)
            if not input_dir.is_dir():
                print("Provided input directory is not a directory...")
                print(input_dir)
                exit()
        elif o == "-d":
            database_dir = Path(a)
        elif o == "-r":
            recursive = True

    db = imagededuplicator.Database(database_dir)
    db.create_all_tables()
    print(f"We already have {db.get_image_count()} images in the database")
    next_dirs = [input_dir]
    while True:
        try:
            next_dir = next_dirs.pop()
        except IndexError:
            break
        print(f"Looking into {next_dir}")
        for item in next_dir.iterdir():
            if item.is_dir() and recursive:
                next_dirs.append(item)
                continue
            elif item.is_dir():
                continue
            img = imagededuplicator.Image(item)
            if not img.is_image():
                continue
            results = db.query_image(img, "dhash")
            if results and results[0][1].file_path == img.file_path:
                # It's the same image...
                pass
            elif results and results[0][0] < 2:
                print(f"{img.file_path} is very similar to {results[0][1].file_path}")
            else:
                db.insert_image(img)
                print("Added image to database")
# It's the same image...