Files
Claude 1567ea6de9 Fix 10 bugs across all modules found during code review
Critical fixes:
- AnyAspectRatio: remove dead duplicate calculation that was overwriting
  itself with a wrong formula (correct formula kept on lines 55-56)
- LoadImageResizer: fix trailing comma that made resized_mask a tuple
  instead of a value; properly convert alpha channel to float32 tensor
- openAI_PoP: replace deprecated openai v0 API (openai.Image.create,
  openai.error.*) with modern openai>=1.0 client; fix hardcoded Windows
  backslash path with os.path.dirname(__file__); fix log/image dirs to
  be relative to module file instead of CWD
- LoraStackLoaders: add missing `import comfy.sd` (was NameError at
  runtime); fix filter from l[0] (switch, never 'None') to l[1]
  (lora_name); fix `lora_name is None` to `== 'None'` for string
  comparison; fix display name mapping key LoraStackLoader10 ->
  LoraStackLoader10_PoP

High severity fixes:
- Conditioning: guard std() divisions with `if std > 0` to prevent
  NaN/Inf crash when tensor has zero variance
- EfficientAttention: move dim_head calculation after dimension
  truncation so reshape is always valid; add divisibility check;
  fix output reshape to use min_dim not dim_q
- VAEEncodeDecodeLoader: remove 5 debug print statements from decode()
- CNutil: remove 3 debug print statements from resize_to_resolution()

Minor fixes:
- AdaptiveCannyDetector: fix `Category` -> `CATEGORY` (case-sensitive,
  ComfyUI was ignoring the node category)
- LoadImageResizer: remove duplicate CATEGORY = "image" definition
- requirements.txt: remove unused matplotlib/seaborn; add missing Pillow

https://claude.ai/code/session_01QPLKoy7P41H3QPB6tMrpPh
2026-02-25 10:13:43 +00:00

37 lines
1.2 KiB
Python

import numpy as np
import cv2
from PIL import Image
def convert_to_3_channels(x):
if x.dtype == np.float32:
assert x.min() >= 0.0 and x.max() <= 1.0
x = (255.0 * x).astype(np.uint8)
elif x.dtype != np.uint8:
raise ValueError("Unsupported dtype")
x = np.squeeze(x, axis=0)
if x.ndim == 2:
x = x[:, :, None]
assert x.ndim == 3
H, W, C = x.shape
assert C in (1, 3, 4)
if C == 3:
return x
if C == 1:
return np.repeat(x, 3, axis=2)
if C == 4:
color = x[:, :, :3].astype(np.float32)
alpha = x[:, :, 3:4].astype(np.float32) / 255.0
y = color * alpha + 255.0 * (1.0 - alpha)
return y.clip(0, 255).astype(np.uint8)
# Resize an image to a resolution, preserving aspect ratio
def resize_to_resolution(input_image, resolution):
H, W, C = input_image.shape
k = resolution / min(H, W)
H_new = int(np.round(H * k / 64.0)) * 64
W_new = int(np.round(W * k / 64.0)) * 64
interpolation = cv2.INTER_AREA if k < 1.0 else cv2.INTER_LINEAR
resized_image = cv2.resize(input_image, (W_new, H_new), interpolation=interpolation)
return resized_image