AlUlkesh / stable-diffusion-webui-images-browser

an images browse for stable-diffusion-webui
627 stars 111 forks source link

Feature request: category and export files by rank #143

Open 112292454 opened 1 year ago

112292454 commented 1 year ago

Since we have already done the function of manually adding rank tags, why not provide a way to classify and export them through rank (locally moving files may be sufficient, like six folder/zip, mapped each ranks).This feature may seem relatively simple, but I don't know how to organize/obtain rank information through wib.sqlite3——and the code is a bit heavy

BTW, I found that marking on the t2i interface and moving it to the save folder did not take effect. It doesn't seem like https://github.com/tsngo/stable-diffusion-webui-aesthetic-image-scorer is written in the file itself, but through the plugin's database. Should we include rank information when moving files?

Gukigit commented 1 year ago

Yes, this will be very useful. I spent a day ranking thousands of images and found it impossible to export them in bulk

Moahmmed1900 commented 8 months ago

I've made a quick python script just for the time being. First, you need to make a copy of the database to new folder. (database found in "extensions/stable-diffusion-webui-images browser/wib.sqlite3")

Then in the new folder, make a new python file "main.py" and paste the below:

import sqlite3
from zipfile import ZipFile
from datetime import datetime
import os

MIN_RANK = 3
MAX_RANK = 4

CATEGORY = "txt2img-images" # txt2img-images, img2img-images, extras-images

def main():
    connection = sqlite3.connect("wib.sqlite3")

    cursor = connection.cursor()

    results = cursor.execute(f"SELECT file, ranking FROM ranking WHERE ranking.ranking <= {MAX_RANK} AND ranking.ranking >= {MIN_RANK};")

    results = results.fetchall()

    to_be_ziped_images = []

    print("Following images will be zipped: ")
    counter = 1
    for image in results:
        if CATEGORY in str(image[0]):
            print(f'{counter}: {image[0]}')
            to_be_ziped_images.append(image[0])
            counter = counter + 1

    if len(to_be_ziped_images) > 0:
        with ZipFile(f'bulk_extract_{datetime.now().strftime("%Y_%m_%d_%H_%M_%S")}.zip', 'w') as zip:
            for image in to_be_ziped_images:
                zip.write(filename=image, arcname=os.path.basename(image))

        print('All images zipped successfully!')
    else:
        print("No images to be extracted")

if __name__ == "__main__":
    main()

Lastly, modify the variables (MIN_RANK, MAX_RANK, CATEGORY) to your needs.

Then just run the script ("python main.py"), it will print the list of image that it found with your criteria, then a new zip file will be created with the images.

Final notes, 1- The script was tested on automatic1111. 2- The script was ran on the same system the WebUI was running on.