import os import args_manager import modules.config import json import urllib.parse from PIL import Image from PIL.PngImagePlugin import PngInfo from modules.util import generate_temp_filename from modules.meta_parser import MetadataParser, get_exif log_cache = {} def get_current_html_path(output_format=None): output_format = output_format if output_format else modules.config.default_output_format date_string, local_temp_filename, only_name = generate_temp_filename(folder=modules.config.path_outputs, extension=output_format) html_name = os.path.join(os.path.dirname(local_temp_filename), 'log.html') return html_name def log(img, metadata, metadata_parser: MetadataParser | None = None, output_format=None) -> str: path_outputs = args_manager.args.temp_path if args_manager.args.disable_image_log else modules.config.path_outputs output_format = output_format if output_format else modules.config.default_output_format date_string, local_temp_filename, only_name = generate_temp_filename(folder=path_outputs, extension=output_format) os.makedirs(os.path.dirname(local_temp_filename), exist_ok=True) parsed_parameters = metadata_parser.parse_string(metadata.copy()) if metadata_parser is not None else '' image = Image.fromarray(img) if output_format == 'png': if parsed_parameters != '': pnginfo = PngInfo() pnginfo.add_text('parameters', parsed_parameters) pnginfo.add_text('fooocus_scheme', metadata_parser.get_scheme().value) else: pnginfo = None image.save(local_temp_filename, pnginfo=pnginfo) elif output_format == 'jpg': image.save(local_temp_filename, quality=95, optimize=True, progressive=True, exif=get_exif(parsed_parameters, metadata_parser.get_scheme().value) if metadata_parser else Image.Exif()) elif output_format == 'webp': image.save(local_temp_filename, quality=95, lossless=False, exif=get_exif(parsed_parameters, metadata_parser.get_scheme().value) if metadata_parser else Image.Exif()) else: image.save(local_temp_filename) if args_manager.args.disable_image_log: return local_temp_filename html_name = os.path.join(os.path.dirname(local_temp_filename), 'log.html') css_styles = ( "" ) js = ( """""" ) begin_part = f"Fooocus Log {date_string}{css_styles}{js}

Fooocus Log {date_string} (private)

\n

Metadata is embedded if enabled in the config or developer debug mode. You can find the information for each image in line Metadata Scheme.

\n\n" end_part = f'\n' middle_part = log_cache.get(html_name, "") if middle_part == "": if os.path.exists(html_name): existing_split = open(html_name, 'r', encoding='utf-8').read().split('') if len(existing_split) == 3: middle_part = existing_split[1] else: middle_part = existing_split[0] div_name = only_name.replace('.', '_') item = f"

\n" item += f"" item += "" item += "
{only_name}
" for label, key, value in metadata: value_txt = str(value).replace('\n', '
') item += f"\n" item += "" js_txt = urllib.parse.quote(json.dumps({k: v for _, k, v in metadata}, indent=0), safe='') item += f"
" item += "
\n\n" middle_part = item + middle_part with open(html_name, 'w', encoding='utf-8') as f: f.write(begin_part + middle_part + end_part) print(f'Image generated with private log at: {html_name}') log_cache[html_name] = middle_part return local_temp_filename