Nodes/comfyui-panels/Sort Panels
ComfyUI Node

Sort Panels

Put Your Comic Panels Back in Reading Order

By bmad4ever·Created 12 months ago·Updated 2 months ago· 41
Sort Panels
  • panels
  • POLYGON
left2righttrue
centerTOP-LEFT

If you're building comic or manga pages in ComfyUI, panel order is quietly the most annoying thing to get right. This node is the fix: it takes a list of shapely.Polygon panels and tries to sort them into reading order - top-left to bottom-right by default, or right-to-left for manga. Panels that come straight out of Build Layout Panels are already ordered correctly, because the pack builds them in cut order. The moment you detect panels from an image with Detect Panels In Image, or shuffle them with list operations, all bets are off. That's when bmad_SortPanels earns its keep.

The author is upfront that it's a heuristic - the node's own description says "simple algo that attempts to sort." Don't treat the output as gospel, but for typical grid-ish page layouts it's right.

How it works

It's a quantized-grid sort, which sounds fancy and isn't. The node finds the global bounding box of all your panels, divides it into an n×n grid (where n = number of panels), then bins each panel into a grid cell based on a reference point you choose. Finally it sorts row by row, left to right - or right to left if you flip the flag.

The clever bit is that it works on bins, not exact coordinates. Panels that don't align perfectly to a strict grid - which is most real panels - get snapped to a cell instead of compared by raw position, so slightly misaligned panels still sort into sane reading order.

The inputs that matter

  • panels - your list of polygons. This input takes a list, so it wires straight out of Detect Panels In Image or Build Layout Panels.
  • left2right - boolean, true by default. Flip it to false for right-to-left manga reading order.
  • center - the reference point used for sorting: TOP-LEFT, CENTER, or BOTTOM-RIGHT of each panel's bounds. Default is TOP-LEFT, which is right for panels that touch the top edge of the page. For oddly shaped panels whose corners are far from where they "belong," CENTER is more forgiving.

The output is a POLYGON list - the same polygons, just reordered. Wire it into the next stage of your page assembly: converting each panel to a mask (Panel to Mask / Polygon To Resized Mask), or feeding a per-panel loop that crops and regenerates each panel before Paste Crops stitches them back together.

Where people get burned

Two things. First, the quantization is a grid, so a page with one giant splash panel and a strip of tiny gutters can fool it - the splash's reference point lands in the wrong cell and the whole order wobbles. If your output looks wrong, try switching center before you blame the layout. Second, sorting fixes order only - it won't fix panels that overlap, and the reading direction is baked in by you, not inferred from the art.

Install

Every node in this pack installs the same way. Easiest: ComfyUI Manager, search comfyui-panels, install, restart. By hand:

cd ComfyUI/custom_nodes
git clone https://github.com/bmad4ever/comfyui_panels
cd comfyui_panels
pip install -r requirements.txt

Then restart ComfyUI. Dependencies are shapely, matplotlib, and opencv-python - no model downloads, nothing to weigh down your disk. Everything lands under the Bmad/Panels menu. It's a niche pack for a very specific workflow, so don't expect a huge community around it, but the geometry is solid and the code is MIT-licensed if you want to poke at the algorithm.

CategoryBmad/Panels

Inputs (3)

NameTypeDefaultDescription
panelsPOLYGON
left2rightBOOLEANtrue
centerCOMBOTOP-LEFTpoint used for sorting.

Outputs (1)

NameTypeDescription
POLYGONPOLYGONPanels (Shapely Polygons)