Skip to content

Conversation

bigcat88
Copy link
Contributor

This pull request refactors the image and animation saving logic for V3 nodes by introducing a centralized ImageSaveHelper class. This consolidation addresses code duplication in the SaveImage, SaveAnimatedPNG, SaveAnimatedWEBP, and PreviewImage nodes, leading to cleaner code, consistent behavior, and easier maintenance.

Key Changes:

  1. New ImageSaveHelper Class in comfy_api/v3/ui.py:

    • A new helper class has been created to encapsulate all image-saving functionalities.
    • It centralizes logic for:
      • Converting image tensors to PIL images.
      • Creating and embedding metadata for PNG, APNG (animated PNG), and WebP formats.
      • Handling file path generation and writing files to disk.
    • The execute methods in SaveImage, SaveAnimatedPNG, SaveAnimatedWEBP, and the __init__ method of PreviewImage have been refactored to use this new helper class.
  2. Behavioral Consistency and Simplification:

    • SaveAnimatedWEBP: The num_frames parameter and its associated logic, which allowed splitting frames into multiple WebP files, has been removed(it was not used in V1). The node now consistently outputs a single animated WebP file containing all input frames. This removes the previous inconsistency with SaveAnimatedPNG and simplifies the node's function.
    • SaveAnimatedPNG: The animated flag in the UI output is now dynamic. It will be False if only one image is provided, ensuring its behavior is identical to SaveAnimatedWEBP. Previously, it was always hardcoded to True.
    • Both animated save nodes now consistently return a list containing a single SavedResult object, reflecting the creation of a single animated file.

Visual test:

Screenshot from 2025-07-20 11-06-55

note: SaveAnimatedWebp results are ok, they just displaying different frames.


Preview nodes, the results is ok, file size between V1 and V3 nodes did not change:

ls -la ../temp/

1289996 Jul 20 10:55 ComfyUI_temp_oybok_00001_.png
1293843 Jul 20 10:55 ComfyUI_temp_oybok_00002_.png
1289996 Jul 20 10:55 ComfyUI_temp_yzvkk_00001_.png
1293843 Jul 20 10:55 ComfyUI_temp_yzvkk_00002_.png

Image Save(+APNG, +WEBP) nodes, the results is ok, file size between V1 and V3 nodes did not change:

~/Comfy/ComfyUI/output$ ls -la 
2404355 Jul 20 10:55 test_animated_mult_V1_00001_.png
1596004 Jul 20 10:55 test_animated_mult_V1_00002_.webp
1596004 Jul 20 10:55 test_animated_mult_V3_00001_.webp
2404355 Jul 20 10:55 test_animated_mult_V3_00002_.png
1304190 Jul 20 10:55 test_animated_solo_V1_00001_.png
814040  Jul 20 10:55 test_animated_solo_V1_00002_.webp
1304190 Jul 20 10:55 test_animated_solo_V3_00001_.png
814040  Jul 20 10:55 test_animated_solo_V3_00002_.webp
1380986 Jul 20 10:55 test_mult_V1_00001_.png
1145540 Jul 20 10:55 test_mult_V1_00002_.png
1380986 Jul 20 10:55 test_mult_V3_00001_.png
1145540 Jul 20 10:55 test_mult_V3_00002_.png
1380986 Jul 20 10:55 test_solo_V1_00001_.png
1380986 Jul 20 10:55 test_solo_V3_00001_.png

@Kosinkadink
Copy link
Collaborator

For the helpers, it would be good if there was a version of them that simply returns some _UIOutput subclass, so that it can be given as the ui= arg directly in NodeOutput and there would be no need to create the dictionary at in the node body.

Other than that, looks good! I'll merge since the above change should not impact anything else.

@Kosinkadink Kosinkadink added the v3 label Jul 22, 2025
@Kosinkadink Kosinkadink merged commit 5afcca1 into comfyanonymous:v3-definition Jul 22, 2025
4 checks passed
@bigcat88 bigcat88 deleted the v3/nodes/refactor-image-save branch July 23, 2025 03:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants