feat: Add script to sort media files and compare images using Ollama
This commit is contained in:
44
sort_media.py
Normal file
44
sort_media.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import os
|
||||
import shutil
|
||||
from datetime import datetime
|
||||
from ollama import Ollama
|
||||
|
||||
def get_earliest_time(file_path):
|
||||
"""Get the earliest of creation or last modified time."""
|
||||
stat = os.stat(file_path)
|
||||
return min(stat.st_ctime, stat.st_mtime)
|
||||
|
||||
def sort_media(directory):
|
||||
"""Sort media files into folders by their earliest time."""
|
||||
if not os.path.exists(directory):
|
||||
print(f"Directory {directory} does not exist.")
|
||||
return
|
||||
|
||||
for filename in os.listdir(directory):
|
||||
file_path = os.path.join(directory, filename)
|
||||
if os.path.isfile(file_path):
|
||||
earliest_time = get_earliest_time(file_path)
|
||||
date_folder = datetime.fromtimestamp(earliest_time).strftime('%Y-%m-%d')
|
||||
target_folder = os.path.join(directory, date_folder)
|
||||
|
||||
if not os.path.exists(target_folder):
|
||||
os.makedirs(target_folder)
|
||||
|
||||
shutil.move(file_path, os.path.join(target_folder, filename))
|
||||
|
||||
def compare_images_with_ollama(directory):
|
||||
"""Compare images using Ollama."""
|
||||
ollama = Ollama(model="ollama3.2:3b")
|
||||
|
||||
for root, _, files in os.walk(directory):
|
||||
for file in files:
|
||||
if file.lower().endswith(('.png', '.jpg', '.jpeg')):
|
||||
file_path = os.path.join(root, file)
|
||||
# Assuming Ollama has a method to compare images
|
||||
result = ollama.compare(file_path)
|
||||
print(f"Comparison result for {file}: {result}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
directory = input("Enter the directory path to sort and compare media: ")
|
||||
sort_media(directory)
|
||||
compare_images_with_ollama(directory)
|
||||
Reference in New Issue
Block a user