refactor: replace custom AspectCrop with built-in ImageScale (crop=center)
Switch to comfy-core ImageScale node which combines center-cropping and resizing in a single built-in step (bilinear, 854x480, center). AspectCrop class removed from the package since it is no longer needed in the reference workflow. Trade-off: changing aspect_ratio on the PrimitiveNode no longer auto-adjusts the crop dims. The two ImageScale width/height widgets must be updated manually (e.g. 1080x1920 for 9:16, 480x480 for 1:1 at 480p). Made-with: Cursor
This commit is contained in:
@@ -3,18 +3,16 @@ credit_tracker — ComfyUI custom node package
|
||||
Install: copy this folder to ComfyUI/custom_nodes/ and restart ComfyUI.
|
||||
"""
|
||||
|
||||
from .nodes import CreditDisplay, CreditDisplayFromStrings, AspectCrop
|
||||
from .nodes import CreditDisplay, CreditDisplayFromStrings
|
||||
|
||||
NODE_CLASS_MAPPINGS = {
|
||||
"CreditDisplay": CreditDisplay,
|
||||
"CreditDisplayFromStrings": CreditDisplayFromStrings,
|
||||
"AspectCrop": AspectCrop,
|
||||
}
|
||||
|
||||
NODE_DISPLAY_NAME_MAPPINGS = {
|
||||
"CreditDisplay": "Credit Display",
|
||||
"CreditDisplayFromStrings": "Credit Display (from strings)",
|
||||
"AspectCrop": "Aspect Crop",
|
||||
}
|
||||
|
||||
WEB_DIRECTORY = "./js"
|
||||
|
||||
@@ -623,64 +623,3 @@ class CreditDisplayFromStrings:
|
||||
return {"ui": {"text": [text]}, "result": (text, credits, usd)}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# ComfyUI node: AspectCrop
|
||||
# Center-crops an image to a target aspect ratio and (optionally) resizes
|
||||
# it to a target resolution. Useful to force LoadImage outputs to match
|
||||
# the aspect ratio expected by a downstream video node (ByteDance, Kling…).
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class AspectCrop:
|
||||
"""
|
||||
Center-crop an IMAGE to the selected aspect ratio.
|
||||
Optionally resizes the cropped image to a target resolution (keeping the
|
||||
chosen aspect). Pure PyTorch — no PIL/NumPy dependency beyond what
|
||||
ComfyUI already ships with.
|
||||
"""
|
||||
|
||||
CATEGORY = "utils/image"
|
||||
FUNCTION = "crop"
|
||||
RETURN_TYPES = ("IMAGE", "INT", "INT")
|
||||
RETURN_NAMES = ("image", "width", "height")
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
"required": {
|
||||
"image": ("IMAGE",),
|
||||
"aspect_ratio": (_ALL_ASPECT_RATIOS, {"default": "16:9"}),
|
||||
"resolution": (["source"] + _ALL_RESOLUTIONS, {"default": "source"}),
|
||||
},
|
||||
}
|
||||
|
||||
def crop(self, image, aspect_ratio: str, resolution: str):
|
||||
import torch # ComfyUI always ships torch
|
||||
import torch.nn.functional as F
|
||||
|
||||
# ComfyUI IMAGE = (B, H, W, C) float tensor in [0,1]
|
||||
b, h, w, c = image.shape
|
||||
|
||||
ar_w, ar_h = [float(x) for x in aspect_ratio.split(":")]
|
||||
target_ratio = ar_w / ar_h
|
||||
src_ratio = w / h
|
||||
|
||||
# Center crop to the target aspect ratio
|
||||
if src_ratio > target_ratio:
|
||||
new_w = int(round(h * target_ratio))
|
||||
x0 = (w - new_w) // 2
|
||||
cropped = image[:, :, x0:x0 + new_w, :]
|
||||
else:
|
||||
new_h = int(round(w / target_ratio))
|
||||
y0 = (h - new_h) // 2
|
||||
cropped = image[:, y0:y0 + new_h, :, :]
|
||||
|
||||
# Optional resize to target resolution
|
||||
if resolution != "source":
|
||||
dims = RESOLUTION_DIMS.get(aspect_ratio, RESOLUTION_DIMS["16:9"])
|
||||
tgt_w, tgt_h = dims.get(resolution, dims["480p"])
|
||||
chw = cropped.permute(0, 3, 1, 2)
|
||||
resized = F.interpolate(chw, size=(tgt_h, tgt_w), mode="bilinear", align_corners=False)
|
||||
cropped = resized.permute(0, 2, 3, 1).contiguous()
|
||||
|
||||
out_h, out_w = cropped.shape[1], cropped.shape[2]
|
||||
return (cropped, out_w, out_h)
|
||||
|
||||
Reference in New Issue
Block a user