Skip to content

Commit b7a47a7

Browse files
committed
added the UpscaleImageWithModelIfNeed node.
1 parent f329437 commit b7a47a7

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,6 @@ Added the node for convenience. The code is originally from ComfyUI-Custom-Scrip
5656

5757
## MatchImageRatioToPreset
5858
According to the input image ratio, decide which standard SDXL training size is the closest match. This is useful for subsequent image resizing and other processes.
59+
60+
## UpscaleImageWithModelIfNeed
61+
Enhanced the official UpscaleImageWithModel node by adding a judge. If the input image area exceeds a predefined threshold, upscaling is bypassed. The threshold is a percentage of the SDXL standard size (1024x1024) area.

nodes.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from PIL import Image, ImageEnhance
2020
from .color_correct import ColorCorrectOfUtils
2121
import cv2
22-
22+
from comfy_extras.nodes_upscale_model import ImageUpscaleWithModel
2323

2424
app_dir = os.path.dirname(os.path.dirname(
2525
os.path.dirname(os.path.realpath(__file__))))
@@ -880,6 +880,32 @@ def forward(self, image):
880880
return (target_w, target_h, min_v, max_v)
881881

882882

883+
class UpscaleImageWithModelIfNeed(ImageUpscaleWithModel):
884+
885+
def __init__(self) -> None:
886+
super().__init__()
887+
888+
@classmethod
889+
def INPUT_TYPES(s):
890+
return {"required": {"upscale_model": ("UPSCALE_MODEL",),
891+
"image": ("IMAGE",),
892+
"tile_size": ("INT", {"default": 512, "min": 128, "max": 10000}),
893+
"threshold_of_xl_area": ("FLOAT", {"default": 0.9, "min": 0.0, "max": 64.0, "step": 0.01}),
894+
}}
895+
RETURN_TYPES = ("IMAGE",)
896+
FUNCTION = "forward"
897+
898+
CATEGORY = "image/upscaling"
899+
900+
def forward(self, image, upscale_model, tile_size=512, threshold_of_xl_area=0.9):
901+
h, w = image.shape[1:-1]
902+
percent = h * w / (1024 * 1024)
903+
if percent > threshold_of_xl_area:
904+
return (image,)
905+
906+
return self.upscale(upscale_model, image, tile_size)
907+
908+
883909
NODE_CLASS_MAPPINGS = {
884910
"LoadImageWithSwitch": LoadImageWithSwitch,
885911
"LoadImageMaskWithSwitch": LoadImageMaskWithSwitch,
@@ -900,6 +926,7 @@ def forward(self, image):
900926
"ImageResizeTo8x": ImageResizeTo8x,
901927
"TextPreview": TextPreview,
902928
"MatchImageRatioToPreset": MatchImageRatioToPreset,
929+
"UpscaleImageWithModelIfNeed": UpscaleImageWithModelIfNeed,
903930
}
904931

905932
NODE_DISPLAY_NAME_MAPPINGS = {
@@ -922,5 +949,6 @@ def forward(self, image):
922949
"CheckpointLoaderSimpleWithSwitch": "Load checkpoint with switch",
923950
"ImageResizeTo8x": "Image resize to 8x",
924951
"TextPreview": "Preview Text",
925-
"MatchImageRatioToPreset": "Match image ratio to stardard size"
952+
"MatchImageRatioToPreset": "Match image ratio to stardard size",
953+
"UpscaleImageWithModelIfNeed": "Upscale image using model if need",
926954
}

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[project]
22
name = "comfyui-utils-nodes"
3-
description = "Nodes:LoadImageWithSwitch, ImageBatchOneOrMore, ModifyTextGender, ImageCompositeMaskedWithSwitch, ColorCorrectOfUtils, SplitMask, MaskFastGrow, CheckpointLoaderSimpleWithSwitch, ImageResizeTo8x, MatchImageRatioToPreset etc."
4-
version = "1.0.8"
3+
description = "Nodes:LoadImageWithSwitch, ImageBatchOneOrMore, ModifyTextGender, ImageCompositeMaskedWithSwitch, ColorCorrectOfUtils, SplitMask, MaskFastGrow, CheckpointLoaderSimpleWithSwitch, ImageResizeTo8x, MatchImageRatioToPreset, UpscaleImageWithModelIfNeed etc."
4+
version = "1.0.9"
55
license = "LICENSE"
66
dependencies = []
77

0 commit comments

Comments
 (0)