lovell / sharp

High performance Node.js image processing, the fastest module to resize JPEG, PNG, WebP, AVIF and TIFF images. Uses the libvips library.
https://sharp.pixelplumbing.com
Apache License 2.0
29.27k stars 1.3k forks source link

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

Closed mceachen closed 2 years ago

mceachen commented 2 years ago

Thanks again for sharp, @lovell ! :100: :1st_place_medal:

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 commented 2 years ago

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 commented 2 years ago

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

ejoebstl commented 2 years ago

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 commented 2 years ago

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