Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can sharp expose the locii for "attention" cropping? #3084

Closed
mceachen opened this issue Feb 9, 2022 · 4 comments
Closed

Can sharp expose the locii for "attention" cropping? #3084

mceachen opened this issue Feb 9, 2022 · 4 comments

Comments

@mceachen
Copy link
Contributor

mceachen commented Feb 9, 2022

Thanks again for sharp, @lovell ! 馃挴 馃

What are you trying to achieve?

I'd like to know what coordinates are being considered when cropping with the "attention" strategy

Have you searched for similar feature requests?

Yes, but I couldn't find anything.

What would you expect the API to look like?

Maybe something like

sharp("input.png").resize({
    width: 200,
    height: 200,
    fit: sharp.fit.cover,
    position: sharp.strategy.attention,
    explain: true // or dryRun? something to tell sharp that we don't actually want to do the resize. 
    // this could just be a wholly new method, like `resizePlan` with the same API as `.resize()`?
})

which would return some sort of struct with information used by the crop algorithm, like

{
  input: {
    width: 1600,
    height: 1200,
  },
  output: {
    left: 100,
    top: 0,
    width: 800,
    height: 800,
  },
}

What alternatives have you considered?

I could try to pixel-match the cropped result with the same image reduced by the same amount, but that would be expensive.

If you think this request is reasonable, I'd be happy to finalize the API with you and submit a PR.

@lovell
Copy link
Owner

lovell commented Feb 9, 2022

Did you see these are already returned by toBuffer() and toFile()?

https://sharp.pixelplumbing.com/api-output#tobuffer
https://sharp.pixelplumbing.com/api-output#tofile

When using a crop strategy also contains cropOffsetLeft and cropOffsetTop.

@mceachen
Copy link
Contributor Author

mceachen commented Feb 9, 2022

Thanks, I'd missed that! That's perfect.

@mceachen mceachen closed this as completed Feb 9, 2022
@ejoebstl
Copy link
Contributor

Hello,

Thanks for your work on sharp, @lovell!

Would it be possible to expose the coordinates of the location of interest, not just the crop offset?

@lovell
Copy link
Owner

lovell commented Nov 12, 2022

@ejoebstl The logic for this lives in smartcrop.c and the "max" score should be the centre of the region returned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants