Nodes/Comfyui_PDuse/PD_CropBorder
ComfyUI Node

PD_CropBorder

Auto-crop the black border off a single image

By 7BEII·Created 2 years ago·Updated 15 days ago· 53
PD_CropBorder
  • image
  • crop image
border_colorblack
threshold10
padding0

PD_CropBorder (PD_CropBorder) detects a solid black or white border on the edges of a single image and crops it off, right there in the graph. It's the in-pipeline version of the pack's folder-batch PD_BatchCropBlackBorder: same detection logic, but instead of writing files it hands you a cropped IMAGE tensor to keep working with.

Why you'd reach for it

Screenshots, upscaled frames, and scraped images love to come wrapped in letterbox bars, and a border wrecks composition checks, aspect-ratio math, and anything downstream that assumes the content fills the frame. This node is the "clean it up before it poisons the rest of the workflow" step. Because it's a real graph node (not an output-only tool), you can chain it: crop the border, then resize, then feed into the sampler or save nodes.

How it works

Same algorithm as its batch sibling: convert to grayscale, scan inward from all four edges, and treat pixels as "border" when they match the target color within threshold. Black means brightness ≤ threshold; white means brightness ≥ 255 − threshold. The first non-border row/column from each edge defines the crop rectangle. threshold (0–255, default 10) sets strictness - lower is stricter, so a slightly-off-black border may need a nudge up. padding (0–100) adds margin back around the crop, which is the right instinct when content nearly touches the edge. border_color picks black or white - one color per pass, so a frame with both would need two nodes or the batch version.

Output is crop image, a single IMAGE at the cropped size. Note the tooltip on threshold: "值越小检测越严格" - smaller value, stricter detection. Worth reading twice.

Installing

Part of the 7BEII/Comfyui_PDuse pack. ComfyUI Manager → search "Comfyui_PDuse" → install, or:

cd ComfyUI/custom_nodes
git clone https://github.com/7BEII/Comfyui_PDuse
cd Comfyui_PDuse
pip install -r requirements.txt

Restart after. No models; needs opencv-python (part of the pack's requirements) plus the usual Pillow/numpy.

Gotchas

The border must be connected to the edge - a floating black box in the middle of the image is content, not a border, and won't be touched, which is exactly the behavior you want. The real trap is content that genuinely reaches the edge: a dark image edge can get mistaken for a black border and cropped. That's what padding and a stricter threshold are for. Unlike the batch node, this one can't overwrite anything - it only outputs pixels - so it's the safe way to test your threshold settings before running the batch version on a real folder.

CategoryPDuse/Image

Inputs (4)

NameTypeDefaultDescription
imageIMAGE
border_colorCOMBOblack选择要删除的边框颜色
thresholdINT100–255颜色检测阈值,0-255,值越小检测越严格
paddingINT00–100裁切后额外保留的边距像素

Outputs (1)

NameTypeDescription
crop imageIMAGE