feat: add --offset and --auto-offset for grid translation (issue #6)#26
Open
hai-pilgrim wants to merge 3 commits into
Open
feat: add --offset and --auto-offset for grid translation (issue #6)#26hai-pilgrim wants to merge 3 commits into
hai-pilgrim wants to merge 3 commits into
Conversation
--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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-offsetor a manual--offsetcan shift sample centres onto the true interior of each grid cell.Backward compatible:
--auto-offsetis 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 ofspacingdetect_grid_with_offset(image)→(grid_w, grid_h, offset_x, offset_y)— existing detection + phasecreate_downsampled_imagegainsoffset_x/offset_yparams (default 0)Test plan
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 rangecreate_downsampled_imagewith offset: zero offset = default, positive/negative offset clamping, shift changes sampled values--offsetand--auto-offsetflags accepted without error🤖 Generated with Claude Code