Allow local images to work with local HTML files. (#207)

* Make local images in local HTML files work.

* Make text2img send work with local files.

* Update the now-outdated info.

* Remove a couple of diagnostic prints.
pull/245/head
Morgon Kanter 2024-02-24 23:27:01 -05:00 committed by GitHub
parent 82d4bc4004
commit d35cec0bc0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 34 additions and 4 deletions

View File

@ -623,6 +623,12 @@ def cleaned_name(file_name):
return f"{clean_name}{extension}"
def fetch_and_process_image(image_url):
use_local = getattr(opts, "local_path_in_html", False)
if use_local:
image = Image.open(image_url)
geninfo, _ = read_info_from_image(image)
return geninfo
response = requests.get(image_url)
if response.status_code == 200:
image = Image.open(BytesIO(response.content))

View File

@ -1,3 +1,4 @@
import base64
import json
import gradio as gr
import urllib.request
@ -13,6 +14,7 @@ import hashlib
from pathlib import Path
from urllib.parse import urlparse
from PIL import Image
from sympy import preview
from modules.shared import cmd_opts, opts
from scripts.civitai_global import print
@ -340,6 +342,28 @@ def gen_sha256(file_path):
return hash_value
def convert_local_images(html):
soup = BeautifulSoup(html)
for simg in soup.find_all("img", attrs={"data-sampleimg": "true"}):
url = urlparse(simg["src"])
path = url.path
if not os.path.exists(path):
print("URL path does not exist: %s" % url.path)
# Try the raw url, files can be saved in windows as "C:\..." and
# that confuses urlparse because people only really test on Linux.
if os.path.exists(simg["src"]):
path = simg["src"]
else:
continue
with open(path, 'rb') as f:
imgdata = f.read()
b64img = base64.b64encode(imgdata).decode('utf-8')
imgtype = Image.open(io.BytesIO(imgdata)).format
if not imgtype:
imgtype = "PNG"
simg["src"] = f"data:image/{imgtype};base64,{b64img}"
return str(soup)
def model_from_sent(model_name, content_type, tile_count, path_input):
modelID_failed = False
output_html = None
@ -352,8 +376,6 @@ def model_from_sent(model_name, content_type, tile_count, path_input):
path_not_found = div + "Model ID not found.<br>Could not locate the model path.</div>"
offline = div + "CivitAI failed to respond.<br>The servers are likely offline."
if local_path_in_html:
use_local_html = False
if path_input == "Not Found":
model_name = re.sub(r'\.\d{3}$', '', model_name)
@ -391,6 +413,8 @@ def model_from_sent(model_name, content_type, tile_count, path_input):
index = output_html.find("</head>")
if index != -1:
output_html = output_html[index + len("</head>"):]
if local_path_in_html:
output_html = convert_local_images(output_html)
if not output_html:
modelID = get_models(model_file, True)

View File

@ -1176,7 +1176,7 @@ def on_ui_settings():
"Use local images in the HTML",
section=browser,
**({'category_id': cat_id} if ver_bool else {})
).info("Does not work in combination with the \"Use local HTML file for model info\" option!")
)
)
shared.opts.add_option(
@ -1260,4 +1260,4 @@ def on_ui_settings():
shared.opts.add_option(f"{setting_name}_subfolder", shared.OptionInfo("None", folder_name, gr.Dropdown, make_lambda(folder, desc), section=download, **({'category_id': cat_id} if ver_bool else {})))
script_callbacks.on_ui_tabs(on_ui_tabs)
script_callbacks.on_ui_settings(on_ui_settings)
script_callbacks.on_ui_settings(on_ui_settings)