Repository navigation
feat: smart cropping for cover with "entropy" and "attention" - #1566
Merged
Merged
Conversation
cover's align now takes "entropy" (keep the most detailed window) or "attention" (edges, saturation and skin tones), like sharp's position strategies. Both analyse a copy at most 256 px on its longest side and slide a window along the one axis cover crops, so they take a few ms and are deterministic. coverRect returns the rectangle cover would keep. Also wired into @jimp/layout (objectPosition on images), @jimp/server (pos=entropy|attention with fit=cover), apply and @jimp/worker, with docs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Collaborator
Author
|
🚀 PR was released in |
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.
Adds content-aware cropping to
cover, like sharp'sposition: "entropy" | "attention", for avatars and cover photos where the subject is off-center.API
"entropy"keeps the window whose luminance histogram (64 bins, weighted by alpha) has the most Shannon entropy."attention"keeps the window with the highest saliency. Each pixel scores Sobel edge strength plus saturation plus 3 × skin tone (a soft YCbCr box), times alpha. Windows are weighted toward their middle, so one subject gets framed instead of two being cut in half.Why
alignand not a new optionalignalready answers "which part does cover keep", and it already takes two shapes (flags,{ x, y }). A strategy is a third answer to the same question, which is how sharp'spositionworks too. A separate option would need rules for what happens when both are set. Strings serialize as JSON, soapply(),@jimp/workerandeditsURLs take them as they are.How it works
coveronly ever crops one axis, so the search is 1-D. It slides a window the size of the kept part along that axis and scores every position exactly. There's no greedy edge-trimming as in libvips.alignX/alignY. The scale-then-crop path is the same as for fixed alignment.Elsewhere
@jimp/layout: images getobjectPosition({ x, y },"entropy"or"attention"), passed tocover. The name follows CSSobject-position, becausepositionis already relative/absolute.containuses{ x, y }and centers for strategies.@jimp/server:pos=entropyandpos=attention. Withfitother thancoverthey're a 400.apply/@jimp/worker: no code changes were needed. Tests cover both.coverpage with before/after images (scripts/cover-images.mjsrenders them intopublic/cover/), a newcoverRectpage andmethods.tsentry, the cover playground op, and therenderand image-server pages.Tests
ap 8000 ply. Image snapshots on cops.jpg, plus a generous timing bound on the panorama.objectPositionrender snapshot. server:posparams. worker: a smart cover in the worker matches the main thread.pnpm build(including the browser bundle check),pnpm lintandastro checkpass.🤖 Generated with Claude Code