Nodes/opencv-comfyui/OpenCV getRotationMatrix2D_0
ComfyUI Node

OpenCV getRotationMatrix2D_0

Rotate and Zoom Anything, the Affine Way

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV getRotationMatrix2D_0
    • nparray
    center
    angle
    scale

    OpenCV getRotationMatrix2D_0 builds the 2×3 affine matrix that rotates an image around a point and scales it - the standard OpenCV recipe for "spin and zoom this crop." It wraps cv2.getRotationMatrix2D(center, angle, scale). On its own it does nothing visible: it returns a 2×3 affine matrix. You feed that matrix into the pack's warpAffine node along with your image, and that's when the pixels actually move. Think of this node as the geometry and warpAffine as the executor.

    Why you'd reach for it

    Rotating a crop around its own center instead of the image corner, zooming into a specific point, straightening a tilted region, or building data-augmentation pipelines that rotate and scale deterministically. OpenCV's warpAffine is the workhorse behind all of these, and this node is the thing that produces the matrix it needs. It's the classic "I want to rotate by 15 degrees and zoom 1.2× around the face center" tool - deterministic, sub-millisecond, and the right answer for a job that has no business going through a diffusion model.

    The inputs

    • center (STRING) - the rotation pivot as a literal, [width/2, height/2] style - e.g. [512, 384] for a 1024×768 image, or an arbitrary point like [300, 200] to rotate around that spot. Brackets required, as with all composite literals in this pack.
    • angle (FLOAT) - rotation in degrees. Sign convention: positive angles rotate counter-clockwise in standard image coordinates. A 45 here turns the image counter-clockwise 45°; use -45 for clockwise.
    • scale (FLOAT) - uniform zoom applied before rotation in the matrix math. 1.0 = no scaling, 1.5 = zoom in, 0.8 = shrink.

    Output is a single nparray - the 2×3 affine matrix, ready for warpAffine. Don't preview it; it's a number. The interesting visual work happens downstream in warpAffine, where you also set the output size. If you want to build the same idea from three point correspondences instead, the pack's getAffineTransform is the alternative - 2×3 from 3 points rather than from center+angle+scale.

    Two gotchas that will bite you

    First, the center is in the source image's coordinate space, not "the middle of the output." Rotating around [0, 0] spins the image around its top-left corner, which is probably not what you want - if you want it to stay put visually, center on the middle of the region you're rotating. Second, rotation around a point inevitably pushes content outside the output canvas, so plan the output size in warpAffine accordingly, or you'll crop off the corners you were trying to keep. Rotating a 1024×1024 image by 45° needs a ~1448×1448 canvas to contain it.

    Install

    ComfyUI Manager, search opencv-comfyui (display "OpenCV"), or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/geroldmeisinger/opencv-comfyui
    pip install opencv-contrib-python
    

    Restart, no models. Pack rules: image in/out is NPARRAY via Image2Nparray/Nparrays2Image at batch size 1, and center needs bracket-literal syntax - [512, 384], not 512, 384, or you'll get invalid syntax (<unknown>, line 0). No _1 duplicate for this one - it's the rare function with a single overload, so what you see is what there is.

    Categoryimage/OpenCV

    Inputs (3)

    NameTypeDefaultDescription
    centerSTRING
    angleFLOAT
    scaleFLOAT

    Outputs (1)

    NameTypeDescription
    nparrayNPARRAY