2025-09-27 00:12:38 +03:00
from typing_extensions import override
2024-01-03 03:30:39 -05:00
import torch
import comfy . utils
2025-09-27 00:12:38 +03:00
from comfy_api . latest import ComfyExtension , io
2024-01-03 03:30:39 -05:00
2025-09-27 00:12:38 +03:00
class SD_4XUpscale_Conditioning ( io . ComfyNode ) :
2024-01-03 03:30:39 -05:00
@classmethod
2025-09-27 00:12:38 +03:00
def define_schema ( cls ) :
return io . Schema (
node_id = " SD_4XUpscale_Conditioning " ,
category = " conditioning/upscale_diffusion " ,
2026-02-16 14:02:17 -08:00
description = " Prepares conditioning for the Stable Diffusion 4x upscale model by resizing the input image and attaching it with noise augmentation to positive and negative conditioning. " ,
short_description = " Prepare conditioning for SD 4x upscale model. " ,
2025-09-27 00:12:38 +03:00
inputs = [
io . Image . Input ( " images " ) ,
io . Conditioning . Input ( " positive " ) ,
io . Conditioning . Input ( " negative " ) ,
io . Float . Input ( " scale_ratio " , default = 4.0 , min = 0.0 , max = 10.0 , step = 0.01 ) ,
io . Float . Input ( " noise_augmentation " , default = 0.0 , min = 0.0 , max = 1.0 , step = 0.001 ) ,
] ,
outputs = [
io . Conditioning . Output ( display_name = " positive " ) ,
io . Conditioning . Output ( display_name = " negative " ) ,
io . Latent . Output ( display_name = " latent " ) ,
] ,
)
2024-01-03 03:30:39 -05:00
2025-09-27 00:12:38 +03:00
@classmethod
def execute ( cls , images , positive , negative , scale_ratio , noise_augmentation ) :
2024-01-03 03:30:39 -05:00
width = max ( 1 , round ( images . shape [ - 2 ] * scale_ratio ) )
height = max ( 1 , round ( images . shape [ - 3 ] * scale_ratio ) )
pixels = comfy . utils . common_upscale ( ( images . movedim ( - 1 , 1 ) * 2.0 ) - 1.0 , width / / 4 , height / / 4 , " bilinear " , " center " )
out_cp = [ ]
out_cn = [ ]
for t in positive :
n = [ t [ 0 ] , t [ 1 ] . copy ( ) ]
n [ 1 ] [ ' concat_image ' ] = pixels
2024-01-03 14:27:11 -05:00
n [ 1 ] [ ' noise_augmentation ' ] = noise_augmentation
2024-01-03 03:30:39 -05:00
out_cp . append ( n )
for t in negative :
n = [ t [ 0 ] , t [ 1 ] . copy ( ) ]
n [ 1 ] [ ' concat_image ' ] = pixels
2024-01-03 14:27:11 -05:00
n [ 1 ] [ ' noise_augmentation ' ] = noise_augmentation
2024-01-03 03:30:39 -05:00
out_cn . append ( n )
latent = torch . zeros ( [ images . shape [ 0 ] , 4 , height / / 4 , width / / 4 ] )
2025-09-27 00:12:38 +03:00
return io . NodeOutput ( out_cp , out_cn , { " samples " : latent } )
class SdUpscaleExtension ( ComfyExtension ) :
@override
async def get_node_list ( self ) - > list [ type [ io . ComfyNode ] ] :
return [
SD_4XUpscale_Conditioning ,
]
2024-01-03 03:30:39 -05:00
2025-09-27 00:12:38 +03:00
async def comfy_entrypoint ( ) - > SdUpscaleExtension :
return SdUpscaleExtension ( )