import re import base64 import html from io import BytesIO def image_to_base64(image): # Convert the image to bytes buffered = BytesIO() image.save(buffered, format="PNG") # You can change format to PNG if needed # Encode the bytes to base64 img_str = base64.b64encode(buffered.getvalue()) return img_str.decode('utf-8') # Convert bytes to string def remove_id_and_ext(text): text = re.sub(r'\[.*\]$', '', text) extension = text[-12:].strip() if extension == "safetensors": text = text[:-13] elif extension == "ckpt": text = text[:-4] return text def extract_data(text): results = {} patterns = { 'prompt': r'(.*)', 'negative_prompt': r'Negative prompt: (.*)', 'steps': r'Steps: (\d+),', 'seed': r'Seed: (\d+),', 'sampler': r'Sampler:\s*([^\s,]+(?:\s+[^\s,]+)*)', 'model': r'Model:\s*([^\s,]+)', 'cfg_scale': r'CFG scale:\s*([\d\.]+)', 'size': r'Size:\s*([0-9]+x[0-9]+)' } for key in ['prompt', 'negative_prompt', 'steps', 'seed', 'sampler', 'model', 'cfg_scale', 'size']: match = re.search(patterns[key], text) if match: results[key] = match.group(1) else: results[key] = None if results['size'] is not None: w, h = results['size'].split("x") results['w'] = w results['h'] = h else: results['w'] = None results['h'] = None return results def place_lora(current_prompt, lora_name): pattern = r"" if re.search(pattern, current_prompt): return re.sub(pattern, "", current_prompt) else: return current_prompt + " " def plaintext_to_html(text, classname=None): content = "
\n".join(html.escape(x) for x in text.split('\n')) return f"

{content}

" if classname else f"

{content}

" def get_exif_data(image): items = image.info info = '' for key, text in items.items(): info += f"""

{plaintext_to_html(str(key))}

{plaintext_to_html(str(text))}

""".strip() + "\n" if len(info) == 0: message = "Nothing found in the image." info = f"

{message}

" return info