Node-Based Image Workflows: Automatically Reusing the Original Filename as a Prefix

    This article explains why an image's filename is lost once it enters a node-based image workflow, and how to fix it by having the loader node emit the filename as a string output that feeds the save node's prefix input. It covers the input/output contract, prefix override priority, safe filename extraction, a thin-wrapper backend implementation, keeping parameter declarations consistent with validation and execution functions, extension installation, save-node verification, and the extra concerns of batch processing, name collisions, and caching. Readers will get a practical, host-agnostic implementation order plus a troubleshooting sequence for when naming does not behave as expected.

    1. The Core Problem: Filenames Are Not Carried Through the Workflow

    In node-based image processing systems, a loader node typically reads an image and outputs image data plus a mask. Downstream nodes can scale, repaint, and generate, but they usually have no idea what the image was originally called.

    The reason is that pixels and filenames are two different kinds of data. Pixels describe visual content; a filename is source metadata. Passing along image data does not mean the file path, filename, or upload information follows automatically.

    For example, if the input file is:

    1. portrait_023.png

    and the processed images are saved with a generic prefix, you may end up with:

    1. output_00001.png
    2. output_00002.png

    These files work fine, but the mapping between input and output is not obvious. A more useful result is:

    1. portrait_023_00001.png
    2. portrait_023_00002.png

    Two concepts need to be separated here:

    • Filename prefix: carries information about the source, task, or category.
    • Final filename: usually composed of the prefix, a sequence number, the extension, and so on.

    So the goal is not to force outputs to share the input’s exact name, but to let the save step automatically pick up the original name and use it as part of the prefix.

    The key to this class of problem is not guessing the source at save time, but preserving it at load time.

    A clear node structure looks like this:

    1. input image
    2. ↓
    3. load image and extract name
    4. ├── image data → downstream image processing → save node
    5. ├── mask data → mask processing branch
    6. └── filename string ────────→ prefix input of the save node

    A custom node can wrap the existing loading logic with one extra layer: image reading stays with the original implementation, and the new logic only handles the filename.

    This design has three advantages.

    First, there is no need to reverse-engineer the original filename from pixel content; the source of the information is explicit. Second, it avoids reimplementing complex logic such as image decoding, alpha channel handling, and multi-frame processing. Third, the filename is placed explicitly in the workflow, so the connection is visible and problems are easier to trace.

    One point needs emphasis: this connection only works directly if the save node accepts a string prefix. If the save node offers only a fixed text input, you need the host’s input conversion mechanism, or you need to add a corresponding string input to the save node. Simply adding one more output to the loader node will not make every save node recognize it.

    3. Define Inputs, Outputs, and Naming Rules First

    Before writing code, make the node’s data contract explicit, so you do not discover an interface mismatch after the implementation is done.

    3.1 Input Parameters

    A minimal implementation should include:

    Parameter Type Purpose
    Input image File reference Specifies the image to load
    Manual filename prefix String Overrides the extracted result when non-empty
    Remove extension Boolean Decides whether the trailing extension is stripped

    “Automatically open the mask editor” can be an optional feature, but it is not required for automatic naming. That feature involves frontend interaction and should be implemented separately from the backend naming logic, so a UI behavior cannot destabilize image loading.

    3.2 Output Data

    Three outputs are recommended:

    1. image data
    2. mask data
    3. filename prefix string

    If the original loader node has other outputs, the wrapper should preserve them according to actual needs rather than arbitrarily changing the interface order or data structure.

    3.3 Override Priority

    An easy-to-follow rule is:

    1. non-empty manual prefix
    2. takes priority over
    3. base filename of the input image
    4. takes priority over
    5. default prefix

    Manual input should be trimmed first. Otherwise, an input that only looks empty may be treated as a valid prefix.

    For “remove extension,” it should also be clear what the option applies to. The recommended default is that it applies only to the automatically extracted original filename, while manual input is handled as the user-specified prefix. This prevents a version number in manual input from being mistaken for an extension.

    For example:

    1. original name: sample.v2.png
    2. automatic prefix: sample.v2
    3. manual prefix: review.v2
    4. final prefix: review.v2

    4. Filename Extraction Cannot Be Simple String Truncation

    The original file reference may be a full path, a relative path, or a host-internal marked file reference. The correct order is:

    1. Use the host’s file resolution mechanism to get the actual file location or normalized name.
    2. Get the base filename, dropping the directory portion.
    3. Remove the last extension according to configuration.
    4. Apply safe sanitization.
    5. Check whether the sanitized result is empty.

    Do not split the filename at the first period. For example:

    1. scene.daylight.v3.png

    Truncating at the first period yields:

    1. scene

    which loses useful version information. Normally you should remove only the last extension and keep:

    1. scene.daylight.v3

    For files that start with a period and have no ordinary extension, use mature path-handling rules rather than assembling an incomplete string algorithm yourself.

    Also distinguish the “original filename” from the “filename at load time”: if the upload system already renamed a file because of a name collision, what the loader node can obtain directly is usually the name in the current input directory. If the business requires the pre-upload name, that metadata must be recorded at upload time; the loader node cannot recover it out of nothing.

    5. Backend Implementation: A Thin Wrapper Is Safer Than a Full Rewrite

    The pseudocode below is interface-agnostic and illustrative. It expresses the implementation order; it is not an installable plugin, and the host calls must be replaced with the interfaces your system actually provides.

    1. function load_and_emit_filename(
    2. input_file_reference,
    3. manual_prefix = "",
    4. remove_extension = true
    5. ):
    6. file_info = host_resolve_input_file(input_file_reference)
    7. if file_info is missing or access is not allowed:
    8. return a clear input validation error
    9. load_result = call_existing_image_loading_logic(input_file_reference)
    10. manual_value = trim(manual_prefix)
    11. if manual_value is not empty:
    12. candidate_prefix = manual_value
    13. else:
    14. candidate_prefix = get_base_filename(file_info)
    15. if remove_extension:
    16. candidate_prefix = remove_last_extension(candidate_prefix)
    17. safe_prefix = sanitize_unsafe_filename_chars(candidate_prefix)
    18. if safe_prefix is empty:
    19. safe_prefix = "image"
    20. return:
    21. image from load_result,
    22. mask from load_result,
    23. safe_prefix

    The most important part here is calling the existing loading logic instead of reimplementing all image processing details.

    Image loading may involve orientation correction, alpha channel conversion, mask generation, multi-frame traversal, color mode conversion, and dimension consistency checks. If the wrapper skips one of these steps, you can end up with rotated images, lost transparent backgrounds, or broken multi-frame output.

    If you truly need to rewrite the loading flow, build tests for these behaviors rather than validating with a single ordinary image.

    6. Parameter Declarations, Validation, and Execution Must Agree

    A common custom-node failure is that the UI has a new parameter but the backend never receives it.

    For instance, the node adds a “remove extension” toggle while the execution function still accepts only the input image, so the host may error when passing parameters. If the input validation function has the same mismatch, it may block task submission before the job even runs.

    When implementing, check item by item:

    • Does every parameter in the input declaration have an execution entry point?
    • Are default values consistent across all entry points?
    • Are booleans handled as booleans rather than strings?
    • Do the number and types of outputs match the output declarations?
    • Do old workflows still run with default values when new parameters are missing?

    If you add an “auto open mask” parameter, its backend behavior must also be clear: it may simply provide configuration to the frontend, and should not attempt to manipulate a local UI on the server side.

    For optional features, a reasonable compatibility strategy is to default them off and ensure that basic loading and naming still work with no frontend extension present.

    7. Handle the Install Directory and Frontend Scripts Through the Host Mechanism

    Extension directories differ across installation methods, so do not treat one desktop installation path as universal.

    A safer order of operations:

    1. Read the current host’s custom node loading rules.
    2. Confirm the extension directory actually in use, not a historical installation directory.
    3. Create a separate extension directory and place the backend entry file there.
    4. Register the node type and display name as the host requires.
    5. Restart or reload the extension.
    6. Check the startup log to confirm the node was recognized.
    7. Verify the node output in an empty workflow.

    The directory layout can be organized conceptually as:

    1. custom extension directory/
    2. └── image-name-output-extension/
    3. ├── backend entry file
    4. └── optional frontend asset directory/
    5. └── interaction script

    Merely creating a frontend asset directory does not mean the script is loaded automatically. Whether extra registration is needed, and how, depends on the host’s extension protocol.

    If the requirement is only to output the filename, complete a pure backend version first. Auto-opening the editor, UI hints, and widget linkage can be added once the core flow is stable.

    8. Save Node Connection and Result Verification

    After installation, build a minimal verification workflow first:

    1. load_and_emit_filename
    2. ├── image → save
    3. └── filename prefix → save prefix

    Leaving out complex generation branches for now makes it faster to tell whether the problem is in the naming chain or the image processing chain.

    At minimum, verify these cases:

    Test input Expected behavior
    Ordinary image name Outputs the same base prefix
    Name with multiple periods Only the last extension is removed
    Chinese, space, or underscore names Preserved within what the target environment allows
    Manual prefix input Overrides the extracted result
    Manual input containing only spaces Falls back to automatic naming
    Input file does not exist Returns a clear error
    Unsafe manual name Sanitized or rejected, never escaping the output directory

    Then connect the full image generation flow and confirm that the filename string and its corresponding image always belong to the same processing run.

    Do not judge whether the association is correct by the sequence number in saved files. Sequence numbers usually reflect save order and are not necessarily the input asset number.

    9. Extra Attention for Batch Processing, Collisions, and Caching

    9.1 Name Counts Must Match in Batch Processing

    With a single image input, one string is usually enough. But if the node outputs multiple images at once, be explicit about whether they all come from the same source file or from different files.

    Multiple results from the same source can share a prefix and be distinguished by sequence number. Results from different sources need names carried per item, or the workflow executed once per file.

    Pairing a set of images from different sources with a single name string destroys traceability.

    9.2 Identical Prefixes Do Not Imply Overwrite Is Acceptable

    The same image name can exist in different directories. Using only the base filename gives the same prefix in all of them.

    You can add a sanitized task identifier, batch identifier, or relative directory identifier, but you should not use the full input path as the output prefix. A full path may expose local directory structure and may also be interpreted by the save node as an output subdirectory.

    The save side should also provide an independent collision handling mechanism. In concurrent tasks, checking whether a file exists before writing does not fully avoid race conditions; you need safe unique naming or atomic creation.

    9.3 Caching Must Account for Content Changes

    If the input filename stays the same but the content is replaced, deciding cache validity from the file reference alone may keep returning the old image.

    The wrapper node should preserve the original loader’s cache invalidation behavior as much as possible. If the host supports file state or content digests in cache decisions, reuse that mechanism.

    The manual prefix and extension toggle should also participate in output change detection; otherwise, after parameters change, the filename output may still be the stale cached result.

    10. Troubleshooting Order and Implementation Advice

    When the feature does not behave as expected, troubleshoot in data flow order:

    1. Node not visible: check the extension directory, entry registration, dependencies, and startup log.
    2. Task validation fails: check that parameter declarations match the validation function.
    3. Image loads but no name is output: check the number, types, and order of return values.
    4. Name output is correct but saving still uses a fixed prefix: check whether the save node actually accepts the string connection.
    5. Result does not change after renaming: check the cache and old workflow parameters.
    6. Generated results overwrite or get mixed up: check the collision strategy, batch mapping, and concurrent save mechanism.

    The safest implementation path is to first complete the minimal loop of “load image, extract name, connect to save,” then gradually add manual override, safe sanitization, batch processing, and UI interaction.

    Ultimately, automatically reusing the original image name is not merely a save setting; it is a matter of propagating source metadata. Preserve the name at the input stage, maintain the association during processing, and apply safe naming at the output stage, and the image workflow gains more reliable retrieval, archiving, and traceability.