π Crop & Rotate to 11:14
Your photo of a document, scanned straight in one node β just don't ask for a different ratio
- image
- IMAGE
You shot a document at a weird angle and it came out crooked. Or sideways. Or both. Normally that means opening an editor and manually rotating and cropping until it looks like a scan - the kind of fiddly task that feels wrong to do by hand when you're already in a node graph. This node is the fix: drop your photo in, get a flattened, upright, 11:14 crop out. The catch, and it's a real one, is that it's built for exactly one ratio and rotates based on faces, not text.
CropRotateNode (it shows up as "π Crop & Rotate to 11:14") is the entire pack. One node, one input, one output. Feed it an IMAGE, get back an IMAGE that's had three classic OpenCV document-scan tricks applied in sequence: detect the document's edges and perspective-warp it flat, figure out which way is up, then center-crop to the 11:14 aspect ratio. Think of it as a mini scanner app condensed into a single box in the image/transform category.
How it actually works
The source is refreshingly small and readable - one file, no magic. The pipeline:
- Deskew by edge detection. The image goes grayscale β Gaussian blur β Canny edge detect. OpenCV then finds the largest contour that's roughly a quadrilateral (the code skips anything under 50,000 pixels), and a perspective transform straightens it. This is the same recipe behind every "scanner" app on your phone, and it's the part that handles your 30-degree-angle shot.
- Rotate upright using a face. This is where MediaPipe comes in. The node runs
FaceMeshon the document in all four rotations (0/90/180/270Β°) and keeps the one where the eyes sit above the nose above the mouth. It's not choosing the rotation that looks best to you - it's choosing the rotation where a human face is oriented correctly. - Center-crop to 11/14. Whichever dimension is too long gets shaved from both sides to hit the target ratio.
The inputs and outputs (all two of them)
image(IMAGE, required) - anything ComfyUI hands you: a Load Image output, a frame from a video, whatever.IMAGE(output) - the processed result. Wire it straight into Save Image, or feed it into an upscaler if the flattened doc is going anywhere near print.
And here's the fine print the README gets wrong: it advertises a target_ratio input like "11/14". That parameter does not exist in the shipped code. The ratio is hardcoded as TARGET_RATIO = 11 / 14 in crop_rotate_node.py. If you need, say, a 3:4 or 4:5 crop, you either edit that one constant and restart, or reach for a different node. Beginner trap number one.
Installing it
ComfyUI Manager is the easy path - search "ComfyUI Document Auto Crop" and hit install. Otherwise, the manual route:
cd ComfyUI/custom_nodes
git clone https://github.com/seanjang990/comfyui-document-auto-crop
cd comfyui-document-auto-crop
pip install -r requirements.txt
Then restart ComfyUI. (Note: the README's clone URL literally says github.com/yourname/... - a placeholder that was never fixed. Use the repo above.) The dependency that matters is mediapipe: it's a chunky wheel and downloads its models on first run, so the first time the node executes you may notice a pause. There's also opencv-python and Pillow. No model files to fetch separately, no API key.
Where people get burned
- No face, no rotation. The rotation step is entirely face-driven. A plain text page or receipt with no person in frame will get deskewed and cropped but not rotated - the node just bails to angle 0 with a warning. It's built for documents with a person in them (ID-style portraits, signed forms, selfies holding papers), not for scans of text.
- Debug PNG spam. Every run dumps
step_0_input.png,step_1_doc_crop.pngand friends intoComfyUI/tmp/comfyui_debug_crop. They're handy the first time; after a hundred runs they're disk clutter. Delete the folder whenever you remember. - "Lossless" is marketing. The README says lossless with color preservation. Color is preserved (the RGBβBGR round-trip is clean), but the perspective warp resamples pixels, so it's not truly lossless - and the ratio crop throws away edges of your document by design. If the doc doesn't fit 11:14, you lose content.
- Busy backgrounds. Edge detection needs the document to contrast with its surroundings. Shoot it on a plain surface and you'll get a far better contour.
Tiny pack, single-purpose node, no update drama - and if 11:14 is your ratio, it does exactly one job and does it quietly.
Inputs (1)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | β |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| IMAGE | IMAGE | β |