Nodes/opencv-comfyui/OpenCV warpAffine_0
ComfyUI Node

OpenCV warpAffine_0

The node that rotates, shifts, and skews your images with a 2×3 matrix

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV warpAffine_0
  • src
  • M
  • dst
  • nparray
dsize
flags
borderMode
borderValue

Every rotation, translation, scale, and shear you've ever done in an image editor is one warpAffine call under the hood. This node wraps that OpenCV workhorse, and it's one of the genuinely useful entries in this pack - the deterministic, millisecond, "just move the pixels" primitive that the KB would tell you to reach for before you burn a diffusion pass on it. Straighten a tilted generation, re-center a subject, cheaply augment a dataset, correct slight jitter between video frames - all of it is warpAffine territory.

How it works

An affine transform is the whole family of "parallel lines stay parallel" geometric operations, encoded as a single 2×3 matrix M. The first two columns mix rotation and scale/shear; the third column is the translation. Feed warpAffine an image plus that matrix and it remaps every pixel to its new location, filling the gaps however you tell it to. Because it's a matrix, you never hand-roll it - you build M with the pack's getRotationMatrix2D_0 node (takes center as a literal, angle, and scale), or with getAffineTransform_0 from point pairs. The hard part of affine warping isn't running it; it's producing the right M, and this pack covers both sides.

Inputs and outputs

The ones you actually set:

  • src - the NPARRAY image (run it through Image2Nparray first if you're starting from a Comfy IMAGE).
  • M - the 2×3 transform matrix as an NPARRAY, usually straight out of getRotationMatrix2D_0.
  • dsize - output size as a literal string, e.g. (512, 512) or (1024, 768). The pack parses these with ast.literal_eval, so the parentheses and comma matter.
  • flags - interpolation. 1 is linear (the default good-enough), 0 is nearest-neighbor (your pixel-art friend), 2 is cubic, 4 Lanczos if you want the fancy.
  • borderMode / borderValue - what fills the edges you just opened up. 0 is constant fill with borderValue (a literal like (0, 0, 0)); 1 replicates the edge pixels. If you rotate and see black corners you don't want, switch to 1.

There's an optional dst - the README's advice is to skip the out-params, so leave it unplugged. Output is a single nparray, which you push through Nparrays2Image to get back to a normal image.

Installing

Standard for the pack. ComfyUI Manager, search "opencv-comfyui", install, restart. Or:

cd ComfyUI/custom_nodes
git clone https://github.com/geroldmeisinger/opencv-comfyui

requirements.txt handles opencv-contrib-python, numpy, torch.

Common issues

  • invalid syntax (<unknown>, line 0) - your dsize or borderValue literal isn't valid Python. It must be (512, 512), not 512, 512 or [512, 512].
  • Black triangles at the edges after rotation - that's borderMode defaulting to constant. Set borderMode to 1 to replicate edges, or set borderValue to match your background.
  • Only images with batch_size==1 are supported! - the pack only handles single images; pull one frame out of a batch with ImageFromBatch first.

One more honest note: this pack has no CUDA path (no Image2UMat node), so warpAffine_0 runs on CPU. For a single warp that's instant; for per-frame warping of a long video, factor in the cost.

Categoryimage/OpenCV

Inputs (7)

NameTypeDefaultDescription
srcNPARRAY
MNPARRAY
dsizeSTRING
flagsINT
borderModeINT
borderValueSTRING
dstoptNPARRAY

Outputs (1)

NameTypeDescription
nparrayNPARRAY