How It Works
This page explains the algorithms behind the two crop modes: focal point detection and crop area suggestion.
General approach
Both modes follow the same high-level pattern:
- Scale the image down. A preview is generated using a Drupal image style that limits the longest side to 512 pixels. This keeps API payload sizes small and response times fast.
- Ask the AI what to focus on. Before any coordinate work begins, the AI is shown the image and the user prompt. It responds with a short description of what it will focus on (e.g. "the woman's face in the center-left of the frame"). This description is reused in every subsequent request so the AI stays consistent.
- Iterate. The module runs a loop where it draws visual guides on the image, sends it to the AI, and refines the coordinates based on the response. The number of iterations is configurable.
- Return normalized coordinates. The final pixel coordinates are converted to normalized 0-1 values so they apply regardless of the actual image dimensions.
Focal point detection
The focal point algorithm uses a grid-based zoom approach. Rather than asking the AI to pinpoint an exact pixel on a full image (which vision models struggle with), the module progressively narrows down the area.
Step by step
Round 1:
Draw a numbered grid (e.g. 5x5 = 25 cells) over the full image.
Ask the AI: "Which cell contains the focal point?"
AI responds: "Cell 13"
Round 2:
Crop and upscale cell 13 to fill the preview size.
Draw a new 5x5 grid on the zoomed-in cell.
Ask the AI again: "Which cell now?"
AI responds: "Cell 8"
... repeat for max_iterations ...
Final:
On the last zoomed cell, ask for exact X/Y coordinates
within that small area using a pixel ruler overlay.
Each round the visible area shrinks by a factor of grid_columns x grid_rows,
so after two rounds on a 5x5 grid the AI is looking at 1/625th of the original
image. This makes sub-pixel accuracy achievable even with models that are
imprecise at raw coordinate estimation.
Visual aids
To help the AI read coordinates accurately, the module draws:
- A ruler along the top and left edges, with tick marks every 5 pixels and numbers every 50 pixels.
- Grid lines every 2 pixels (thicker lines every 5 pixels) for fine positioning on the final round.
Crop area suggestion
The crop area algorithm uses a batch proposal approach. Instead of asking the AI to produce a single crop and then nudging it, the module asks for multiple candidates at once and lets the AI pick the best one.
Step by step
Round 0 (initial batch):
Show the image with a ruler/grid overlay.
Ask the AI: "Propose 10 different crop rectangles."
AI returns 10 suggestions with varying positions and sizes.
Round 1 .. N-1 (selection + refinement):
Draw all 10 crop rectangles on the image, each with a
distinct colored border (Red, Blue, Green, Yellow, etc.)
and a numbered label.
Ask the AI: "Pick the best one, then propose 10 new
variations near it."
AI responds: selected=3, plus 10 new suggestions.
Round N (final selection):
Draw the final 10 candidates on the image.
Ask the AI: "Just pick the best one."
AI responds: selected=7.
Return the coordinates of the selected crop.
Why batches?
Asking for 10 proposals at once has several advantages over the previous single-proposal-and-refine approach:
- Broader exploration. The AI considers tight crops, wide crops, and various offsets simultaneously, reducing the chance of getting stuck in a local optimum.
- Fewer API calls. Two rounds of batch selection (initial + one refinement) typically outperform five rounds of single-point refinement, at roughly the same number of API calls.
- Visual verification. Drawing all candidates on the image lets the AI compare them side by side rather than relying on memory of previous iterations.
The 10 colors
Each of the 10 proposals is drawn with a unique colored border so the AI can refer to them by number and color:
| # | Color | RGB |
|---|---|---|
| 1 | Red | (255, 0, 0) |
| 2 | Blue | (0, 0, 255) |
| 3 | Green | (0, 160, 0) |
| 4 | Yellow | (255, 255, 0) |
| 5 | Orange | (255, 165, 0) |
| 6 | Purple | (160, 32, 240) |
| 7 | Cyan | (0, 255, 255) |
| 8 | Magenta | (255, 0, 255) |
| 9 | Brown | (139, 69, 19) |
| 10 | Pink | (255, 105, 180) |
Each border is drawn as a 5px black outline with a 3px colored line inside. A small colored pill at the top-left corner of each rectangle shows its number.
Aspect ratio enforcement
The user specifies a target aspect ratio (width / height). The AI is instructed to respect this ratio in every proposal. The module clamps all returned coordinates to the image bounds to prevent out-of-range values.
Image processing
All image drawing (rulers, grids, colored rectangles) is done server-side using either GD or ImageMagick, depending on which image toolkit is configured in Drupal. The module detects the active toolkit automatically.
Temporary files created during processing are tracked and cleaned up after each crop operation completes.