8000 feat: smart cropping for cover with "entropy" and "attention" by hipstersmoothie · Pull Request #1566 · jimp-dev/jimp · GitHub
[go: up one dir, main page]

Skip to content

feat: smart cropping for cover with "entropy" and "attention" - #1566

Merged
hipstersmoothie merged 1 commit into
mainfrom
sesh/smart-crop
Nov 25, 2026
Merged

hipstersmoothie merged 1 commit into
mainfrom
sesh/smart-crop

Conversation

@hipstersmoothie
Copy link
Copy Markdown
Collaborator

Adds content-aware cropping to cover, like sharp's position: "entropy" | "attention", for avatars and cover photos where the subject is off-center.

API

// align takes a strategy as well as flags and { x, y } fractions
image.cover({ w: 150, h: 320, align: "attention" });
image.cover({ w: 150, h: 320, align: "entropy" });

// new: the rectangle cover would keep, in the image's pixels, without changing it
image.coverRect({ w: 1200, h: 630, align: "attention" }); // { x, y, w, h }
  • "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 align and not a new option

align already 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's position works too. A separate option would need rules for what happens when both are set. Strings serialize as JSON, so apply(), @jimp/worker and edits URLs take them as they are.

How it works

  • cover only 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.
  • The analysis runs on a copy at most 256 px on its longest side. Each analysed pixel averages at most 4×4 evenly spaced source samples, so cost is bounded even for huge images: 1–9 ms on cops.jpg and on the 10000 × 783 panorama.
  • Determinism: it's plain float math, with no randomness. Windows within 1% of the best score count as ties, and the one nearest the middle wins, so noise can't push the crop to an edge.
  • A strategy only decides alignX/alignY. The scale-then-crop path is the same as for fixed alignment.

Elsewhere

  • @jimp/layout: images get objectPosition ({ x, y }, "entropy" or "attention"), passed to cover. The name follows CSS object-position, because position is already relative/absolute. contain uses { x, y } and centers for strategies.
  • @jimp/server: pos=entropy and pos=attention. With fit other than cover they're a 400.
  • apply / @jimp/worker: no code changes were needed. Tests cover both.
  • Docs: a "Smart cropping" section on the cover page with before/after images (scripts/cover-images.mjs renders them into public/cover/), a new coverRect page and methods.ts entry, the cover playground op, and the render and image-server pages.

Tests

  • plugin-cover: synthetic cases for each strategy and axis, flat images, transparency, the coverRect/cover match, determinism, unknown strategies and ap 8000 ply. Image snapshots on cops.jpg, plus a generous timing bound on the panorama.
  • layout: an objectPosition render snapshot. server: pos params. worker: a smart cover in the worker matches the main thread.
  • Root pnpm build (including the browser bundle check), pnpm lint and astro check pass.

🤖 Generated with Claude Code

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>
@hipstersmoothie hipstersmoothie added the minor Increment the minor version when merged label Nov 25, 2026
@hipstersmoothie
hipstersmoothie merged commit ac1131b into main Nov 25, 2026
7 checks passed
@hipstersmoothie
hipstersmoothie deleted the sesh/smart-crop branch November 25, 2026 03:29
@hipstersmoothie
Copy link
Copy Markdown
Collaborator Author

🚀 PR was released in v2.2.0 🚀

@hipstersmoothie hipstersmoothie added the released This issue/pull request has been released. label Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

minor Increment the minor version when merged released This issue/pull request has been released.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0