diff --git a/README.md b/README.md index b3490f1..3a955b7 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # Introduction Some nodes for stable diffusion comfyui.Sometimes it helps conveniently to use less nodes for doing the same things. +If you use workflow in my "blogs" repo, you need to dowmload these nodes. + # How to install The same with others custom nodes. Just cd custom_nodes and then git clone. diff --git a/tdxh_lib.py b/tdxh_lib.py new file mode 100644 index 0000000..f2c67e6 --- /dev/null +++ b/tdxh_lib.py @@ -0,0 +1,47 @@ + +# Define a function that takes a tuple representing the image width and height as a parameter +def get_SDXL_best_size(image_size): + # Assign the image width and height to w and h respectively + w, h = image_size + # Calculate the image aspect ratio + ratio = w / h + # Define a list to store the target sizes + target_sizes = [ + (1024, 1024), # 1 # 1/1 + (1152, 896), # 1.2857... # 4/3 + (896, 1152), # 0.7777... # 3/4 + (1216, 832), # 1.4615... # 3/2 7/5 + (832, 1216), # 0.6842... # 2/3 5/7 + (1344, 768), # 1.75 # 16/9 + (768, 1344), # 0.5714... # 9/16 + (1536, 640), # 2.4 # 12/5 + (640, 1536) # 0.4166 # 5/12 + ] + # Define a variable to store the minimum difference + min_diff = float('inf') + # Define a variable to store the closest target size + best_size = None + # Loop through the target size list + for target_size in target_sizes: + # Calculate the target size aspect ratio + target_ratio = target_size[0] / target_size[1] + # Calculate the absolute value of the difference between the image aspect ratio and the target size aspect ratio + diff = abs(ratio - target_ratio) + # If the difference is smaller than the minimum difference + if diff < min_diff: + # Update the minimum difference and the closest target size + min_diff = diff + best_size = target_size + # Return the closest target size + return best_size + +# Test the function +def test1(): + print(get_SDXL_best_size((800, 800))) # Output (1024, 1024) + print(get_SDXL_best_size((1200, 900))) # Output (1152, 896) + print(get_SDXL_best_size((600, 800))) # Output (896, 1152) + print(get_SDXL_best_size((700, 500))) # Output (1216, 832) + print(get_SDXL_best_size((1080, 1920))) # Output (768, 1344) + print(get_SDXL_best_size((1200, 500))) # Output (1536, 640) + +# test1() diff --git a/tdxh_node_comfyui.py b/tdxh_node_comfyui.py index f774826..bbbf4a1 100644 --- a/tdxh_node_comfyui.py +++ b/tdxh_node_comfyui.py @@ -10,6 +10,8 @@ import comfy.sd import folder_paths +from .tdxh_lib import get_SDXL_best_size + # Tensor to PIL def tensor2pil(image): return Image.fromarray(np.clip(255. * image.cpu().numpy().squeeze(), 0, 255).astype(np.uint8)) @@ -70,7 +72,7 @@ class TdxhImageToSizeAdvanced: }), "what_to_follow": ([ "only_width", "only_height", "both_width_and_height", "only_ratio", - "only_image" + "only_image","get_SDXL_best_size" ],), } } @@ -85,9 +87,10 @@ class TdxhImageToSizeAdvanced: image_size = self.tdxh_image_to_size(image) # width = self.tdxh_nearest_divisible_by_8(width) # height = self.tdxh_nearest_divisible_by_8(height) - # w, h = image_size[0], image_size[1] if what_to_follow == "only_image": return image_size + elif what_to_follow == "get_SDXL_best_size": + w, h = get_SDXL_best_size((image_size[0],image_size[1])) elif what_to_follow == "only_ratio": w, h = ratio * image_size[0], ratio * image_size[1] w, h = self.tdxh_nearest_divisible_by_8(w), self.tdxh_nearest_divisible_by_8(h) @@ -99,6 +102,7 @@ class TdxhImageToSizeAdvanced: elif what_to_follow == "only_height": new_width = self.tdxh_nearest_divisible_by_8(image_size[0] * height / image_size[1]) w, h = new_width, height + return self.tdxh_size_out(w,h) def tdxh_image_to_size(self, image):