[Feature] add margin/pad parameter to trim()
Feature request
What are you trying to achieve?
I have a bulk of small images which I .trim() to zoom into the actual content.
Later on, I run some analysis on the image which performs better with padding/margin.
Instead of trimming all the way to the content, I'd like to keep a small padding/margin around it.
When you searched for similar feature requests, what did you find that might be related?
https://github.com/lovell/sharp/issues/128
What would you expect the API to look like?
await sharp(input)
.trim({
threshold: 0,
marginPx: 10 // 10px margin around the image when trimming
})
.toFile(output);
What alternatives have you considered?
I'm aware of the .extend() that may help achieve the same thing but it defaults to black as a background colour. I'd like to preserve the original shade.
I suppose, I could also pull the top-left pixel colour, trim and then apply extend with the same colour. However, that may lose potential gradient backgrounds that still fall into the trimming threshold.
In addition, I'm willing to contribute the feature.
Please provide sample image(s) that help explain this feature
Before
After with margin/padding
Sounds good, happy to review and help with a PR that implements this if you're able, thank you. In terms of naming I think margin would be a good name for the property that provides this feature.