Skip to content

feat: add --offset and --auto-offset for grid translation (issue #6)#26

Open
hai-pilgrim wants to merge 3 commits into
marksverdhei:mainfrom
hai-pilgrim:feat/grid-translation
Open

feat: add --offset and --auto-offset for grid translation (issue #6)#26
hai-pilgrim wants to merge 3 commits into
marksverdhei:mainfrom
hai-pilgrim:feat/grid-translation

Conversation

@hai-pilgrim
Copy link
Copy Markdown

Summary

Refs #6

Adds grid translation — control over where sample centres are placed relative to the detected grid.

New flags:

  • --offset XxY — manually shift all sample centres right by X pixels and down by Y pixels (e.g. --offset 2x3). Useful when you know the grid doesn't start at pixel 0.
  • --auto-offset — automatically detects the grid phase from the gradient profile and applies the best-fit offset. Improves alignment when grid lines are not at pixel 0.

When to use: If the auto-detected grid produces slightly blurry or misaligned output, --auto-offset or a manual --offset can shift sample centres onto the true interior of each grid cell.

Backward compatible: --auto-offset is opt-in. Default behaviour (no offset) is unchanged — all existing tests pass.

Internals

  • find_grid_offset(profile, spacing) — O(spacing) phase scan that maximises gradient energy at multiples of spacing
  • detect_grid_with_offset(image)(grid_w, grid_h, offset_x, offset_y) — existing detection + phase
  • create_downsampled_image gains offset_x/offset_y params (default 0)

Test plan

  • 18 new tests in tests/test_grid_translation.py:
    • find_grid_offset: known-offset synthetic profiles, edge cases (empty, zero spacing, short profile)
    • detect_grid_with_offset: 4-tuple shape, failed detection returns (0,0,0,0), offset in valid range
    • create_downsampled_image with offset: zero offset = default, positive/negative offset clamping, shift changes sampled values
    • CLI: --offset and --auto-offset flags accepted without error
  • All 71 existing tests pass

🤖 Generated with Claude Code

marksverdhei and others added 3 commits March 28, 2026 23:35
--res WxH forces the output to an exact pixel resolution using NEAREST
resampling (pixel-art safe). --aspectratio W:H center-crops the output
to the given aspect ratio. --res takes precedence when both are given.

  spritegrid input.png --res 32x32 -o sprite.png
  spritegrid input.png --aspectratio 4:3 -o cropped.png

Includes 18 unit tests covering parsing, resizing, cropping, and
edge cases (identity, centering, portrait/landscape).

Closes marksverdhei#4

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…erdhei#6

Adds two new ways to control where the grid origin sits:

- `--offset XxY`: manually shift all sample centres right by X and down by Y
  pixels (e.g. `--offset 2x3`)
- `--auto-offset`: auto-detect the grid phase from the gradient profile and
  apply the best-fit offset; improves alignment when the grid doesn't start
  at pixel 0

Internals:
- `find_grid_offset(profile, spacing)` — O(spacing) scan to find the phase
  offset that maximises gradient energy at grid-line positions
- `detect_grid_with_offset(image)` → `(grid_w, grid_h, offset_x, offset_y)` —
  wraps the existing detection and appends the detected phase
- `create_downsampled_image` gains `offset_x`/`offset_y` params (default 0)
- Auto-offset is **opt-in** to preserve backward-compatible output

Tests: 18 new cases in `tests/test_grid_translation.py`

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Pillow 12 deprecates Image.Image.getdata in favour of get_flattened_data
(slated for removal in Pillow 14). Update the two affected comparisons in
test_grid_translation.py to silence the DeprecationWarning.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants