from PIL import Image, ImageOps, UnidentifiedImageError, ImageDraw from datetime import datetime from io import StringIO from itertools import chain from modules import paths, shared, script_callbacks, scripts, images from modules.shared import opts, cmd_opts from modules.ui_common import plaintext_to_html, save_files from modules.ui_components import ToolButton, DropdownMulti from packaging import version from pathlib import Path from typing import List, Tuple import codecs import csv import gradio as gr import hashlib import importlib import json import logging import math import modules.extras import modules.images import modules.ui import os import platform import random import re import shutil import stat import subprocess as sp import sys import tempfile import time import torch import traceback try: from scripts.wib import wib_db except ModuleNotFoundError: sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "scripts"))) from wib import wib_db try: from send2trash import send2trash send2trash_installed = True except ImportError: print("Image Browser: send2trash is not installed. Recycle bin cannot be used.") send2trash_installed = False try: import cv2 opencv_installed = True except ImportError: print("Image Browser: opencv is not installed. Video related actions cannot be performed.") opencv_installed = False try: from modules import generation_parameters_copypaste as sendto except ImportError: from modules import infotext_utils as sendto try: from modules_forge import forge_version forge = True except ImportError: forge = False # Force reload wib_db, as it doesn't get reloaded otherwise, if an extension update is started from webui importlib.reload(wib_db) yappi_do = False components_list = ["Sort by", "Filename keyword search", "EXIF keyword search", "Ranking Filter", "Aesthestic Score", "Generation Info", "File Name", "File Time", "Open Folder", "Send to buttons", "Copy to directory", "Gallery Controls Bar", "Ranking Bar", "Delete Bar", "Additional Generation Info"] num_of_imgs_per_page = 0 loads_files_num = 0 image_ext_list = [".png", ".jpg", ".jpeg", ".bmp", ".gif", ".webp", ".svg"] video_ext_list = [".mp4", ".mov", ".avi", ".wmv", ".flv", ".mkv", ".webm", ".mpeg", ".mpg", ".3gp", ".ogv", ".m4v"] if not opencv_installed: video_ext_list = [] exif_cache = {} aes_cache = {} none_select = "Nothing selected" refresh_symbol = "\U0001f504" # 🔄 up_symbol = "\U000025b2" # ▲ down_symbol = "\U000025bc" # ▼ caution_symbol = "\U000026a0" # ⚠ folder_symbol = "\U0001f4c2" # 📂 play_symbol = "\U000023f5" # ⏵ current_depth = 0 init = True copy_move = ["Move", "Copy"] copied_moved = ["Moved", "Copied"] np = "negative_prompt: " openoutpaint = False controlnet = False js_dummy_return = None log_file = os.path.join(scripts.basedir(), "image_browser.log") optimized_cache = None show_progress_setting = True gradio_min = "3.23.0" gradio3_new_gallery_syntax = "3.39.0" gradio4 = "4.0.0" gradio4_new_gallery_syntax = "4.16.0" temp_temp = os.path.join(scripts.basedir(), "temp") db_version = wib_db.check() favorite_tab_name = "Favorites" default_tab_options = ["txt2img", "img2img", "txt2img-grids", "img2img-grids", "Extras", favorite_tab_name, "Others", "All", "Maintenance"] def check_image_browser_active_tabs(): last_default_tab = wib_db.get_last_default_tab() if last_default_tab[0] == "Others": # New tabs don't exist yet in image_browser_active_tabs, add them with wib_db.transaction() as cursor: wib_db.update_db_data(cursor, "last_default_tab", "Maintenance") if hasattr(opts, "image_browser_active_tabs"): active_and_new_tabs = f"{opts.image_browser_active_tabs}, All, Maintenance" shared.opts.__setattr__("image_browser_active_tabs", active_and_new_tabs) shared.opts.save(shared.config_filename) check_image_browser_active_tabs() tabs_list = [tab.strip() for tab in chain.from_iterable(csv.reader(StringIO(opts.image_browser_active_tabs))) if tab] if hasattr(opts, "image_browser_active_tabs") and opts.image_browser_active_tabs != "" else default_tab_options path_maps = { "txt2img": opts.outdir_samples or opts.outdir_txt2img_samples, "img2img": opts.outdir_samples or opts.outdir_img2img_samples, "txt2img-grids": opts.outdir_grids or opts.outdir_txt2img_grids, "img2img-grids": opts.outdir_grids or opts.outdir_img2img_grids, "Extras": opts.outdir_samples or opts.outdir_extras_samples, favorite_tab_name: opts.outdir_save } class ImageBrowserTab(): seen_base_tags = set() def __init__(self, name: str): self.name: str = os.path.basename(name) if os.path.isdir(name) else name self.path: str = os.path.realpath(path_maps.get(name, name)) self.base_tag: str = f"image_browser_tab_{self.get_unique_base_tag(self.remove_invalid_html_tag_chars(self.name).lower())}" def remove_invalid_html_tag_chars(self, tag: str) -> str: # Removes any character that is not a letter, a digit, a hyphen, or an underscore removed = re.sub(r"[^a-zA-Z0-9\-_]", "", tag) return removed def get_unique_base_tag(self, base_tag: str) -> str: counter = 1 while base_tag in self.seen_base_tags: match = re.search(r"_(\d+)$", base_tag) if match: counter = int(match.group(1)) + 1 base_tag = re.sub(r"_(\d+)$", f"_{counter}", base_tag) else: base_tag = f"{base_tag}_{counter}" counter += 1 self.seen_base_tags.add(base_tag) return base_tag def __str__(self): return f"Name: {self.name} / Path: {self.path} / Base tag: {self.base_tag} / Seen base tags: {self.seen_base_tags}" tabs_list = [ImageBrowserTab(tab) for tab in tabs_list] # Set temp_dir from webui settings, so gradio uses it if shared.opts.temp_dir != "": tempfile.tempdir = shared.opts.temp_dir if forge: # Workaround for forge: Its ui_tempdir seems to not use the temp_dir configuration # The following makes sure, that onchange triggers after setting temp_dir shared.opts.temp_dir = temp_temp shared.opts.temp_dir = tempfile.tempdir debug_level_types = ["none", "warning log", "debug log", "javascript log", "capture logs to file"] debug_levels_list = [] for i in range(len(debug_level_types)): level = debug_level_types[i].split(" ")[0] text = str(i) + " - " + debug_level_types[i] debug_levels_list.append((level, text)) def debug_levels(arg_value=None, arg_level=None, arg_text=None): if arg_value is not None: return arg_value, debug_levels_list[arg_value] elif arg_level is not None: for i, (level, text) in enumerate(debug_levels_list): if level == arg_level: return i, debug_levels_list[i] elif arg_text is not None: for i, (level, text) in enumerate(debug_levels_list): if text == arg_text: return i, debug_levels_list[i] # Logging logger = None logger_mode = None def setup_file_handler(): handler_active = False handlers = logger.handlers for handler in handlers: if isinstance(handler, logging.FileHandler): if os.path.basename(handler.baseFilename) == os.path.basename(log_file): handler_active = True break if not handler_active: file_handler = logging.FileHandler(log_file) file_handler.setLevel(logger_mode) formatter = logging.Formatter(f"%(asctime)s image_browser.py: %(message)s", datefmt="%Y-%m-%d-%H:%M:%S") file_handler.setFormatter(formatter) logger.addHandler(file_handler) def setup_debug(): global logger, logger_mode try: common_logger = True logger = shared.log except AttributeError: common_logger = False logger = logging.getLogger(__name__) logger.disabled = False logger_mode = logging.ERROR level_value = 0 capture_level_value = 99 if hasattr(opts, "image_browser_debug_level"): warning_level_value, (warning_level, warning_level_text) = debug_levels(arg_level="warning") debug_level_value, (debug_level, debug_level_text) = debug_levels(arg_level="debug") capture_level_value, (capture_level, capture_level_text) = debug_levels(arg_level="capture") level_value, (level, level_text) = debug_levels(arg_text=opts.image_browser_debug_level) if level_value >= debug_level_value: logger_mode = logging.DEBUG elif level_value >= warning_level_value: logger_mode = logging.WARNING logger.setLevel(logger_mode) if not common_logger: console_handler = logging.StreamHandler() console_handler.setLevel(logger_mode) formatter = logging.Formatter(f"%(asctime)s image_browser.py: %(message)s", datefmt="%Y-%m-%d-%H:%M:%S") console_handler.setFormatter(formatter) logger.addHandler(console_handler) if level_value >= capture_level_value: try: os.unlink(log_file) except FileNotFoundError: pass setup_file_handler() logger.warning(f"debug_level: {level_value}") # Debug logging if logger.getEffectiveLevel() == logging.DEBUG: logger.debug(f"{sys.executable} {sys.version}") logger.debug(f"{platform.system()} {platform.version()}") try: git = os.environ.get("GIT", "git") webui_commit_hash = os.popen(f"{git} rev-parse HEAD").read().strip() sm_hashes = os.popen(f"{git} submodule").read() sm_hashes_lines = sm_hashes.splitlines() image_browser_commit_hash = f"image_browser_commit_hash not found: {sm_hashes}" for sm_hashes_line in sm_hashes_lines: if "images-browser" in sm_hashes_line.lower(): image_browser_commit_hash = sm_hashes_line[1:41] break except Exception as e: webui_commit_hash = e image_browser_commit_hash = e logger.debug(f"Webui {webui_commit_hash}") logger.debug(f"Image Browser {image_browser_commit_hash}") logger.debug(f"Gradio {gr.__version__}") logger.debug(f"{paths.script_path}") with open(cmd_opts.ui_config_file, "r", encoding="utf-8") as f: logger.debug(f.read()) with open(cmd_opts.ui_settings_file, "r", encoding="utf-8") as f: logger.debug(f.read()) logger.debug(os.path.realpath(__file__)) logger.debug([str(tab) for tab in tabs_list]) logger.debug(f"db_version: {db_version}") setup_debug() def delete_recycle(filename): if opts.image_browser_delete_recycle and send2trash_installed: send2trash(filename) else: file = Path(filename) file.unlink() return def img_path_subdirs_get(img_path): subdirs = [] subdirs.append(none_select) for item in os.listdir(img_path): item_path = os.path.join(img_path, item) if os.path.isdir(item_path): subdirs.append(item_path) return gr.update(choices=subdirs) def img_path_add_remove(img_dir, path_recorder, add_remove, img_path_depth): img_dir = os.path.realpath(img_dir) if add_remove == "add" or (add_remove == "remove" and img_dir in path_recorder): if add_remove == "add": path_recorder[img_dir] = { "depth": int(img_path_depth), "path_display": f"{img_dir} [{int(img_path_depth)}]" } wib_db.update_path_recorder(img_dir, path_recorder[img_dir]["depth"], path_recorder[img_dir]["path_display"]) else: del path_recorder[img_dir] wib_db.delete_path_recorder(img_dir) path_recorder_formatted = [value.get("path_display") for key, value in path_recorder.items()] path_recorder_formatted = sorted(path_recorder_formatted, key=lambda x: natural_keys(x.lower())) if add_remove == "remove": selected = path_recorder[list(path_recorder.keys())[0]]["path_display"] else: selected = path_recorder[img_dir]["path_display"] return path_recorder, gr.update(choices=path_recorder_formatted, value=selected) def sort_order_flip(turn_page_switch, sort_order): if sort_order == up_symbol: sort_order = down_symbol else: sort_order = up_symbol return 1, turn_page_switch + 1, sort_order def read_path_recorder(): path_recorder = wib_db.load_path_recorder() path_recorder_formatted = [value.get("path_display") for key, value in path_recorder.items()] path_recorder_formatted = sorted(path_recorder_formatted, key=lambda x: natural_keys(x.lower())) path_recorder_unformatted = list(path_recorder.keys()) path_recorder_unformatted = sorted(path_recorder_unformatted, key=lambda x: natural_keys(x.lower())) return path_recorder, path_recorder_formatted, path_recorder_unformatted def pure_path(path): if path == []: return path, 0 match = re.search(r" \[(\d+)\]$", path) if match: path = path[:match.start()] depth = int(match.group(1)) else: depth = 0 path = os.path.realpath(path) return path, depth def browser2path(img_path_browser): img_path, _ = pure_path(img_path_browser) return img_path def totxt(file): base, _ = os.path.splitext(file) file_txt = base + ".txt" return file_txt def tab_select(): path_recorder, path_recorder_formatted, path_recorder_unformatted = read_path_recorder() return path_recorder, gr.update(choices=path_recorder_unformatted) def js_logs_output(js_log): logger.debug(f"js_log: {js_log}") return js_log def ranking_filter_settings(page_index, turn_page_switch, ranking_filter): if ranking_filter == "Min-max": interactive = True else: interactive = False page_index = 1 return page_index, turn_page_switch + 1, gr.update(interactive=interactive), gr.update(interactive=interactive) def reduplicative_file_move(src, dst): def same_name_file(basename, path): name, ext = os.path.splitext(basename) f_list = os.listdir(path) max_num = 0 for f in f_list: if len(f) <= len(basename): continue f_ext = f[-len(ext):] if len(ext) > 0 else "" if f[:len(name)] == name and f_ext == ext: if f[len(name)] == "(" and f[-len(ext)-1] == ")": number = f[len(name)+1:-len(ext)-1] if number.isdigit(): if int(number) > max_num: max_num = int(number) return f"{name}({max_num + 1}){ext}" name = os.path.basename(src) save_name = os.path.join(dst, name) src_txt_exists = False if opts.image_browser_txt_files: src_txt = totxt(src) if os.path.exists(src_txt): src_txt_exists = True if not os.path.exists(save_name): if opts.image_browser_copy_image: shutil.copy2(src, dst) if opts.image_browser_txt_files and src_txt_exists: shutil.copy2(src_txt, dst) else: shutil.move(src, dst) if opts.image_browser_txt_files and src_txt_exists: shutil.move(src_txt, dst) else: name = same_name_file(name, dst) if opts.image_browser_copy_image: shutil.copy2(src, os.path.join(dst, name)) if opts.image_browser_txt_files and src_txt_exists: shutil.copy2(src_txt, totxt(os.path.join(dst, name))) else: shutil.move(src, os.path.join(dst, name)) if opts.image_browser_txt_files and src_txt_exists: shutil.move(src_txt, totxt(os.path.join(dst, name))) def save_image(file_name, filenames, page_index, turn_page_switch, dest_path): if file_name is not None and os.path.exists(file_name): try: # Check if txt file exists and should be handled src_txt_exists = False src_txt = None if opts.image_browser_txt_files: src_txt = totxt(file_name) if os.path.exists(src_txt): src_txt_exists = True # Use the existing save_files function from ui_common.py # First, we need to prepare the data in the format expected by save_files # Read image metadata from PIL import Image try: image = Image.open(file_name) geninfo, items = images.read_info_from_image(image) # Parse generation parameters if available if geninfo: # Use the already imported sendto module for parameter parsing parameters = sendto.parse_generation_parameters(geninfo, []) else: parameters = {} # Create data structure expected by save_files js_data = { "infotexts": [geninfo or ""], "index_of_first_image": 0, "width": image.width, "height": image.height, "sampler_name": parameters.get("Sampler", "Unknown"), "cfg_scale": parameters.get("CFG scale", 7.0), "steps": parameters.get("Steps", 20), "sd_model_name": parameters.get("Model", "Unknown"), "sd_model_hash": parameters.get("Model hash", "Unknown") } # Prepare image data as expected by save_files images_data = [(image, None)] # (image, info) tuple # Temporarily change the output directory to our destination original_outdir = shared.opts.outdir_save shared.opts.outdir_save = dest_path try: # Call the existing save_files function file_result, message_result = save_files( json.dumps(js_data), images_data, False, # do_make_zip 0 # index ) message = message_result # Handle .txt file copying/moving manually since save_files doesn't know about it if src_txt_exists and file_result and "value" in file_result and file_result["value"]: # Get the saved image filename from the result saved_files = file_result["value"] if isinstance(file_result["value"], list) else [file_result["value"]] if saved_files: # Find the image file (not zip) saved_image_path = None for saved_file in saved_files: if isinstance(saved_file, str) and not saved_file.endswith(".zip"): saved_image_path = saved_file break if saved_image_path: # Calculate destination txt file path dest_txt = totxt(saved_image_path) try: if opts.image_browser_copy_image: shutil.copy2(src_txt, dest_txt) else: shutil.move(src_txt, dest_txt) except Exception as e: logger.warning(f"Failed to handle txt file: {e}") finally: # Restore original output directory shared.opts.outdir_save = original_outdir # If we're moving (not copying), remove the original file if not opts.image_browser_copy_image: try: os.remove(file_name) # txt file should already be moved above, but clean up if it wasn't if src_txt_exists and os.path.exists(src_txt): os.remove(src_txt) except Exception as e: logger.warning(f"Failed to remove original file: {e}") # Force page refresh with checking filenames filenames = [] turn_page_switch += 1 except Exception as e: logger.warning(f"Failed to use save_files function, falling back to reduplicative_file_move: {e}") # Fallback to original method reduplicative_file_move(file_name, dest_path) message = f"
{plaintext_to_html(str(key))}
{plaintext_to_html(str(text))}
Favorites path from settings: {opts.outdir_save}") with gr.Row(visible=others_dir): with gr.Column(scale=10): img_path = gr.Textbox(dir_name, label="Images directory", placeholder="Input images directory", interactive=others_dir) with gr.Column(scale=1): img_path_depth = gr.Number(value="0", label="Sub directory depth") with gr.Column(scale=1): img_path_save_button = gr.Button(value="Add to / replace in saved directories") with gr.Row(visible=others_dir): with gr.Column(scale=10): img_path_browser = gr.Dropdown(choices=path_recorder_formatted, label="Saved directories") with gr.Column(scale=1): img_path_remove_button = gr.Button(value="Remove from saved directories") with gr.Row(visible=others_dir): with gr.Column(scale=10): img_path_subdirs = gr.Dropdown(choices=[none_select], value=none_select, label="Sub directories", interactive=True, elem_id=f"{tab.base_tag}_img_path_subdirs") with gr.Column(scale=1): img_path_subdirs_button = gr.Button(value="Get sub directories") with gr.Row(visible=standard_ui, elem_id=f"{tab.base_tag}_image_browser") as main_panel: with gr.Column(): with gr.Row(): with gr.Column(scale=2): with gr.Row(elem_id=f"{tab.base_tag}_image_browser_gallery_controls") as gallery_controls_panel: with gr.Column(scale=4, min_width=20): first_page = gr.Button("First Page", elem_id=f"{tab.base_tag}_control_image_browser_first_page") with gr.Column(scale=4, min_width=20): prev_page = gr.Button("Prev Page", elem_id=f"{tab.base_tag}_control_image_browser_prev_page") with gr.Row(elem_classes="page-index-panel"): page_index = gr.Number(value=1, label="Page Index", elem_id=f"{tab.base_tag}_control_image_browser_page_index") with gr.Column(scale=1, min_width=20, elem_classes="refresh-index-panel"): refresh_index_button = ToolButton(value=refresh_symbol, elem_id=f"{tab.base_tag}_control_image_browser_refresh_index") with gr.Column(scale=4, min_width=20): next_page = gr.Button("Next Page", elem_id=f"{tab.base_tag}_control_image_browser_next_page") with gr.Column(scale=4, min_width=20): end_page = gr.Button("End Page", elem_id=f"{tab.base_tag}_control_image_browser_end_page") with gr.Row(): with gr.Column(scale=5, min_width=40, visible=False) as ranking_panel: with gr.Row(): with gr.Column(scale=1, min_width=20): ranking_current = gr.Textbox(value="None", label="Current ranking", interactive=False) with gr.Column(scale=4, min_width=20): ranking = gr.Radio(choices=["1", "2", "3", "4", "5", "None"], label="Set ranking to", elem_id=f"{tab.base_tag}_control_image_browser_ranking", interactive=True) with gr.Column(scale=1, min_width=20, visible=False) as video_checkbox_panel: video_checkbox = gr.Checkbox(value=False, label="Show video frame", elem_id=f"{tab.base_tag}_image_browser_video_checkbox") with gr.Column(scale=5, min_width=40): gr.HTML(" ") if opts.image_browser_video_pos == "Above": with gr.Row(): video_element = gr.Video(visible=False, width=opts.image_browser_video_x, height=opts.image_browser_video_y) with gr.Row(): if version.parse(gr.__version__) < version.parse(gradio3_new_gallery_syntax): # Version for Gradio 3 image_gallery = gr.Gallery(show_label=False, elem_id=f"{tab.base_tag}_image_browser_gallery").style(columns=opts.image_browser_page_columns, height=("max-content" if opts.image_browser_height_auto else None)) elif version.parse(gr.__version__) >= version.parse(gradio4_new_gallery_syntax): # Most up-to-date version image_gallery = gr.Gallery(show_label=False, elem_id=f"{tab.base_tag}_image_browser_gallery", interactive=False, columns=opts.image_browser_page_columns, height=("max-content" if opts.image_browser_height_auto else None)) else: # Version for Gradio 4.0 - 4.15 image_gallery = gr.Gallery(show_label=False, elem_id=f"{tab.base_tag}_image_browser_gallery", columns=opts.image_browser_page_columns, height=("max-content" if opts.image_browser_height_auto else None)) if opts.image_browser_video_pos == "Below": with gr.Row(): video_element = gr.Video(visible=False, width=opts.image_browser_video_x, height=opts.image_browser_video_y) with gr.Row() as delete_panel: with gr.Column(scale=1): delete_num = gr.Number(value=1, interactive=True, label="delete next", elem_id=f"{tab.base_tag}_image_browser_del_num") delete_confirm = gr.Checkbox(value=False, label="also delete off-screen images") with gr.Column(scale=3): delete = gr.Button("Delete", elem_id=f"{tab.base_tag}_image_browser_del_img_btn") with gr.Row() as info_add_panel: with gr.Box(visible=opts.image_browser_info_add): gr.HTML("
Choose Min-max to activate these controls
") with gr.Box() as aesthetic_score_filter_panel: with gr.Row(): aes_filter_min = gr.Textbox(value="", label="Minimum score") aes_filter_max = gr.Textbox(value="", label="Maximum score") with gr.Box() as generation_info_panel: with gr.Row(): img_file_info_format = gr.Checkbox(value=opts.image_browser_info_format, label="Formatted display") with gr.Row(): img_file_info = gr.Textbox(label="Generation Info", interactive=False, lines=6, visible=not opts.image_browser_info_format) img_file_info_formatted = gr.DataFrame(label="Generation Info", headers=["Key", "Value"], wrap=True, interactive=False, visible=opts.image_browser_info_format, elem_classes="image_browser_file_info_formatted") with gr.Row() as filename_panel: img_file_name = gr.Textbox(value="", label="File Name", interactive=False) with gr.Row() as filetime_panel: img_file_time= gr.HTML() with gr.Row() as open_folder_panel: if tab.name == "All": open_folder_button = gr.Button(folder_symbol, visible=False) else: open_folder_button = gr.Button(folder_symbol, visible=standard_ui or others_dir) gr.HTML(" ") gr.HTML(" ") gr.HTML(" ") with gr.Row(elem_id=f"{tab.base_tag}_image_browser_button_panel", visible=False) as button_panel: with gr.Column(): with gr.Row(): if tab.name == favorite_tab_name: favorites_btn_show = False else: favorites_btn_show = True favorites_btn = gr.Button(f"{copy_move[opts.image_browser_copy_image]} to favorites", elem_id=f"{tab.base_tag}_image_browser_favorites_btn", visible=favorites_btn_show) try: send_to_buttons = sendto.create_buttons(["txt2img", "img2img", "inpaint", "extras"]) except: pass sendto_openoutpaint = gr.Button("Send to openOutpaint", elem_id=f"{tab.base_tag}_image_browser_openoutpaint_btn", visible=openoutpaint) with gr.Row(visible=controlnet): sendto_controlnet_txt2img = gr.Button("Send to txt2img ControlNet", visible=controlnet) sendto_controlnet_img2img = gr.Button("Send to img2img ControlNet", visible=controlnet) with gr.Row(visible=controlnet): if "control_net_unit_count" in opts.data: controlnet_max = opts.data.get("control_net_unit_count", 3) else: controlnet_max = opts.data.get("control_net_max_models_num", 3) sendto_controlnet_num = gr.Dropdown([str(i) for i in range(controlnet_max)], label="ControlNet number", value="0", interactive=True, visible=(controlnet and controlnet_max > 1)) if controlnet_max is None: sendto_controlnet_type = gr.Textbox(value="none", visible=False) elif controlnet_max == 1: sendto_controlnet_type = gr.Textbox(value="single", visible=False) else: sendto_controlnet_type = gr.Textbox(value="multiple", visible=False) with gr.Row(elem_id=f"{tab.base_tag}_image_browser_to_dir_panel", visible=False) as to_dir_panel: with gr.Column(): with gr.Row(): to_dir_path = gr.Textbox(label="Directory path") with gr.Row(): to_dir_saved = gr.Dropdown(choices=path_recorder_unformatted, label="Saved directories") with gr.Row(): to_dir_btn = gr.Button(f"{copy_move[opts.image_browser_copy_image]} to directory", elem_id=f"{tab.base_tag}_image_browser_to_dir_btn") with gr.Row(): collected_warning = gr.HTML() with gr.Row(visible=False): renew_page = gr.Button("Renew Page", elem_id=f"{tab.base_tag}_image_browser_renew_page") visible_img_num = gr.Number() tab_base_tag_box = gr.Textbox(tab.base_tag) image_index = gr.Textbox(value=-1, elem_id=f"{tab.base_tag}_image_browser_image_index") set_index = gr.Button("set_index", elem_id=f"{tab.base_tag}_image_browser_set_index") filenames = gr.State([]) hidden = gr.Image(type="pil", elem_id=f"{tab.base_tag}_image_browser_hidden_image") image_page_list = gr.Textbox(elem_id=f"{tab.base_tag}_image_browser_image_page_list") info1 = gr.Textbox() info2 = gr.Textbox() # turn_page_switch: reload page at thumbnail overview, requests add 1 to trigger change-event turn_page_switch = gr.Number(value=1, label="turn_page_switch") load_switch = gr.Number(value=1, label="load_switch") to_dir_load_switch = gr.Number(value=1, label="to_dir_load_switch") to_dir_load_switch_panel = gr.Checkbox(value=True, label="to_dir_load_switch_panel") select_image = gr.Number(value=1) img_path_add = gr.Textbox(value="add") img_path_remove = gr.Textbox(value="remove") favorites_path = gr.Textbox(value=opts.outdir_save) mod_keys = "" if opts.image_browser_mod_ctrl_shift: mod_keys = f"{mod_keys}CS" elif opts.image_browser_mod_shift: mod_keys = f"{mod_keys}S" image_browser_mod_keys = gr.Textbox(value=mod_keys, elem_id=f"{tab.base_tag}_image_browser_mod_keys") image_browser_prompt = gr.Textbox(elem_id=f"{tab.base_tag}_image_browser_prompt") image_browser_neg_prompt = gr.Textbox(elem_id=f"{tab.base_tag}_image_browser_neg_prompt") js_logs = gr.Textbox() image_browser_img_info = gr.Textbox(value="[]", elem_id=f"{tab.base_tag}_image_browser_img_info") # Maintenance tab with gr.Row(visible=maint): with gr.Column(scale=4): gr.HTML(f"{caution_symbol} Caution: You should only use these options if you know what you are doing. {caution_symbol}") with gr.Column(scale=3): maint_wait = gr.HTML("Status:") with gr.Column(scale=7): gr.HTML(" ") with gr.Row(visible=maint): maint_last_msg = gr.Textbox(label="Last message", interactive=False) with gr.Row(visible=maint): with gr.Column(scale=1): maint_exif_rebuild = gr.Button(value="Rebuild exif cache") with gr.Column(scale=1): maint_exif_delete_0 = gr.Button(value="Delete 0-entries from exif cache") with gr.Column(scale=10): gr.HTML(visible=False) with gr.Row(visible=maint): with gr.Column(scale=2): maint_update_dirs = gr.Button(value="Update directory names in database") with gr.Column(scale=2, min_width=40): maint_update_dirs_path_recorder = gr.Checkbox(value=True, label="path_recorder") with gr.Column(scale=2, min_width=40): maint_update_dirs_exif_data = gr.Checkbox(value=True, label="exif_data") with gr.Column(scale=2, min_width=40): maint_update_dirs_ranking = gr.Checkbox(value=True, label="ranking") with gr.Column(scale=10): maint_update_dirs_from = gr.Textbox(label="From (full path)") with gr.Column(scale=10): maint_update_dirs_to = gr.Textbox(label="to (full path)") with gr.Row(visible=maint): with gr.Column(scale=1): maint_recreate_hash = gr.Button(value="Recreate hash for existing files") with gr.Column(scale=10): gr.HTML(visible=False) with gr.Row(visible=maint): with gr.Column(scale=1): maint_reapply_ranking = gr.Button(value="Reapply ranking after moving files") with gr.Column(scale=10): gr.HTML(visible=False) with gr.Row(visible=maint): with gr.Column(scale=1): maint_restart_debug = gr.Button(value="Restart debug") with gr.Column(scale=10): gr.HTML(visible=False) with gr.Row(visible=maint): with gr.Column(scale=1): maint_get_js_logs = gr.Button(value="Get javascript logs") with gr.Column(scale=10): maint_show_logs = gr.Textbox(label="Javascript logs", lines=10, interactive=False) with gr.Row(visible=False): with gr.Column(scale=1): maint_rebuild_ranking = gr.Button(value="Rebuild ranking from exif info") with gr.Column(scale=10): gr.HTML(visible=False) # Hide components based on opts.image_browser_hidden_components hidden_component_map = { "Sort by": sort_panel, "Filename keyword search": filename_search_panel, "EXIF keyword search": exif_search_panel, "Ranking Filter": ranking_filter_panel, "Aesthestic Score": aesthetic_score_filter_panel, "Generation Info": generation_info_panel, "File Name": filename_panel, "File Time": filetime_panel, "Open Folder": open_folder_panel, "Send to buttons": button_panel, "Copy to directory": to_dir_panel, "Gallery Controls Bar": gallery_controls_panel, "Ranking Bar": ranking_panel, "Delete Bar": delete_panel, "Additional Generation Info": info_add_panel } if set(hidden_component_map.keys()) != set(components_list): logger.warning(f"Invalid items present in either hidden_component_map or components_list. Make sure when adding new components they are added to both.") override_hidden = set() if hasattr(opts, "image_browser_hidden_components"): for item in opts.image_browser_hidden_components: hidden_component_map[item].visible = False override_hidden.add(hidden_component_map[item]) change_dir_outputs = [warning_box, main_panel, img_path_browser, path_recorder, load_switch, img_path, img_path_depth] img_path.submit(change_dir_textbox, inputs=[img_path, path_recorder, load_switch, img_path_browser, img_path_depth], outputs=change_dir_outputs, show_progress=show_progress_setting) img_path_browser.change(change_dir_dropdown, inputs=[img_path_browser, path_recorder, load_switch, img_path_browser, img_path_depth], outputs=change_dir_outputs, show_progress=show_progress_setting) to_dir_saved.change(change_dir_dropdown, inputs=[to_dir_saved, path_recorder, to_dir_load_switch, to_dir_saved, img_path_depth], outputs=[warning_box, to_dir_load_switch_panel, to_dir_saved, path_recorder, to_dir_load_switch, to_dir_path, img_path_depth], show_progress=show_progress_setting) #delete delete.click( fn=delete_image, inputs=[tab_base_tag_box, delete_num, img_file_name, filenames, image_index, visible_img_num, delete_confirm, turn_page_switch, image_page_list], outputs=[filenames, delete_num, turn_page_switch, visible_img_num, image_gallery, select_image, image_page_list], show_progress=show_progress_setting ).then( fn=None, _js="image_browser_select_image", inputs=[tab_base_tag_box, image_index, select_image], outputs=[js_dummy_return], show_progress=show_progress_setting ) to_dir_btn.click(save_image, inputs=[img_file_name, filenames, page_index, turn_page_switch, to_dir_path], outputs=[collected_warning, filenames, page_index, turn_page_switch], show_progress=show_progress_setting) #turn page first_page.click(lambda s:(1, s + 1) , inputs=[turn_page_switch], outputs=[page_index, turn_page_switch], show_progress=show_progress_setting) next_page.click(lambda p, s: (p + 1, s + 1), inputs=[page_index, turn_page_switch], outputs=[page_index, turn_page_switch], show_progress=show_progress_setting) prev_page.click(lambda p, s: (p - 1, s + 1), inputs=[page_index, turn_page_switch], outputs=[page_index, turn_page_switch], show_progress=show_progress_setting) end_page.click(lambda s: (-1, s + 1), inputs=[turn_page_switch], outputs=[page_index, turn_page_switch], show_progress=show_progress_setting) load_switch.change(lambda s:(1, s + 1), inputs=[turn_page_switch], outputs=[page_index, turn_page_switch], show_progress=show_progress_setting) filename_keyword_search.submit(lambda s:(1, s + 1), inputs=[turn_page_switch], outputs=[page_index, turn_page_switch], show_progress=show_progress_setting) exif_keyword_search.submit(lambda s:(1, s + 1), inputs=[turn_page_switch], outputs=[page_index, turn_page_switch], show_progress=show_progress_setting) ranking_filter_min.submit(lambda s:(1, s + 1), inputs=[turn_page_switch], outputs=[page_index, turn_page_switch], show_progress=show_progress_setting) ranking_filter_max.submit(lambda s:(1, s + 1), inputs=[turn_page_switch], outputs=[page_index, turn_page_switch], show_progress=show_progress_setting) aes_filter_min.submit(lambda s:(1, s + 1), inputs=[turn_page_switch], outputs=[page_index, turn_page_switch], show_progress=show_progress_setting) aes_filter_max.submit(lambda s:(1, s + 1), inputs=[turn_page_switch], outputs=[page_index, turn_page_switch], show_progress=show_progress_setting) sort_by.change(lambda s:(1, s + 1), inputs=[turn_page_switch], outputs=[page_index, turn_page_switch], show_progress=show_progress_setting) page_index.submit(lambda s: s + 1, inputs=[turn_page_switch], outputs=[turn_page_switch], show_progress=show_progress_setting) renew_page.click(lambda s: s + 1, inputs=[turn_page_switch], outputs=[turn_page_switch], show_progress=show_progress_setting) refresh_index_button.click(lambda p, s:(p, s + 1), inputs=[page_index, turn_page_switch], outputs=[page_index, turn_page_switch], show_progress=show_progress_setting) img_path_depth.change(lambda s: s + 1, inputs=[turn_page_switch], outputs=[turn_page_switch], show_progress=show_progress_setting) hide_on_thumbnail_view = [delete_panel, button_panel, ranking_panel, to_dir_panel, info_add_panel] sort_order.click( fn=sort_order_flip, inputs=[turn_page_switch, sort_order], outputs=[page_index, turn_page_switch, sort_order], show_progress=show_progress_setting ) ranking_filter.change( fn=ranking_filter_settings, inputs=[page_index, turn_page_switch, ranking_filter], outputs=[page_index, turn_page_switch, ranking_filter_min, ranking_filter_max], show_progress=show_progress_setting ) img_file_info_format.change( fn=img_file_info_format_visibility, inputs=[img_file_info_format], outputs=[img_file_info, img_file_info_formatted], show_progress=show_progress_setting ) img_file_info.change( fn=img_file_info_do_format, inputs=[img_file_info], outputs=[img_file_info_formatted], show_progress=show_progress_setting ) # Others img_path_subdirs_button.click( fn=img_path_subdirs_get, inputs=[img_path], outputs=[img_path_subdirs], show_progress=show_progress_setting ) img_path_subdirs.change( fn=change_dir_dropdown, inputs=[img_path_subdirs, path_recorder, load_switch, img_path_browser, img_path_depth], outputs=change_dir_outputs, show_progress=show_progress_setting ) img_path_save_button.click( fn=img_path_add_remove, inputs=[img_path, path_recorder, img_path_add, img_path_depth], outputs=[path_recorder, img_path_browser], show_progress=show_progress_setting ) img_path_remove_button.click( fn=img_path_add_remove, inputs=[img_path, path_recorder, img_path_remove, img_path_depth], outputs=[path_recorder, img_path_browser], show_progress=show_progress_setting ) maint_exif_rebuild.click( fn=exif_rebuild, inputs=[maint_wait], outputs=[maint_wait, maint_last_msg], show_progress=True ) maint_exif_delete_0.click( fn=exif_delete_0, inputs=[maint_wait], outputs=[maint_wait, maint_last_msg], show_progress=True ) maint_update_dirs.click( fn=exif_update_dirs, inputs=[maint_update_dirs_path_recorder, maint_update_dirs_exif_data, maint_update_dirs_ranking, maint_update_dirs_from, maint_update_dirs_to, maint_wait], outputs=[maint_wait, maint_last_msg], show_progress=True ) maint_recreate_hash.click( fn=recreate_hash, inputs=[maint_wait], outputs=[maint_wait, maint_last_msg], show_progress=True ) maint_reapply_ranking.click( fn=reapply_ranking, inputs=[path_recorder, maint_wait], outputs=[maint_wait, maint_last_msg], show_progress=True ) maint_restart_debug.click( fn=restart_debug, inputs=[maint_wait], outputs=[maint_wait, maint_last_msg], show_progress=True ) maint_get_js_logs.click( fn=js_logs_output, _js="get_js_logs", inputs=[js_logs], outputs=[maint_show_logs], show_progress=True ) # other functions set_index.click( fn=show_image_info, _js="image_browser_get_current_img", inputs=[tab_base_tag_box, image_index, page_index, filenames, turn_page_switch, image_gallery], outputs=[img_file_name, img_file_time, image_index, hidden, turn_page_switch, img_file_info_add, image_gallery, video_checkbox, video_checkbox_panel], show_progress=show_progress_setting ).then( fn=None, _js="image_browser_img_show_progress_update", inputs=[tab_base_tag_box], outputs=[js_dummy_return], show_progress=show_progress_setting ) set_index.click(fn=lambda:(gr.update(visible=delete_panel not in override_hidden), gr.update(visible=button_panel not in override_hidden), gr.update(visible=ranking_panel not in override_hidden), gr.update(visible=to_dir_panel not in override_hidden), gr.update(visible=info_add_panel not in override_hidden)), inputs=None, outputs=hide_on_thumbnail_view, show_progress=show_progress_setting) favorites_btn.click(save_image, inputs=[img_file_name, filenames, page_index, turn_page_switch, favorites_path], outputs=[collected_warning, filenames, page_index, turn_page_switch], show_progress=show_progress_setting) img_file_name.change(img_file_name_changed, inputs=[img_file_name, favorites_btn, to_dir_btn], outputs=[ranking_current, ranking, collected_warning, favorites_btn, to_dir_btn], show_progress=show_progress_setting) hidden.change(fn=run_pnginfo, inputs=[hidden, img_path, img_file_name], outputs=[info1, img_file_info, info2, image_browser_prompt, image_browser_neg_prompt], show_progress=show_progress_setting) #ranking ranking.change(update_ranking, inputs=[img_file_name, ranking_current, ranking, img_file_info], outputs=[ranking_current, ranking, img_file_info], show_progress=show_progress_setting) try: sendto.bind_buttons(send_to_buttons, hidden, img_file_info) except: pass if standard_ui: current_gr_tab.select( fn=tab_select, inputs=[], outputs=[path_recorder, to_dir_saved], show_progress=show_progress_setting ) open_folder_button.click( fn=lambda: open_folder(dir_name), inputs=[], outputs=[], show_progress=show_progress_setting ) elif others_dir: open_folder_button.click( fn=open_folder, inputs=[img_path], outputs=[], show_progress=show_progress_setting ) if standard_ui or others_dir: turn_page_switch.change( fn=get_image_page, inputs=[img_path, page_index, filenames, filename_keyword_search, sort_by, sort_order, tab_base_tag_box, img_path_depth, ranking_filter, ranking_filter_min, ranking_filter_max, aes_filter_min, aes_filter_max, exif_keyword_search, negative_prompt_search, use_regex], outputs=[filenames, page_index, image_gallery, img_file_name, img_file_time, img_file_info, visible_img_num, warning_box, hidden, image_page_list, image_browser_img_info, video_checkbox, video_checkbox_panel], show_progress=show_progress_setting ).then( fn=None, _js="image_browser_turnpage", inputs=[tab_base_tag_box], outputs=[js_dummy_return], show_progress=show_progress_setting ) turn_page_switch.change(fn=lambda:(gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)), inputs=None, outputs=hide_on_thumbnail_view, show_progress=show_progress_setting) sendto_openoutpaint.click( fn=None, inputs=[tab_base_tag_box, image_index, image_browser_prompt, image_browser_neg_prompt], outputs=[js_dummy_return], _js="image_browser_openoutpaint_send", show_progress=show_progress_setting ) sendto_controlnet_txt2img.click( fn=None, inputs=[tab_base_tag_box, image_index, sendto_controlnet_num, sendto_controlnet_type], outputs=[js_dummy_return], _js="image_browser_controlnet_send_txt2img", show_progress=show_progress_setting ) sendto_controlnet_img2img.click( fn=None, inputs=[tab_base_tag_box, image_index, sendto_controlnet_num, sendto_controlnet_type], outputs=[js_dummy_return], _js="image_browser_controlnet_send_img2img", show_progress=show_progress_setting ) video_checkbox.change( fn=show_video, inputs=[video_checkbox, img_file_name], outputs=[video_element], show_progress=show_progress_setting ) def run_pnginfo(image, image_path, image_file_name): if image is None or os.path.splitext(image_file_name)[1] not in image_ext_list: return "", "", "", "", "" try: geninfo, items = images.read_info_from_image(image) info = pnginfo2html(geninfo, items) except UnidentifiedImageError as e: geninfo = None info = "" if geninfo is None: try: filename = os.path.splitext(image_file_name)[0] + ".txt" geninfo = "" with open(filename) as f: for line in f: geninfo += line except Exception: logger.warning(f"run_pnginfo: No EXIF in image or txt file") if openoutpaint: prompt, neg_prompt = wib_db.select_prompts(image_file_name) if prompt == "0": prompt = "" if neg_prompt == "0": neg_prompt = "" else: prompt = "" neg_prompt = "" return "", geninfo, info, prompt, neg_prompt def on_ui_tabs(): global num_of_imgs_per_page, loads_files_num, js_dummy_return num_of_imgs_per_page = int(opts.image_browser_page_columns * opts.image_browser_page_rows) loads_files_num = int(opts.image_browser_pages_perload * num_of_imgs_per_page) with gr.Blocks(analytics_enabled=False) as image_browser: if version.parse(gr.__version__) < version.parse(gradio_min): gr.HTML(f'
You are running Gradio version {gr.__version__}. This version of the extension requires at least Gradio version {gradio_min}.
For more details see https://github.com/AlUlkesh/stable-diffusion-webui-images-browser/issues/116#issuecomment-1493259585
') else: with gr.Tabs(elem_id="image_browser_tabs_container") as tabs: js_dummy_return = gr.Textbox(interactive=False, visible=False) for i, tab in enumerate(tabs_list): with gr.Tab(tab.name, elem_id=f"{tab.base_tag}_image_browser_container") as current_gr_tab: with gr.Blocks(analytics_enabled=False): create_tab(tab, current_gr_tab) gr.Checkbox(value=opts.image_browser_preload, elem_id="image_browser_preload", visible=False) gr.Textbox(",".join( [tab.base_tag for tab in tabs_list] ), elem_id="image_browser_tab_base_tags_list", visible=False) gr.Checkbox(value=opts.image_browser_swipe, elem_id=f"image_browser_swipe", visible=False) javascript_level_value, (javascript_level, javascript_level_text) = debug_levels(arg_level="javascript") level_value, (level, level_text) = debug_levels(arg_text=opts.image_browser_debug_level) if level_value >= javascript_level_value: debug_level_option = level else: debug_level_option = "" gr.Textbox(value=debug_level_option, elem_id="image_browser_debug_level_option", visible=False) # Webui's ui_loadsave uses gradio labels as keys, this does not work with image browser, as the same labels are used on different tabs # For this reason the do_not_save_to_config attribute is added to each gradio element for key, value in image_browser.blocks.items(): setattr(value, "do_not_save_to_config", True) return (image_browser, "Image Browser", "image_browser"), def move_setting(cur_setting_name, old_setting_name, option_info, section, added): try: old_value = shared.opts.__getattr__(old_setting_name) except AttributeError: old_value = None try: new_value = shared.opts.__getattr__(cur_setting_name) except AttributeError: new_value = None if old_value is not None and new_value is None: # Add new option shared.opts.add_option(cur_setting_name, shared.OptionInfo(*option_info, section=section)) shared.opts.__setattr__(cur_setting_name, old_value) added = added + 1 # Remove old option shared.opts.data.pop(old_setting_name, None) return added def on_ui_settings(): # [current setting_name], [old setting_name], [default], [label], [component], [component_args] active_tabs_description = f"List of active tabs (separated by commas). Available options are {', '.join(default_tab_options)}. Custom folders are also supported by specifying their path." debug_level_choices = [] for i in range(len(debug_level_types)): level_value, (level, level_text) = debug_levels(arg_value=i) debug_level_choices.append(level_text) image_browser_options = [ ("image_browser_active_tabs", None, ", ".join(default_tab_options), active_tabs_description), ("image_browser_hidden_components", None, [], "Select components to hide", DropdownMulti, lambda: {"choices": components_list}), ("image_browser_with_subdirs", "images_history_with_subdirs", True, "Include images in sub directories"), ("image_browser_preload", "images_history_preload", False, "Preload images at startup for first tab"), ("image_browser_copy_image", "images_copy_image", False, "Move buttons copy instead of move"), ("image_browser_delete_message", "images_delete_message", True, "Print image deletion messages to the console"), ("image_browser_txt_files", "images_txt_files", True, "Move/Copy/Delete matching .txt files"), ("image_browser_debug_level", None, debug_level_choices[0], "Debug level", gr.Dropdown, lambda: {"choices": debug_level_choices}), ("image_browser_delete_recycle", "images_delete_recycle", True, "Use recycle bin when deleting images"), ("image_browser_scan_exif", "images_scan_exif", True, "Scan Exif-/.txt-data (initially slower, but required for many features to work)"), ("image_browser_mod_shift", None, False, "Change CTRL keybindings to SHIFT"), ("image_browser_mod_ctrl_shift", None, False, "or to CTRL+SHIFT"), ("image_browser_swipe", None, False, "Swipe left/right navigates to the next image"), ("image_browser_ranking_pnginfo", None, False, "Save ranking in image's pnginfo"), ("image_browser_page_columns", "images_history_page_columns", 6, "Number of columns on the page"), ("image_browser_page_rows", "images_history_page_rows", 6, "Number of rows on the page"), ("image_browser_pages_perload", "images_history_pages_perload", 20, "Minimum number of pages per load"), ("image_browser_height_auto", None, False, "Use automatic height for gallery (requires Gradio >= 3.36.0)"), ("image_browser_use_thumbnail", None, False, "Use optimized images in the thumbnail interface (significantly reduces the amount of data transferred)"), ("image_browser_thumbnail_size", None, 200, "Size of the thumbnails (px)"), ("image_browser_thumbnail_crop", None, False, "Crop thumbnail to square"), ("image_browser_img_tooltips", None, True, "Enable thumbnail tooltips"), ("image_browser_show_progress", None, True, "Show progress indicator"), ("image_browser_info_format", None, True, "Initially display Generation Info as formatted"), ("image_browser_info_order", None, "Prompt,Negative prompt,Model,VAE,Sampler,Schedule,Steps,CFG scale,Seed,Size", "Display order for Generation Info as formatted"), ("image_browser_info_add", None, False, "Show Additional Generation Info"), ("image_browser_video_pos", None, "Above", "Video above or below gallery", gr.Dropdown, lambda: {"choices": ["Above", "Below"]}), ("image_browser_video_x", None, 640, "Video player width (px)"), ("image_browser_video_y", None, 640, "Video player height (px)"), ] section = ("image-browser", "Image Browser") # Move historic setting names to current names added = 0 for cur_setting_name, old_setting_name, *option_info in image_browser_options: if old_setting_name is not None: added = move_setting(cur_setting_name, old_setting_name, option_info, section, added) if added > 0: shared.opts.save(shared.config_filename) for cur_setting_name, _, *option_info in image_browser_options: shared.opts.add_option(cur_setting_name, shared.OptionInfo(*option_info, section=section)) script_callbacks.on_ui_settings(on_ui_settings) script_callbacks.on_ui_tabs(on_ui_tabs)