Files
DumasNodes/dumas_reference_image_preprocessor.py
T

115 lines
4.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Reference-image prompt helper for reusable image references."""
from __future__ import annotations
REFERENCE_TYPES = ("Character", "Object", "Scene", "Creature", "Vehicle")
REFERENCE_IDS = tuple(str(index) for index in range(1, 10))
def reference_token(reference_id: str | int) -> str:
"""Return the Continuum reference token for a user-facing ID."""
try:
index = int(reference_id)
except (TypeError, ValueError) as exc:
raise ValueError("reference_id must be an integer from 1 to 9") from exc
if index < 1 or index > 9:
raise ValueError("reference_id must be an integer from 1 to 9")
return f"@R{index}"
_TEMPLATES = {
"Character": (
"Describe the character in the image in exquisite detail. Include "
"clothing, accessories, body type, proportions, and size. The character "
"will be referred to as {token} and should be treated as a photographic "
"representation of a real person, so start the description with \"{token}\". "
"Do not refer to individual shots; treat this as a visual description of "
"the character."
),
"Object": (
"Describe the object in the image in exquisite detail. Include its shape, "
"size, materials, colours, construction, surface detail, condition, and "
"distinctive features. The object will be referred to as {token}. Do not "
"refer to individual shots; treat this as a visual description of the object."
),
"Scene": (
"Describe the scene in the image in exquisite detail. Include the "
"environment, architecture, spatial layout, lighting, atmosphere, colours, "
"perspective, and important background elements. The scene will be referred "
"to as {token}. Do not refer to individual shots; treat this as a visual "
"description of the scene."
),
"Creature": (
"Describe the creature in the image in exquisite detail. Include its anatomy, "
"body structure, size, proportions, skin, fur, scales, colouring, distinctive "
"features, expression, and physical condition. The creature will be referred "
"to as {token}. Do not refer to individual shots; treat this as a visual "
"description of the creature."
),
"Vehicle": (
"Describe the vehicle in the image in exquisite detail. Include its type, "
"dimensions, shape, materials, colours, design, mechanical features, "
"condition, markings, and distinctive details. The vehicle will be referred "
"to as {token}. Do not refer to individual shots; treat this as a visual "
"description of the vehicle."
),
}
def build_reference_prompt(reference_type: str, reference_id: str | int, description: str = "") -> str:
"""Build the editable prompt shown by the node."""
if reference_type not in _TEMPLATES:
raise ValueError(f"reference_type must be one of: {', '.join(REFERENCE_TYPES)}")
token = reference_token(reference_id)
prompt = _TEMPLATES[reference_type].format(token=token)
extra = str(description or "").strip()
if extra:
prompt += f'\n\nAdditional information about the {reference_type.lower()}: "{extra}"'
return prompt
class DumasReferenceImagePreprocessor:
"""Keep an image unchanged and generate an editable reference prompt."""
CATEGORY = "Dumas/Reference"
DESCRIPTION = (
"Pass through a reference image and create an editable @R1–@R9 prompt "
"for a Character, Object, Scene, Creature, or Vehicle."
)
RETURN_TYPES = ("IMAGE", "STRING")
RETURN_NAMES = ("image", "reference_prompt")
FUNCTION = "process"
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"image": ("IMAGE",),
"reference_type": (list(REFERENCE_TYPES), {"default": "Character"}),
"reference_id": (list(REFERENCE_IDS), {"default": "1"}),
"description": ("STRING", {"default": "", "multiline": True}),
"generated_prompt": (
"STRING",
{
"default": build_reference_prompt("Character", "1"),
"multiline": True,
},
),
}
}
def process(self, image, reference_type, reference_id, description, generated_prompt):
prompt = str(generated_prompt or "").strip()
if not prompt:
prompt = build_reference_prompt(reference_type, reference_id, description)
return (image, prompt)
NODE_CLASS_MAPPINGS = {"DumasReferenceImagePreprocessor": DumasReferenceImagePreprocessor}
NODE_DISPLAY_NAME_MAPPINGS = {
"DumasReferenceImagePreprocessor": "DUMAS Reference Image Preprocessor",
}