Add the option to provide a specific background colour to trim #3332
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.
I've got a use case to trim just a specific background colour of an image. Mostly to detect letter and pillar boxes in images by trimming off any black areas. This pull request adds the option to do so, by providing this colour as a string or object, which will be parsed by the
color
module.For me, it'd be more typical to provide a specific colour and use the default threshold, so I made the former the first optional argument. However, the Javascript side will handle the case where a
threshold
is provided as the first argument to retain backwards compatibility. I'm not sure if I made it fully clear in the docs that both arguments are optional, maybe you can improve my phrasing if you agree?The output of the
color
module is always 8-bit, so it's scaled accordingly if the image is 16-bit. I've noticed that thethreshold
option is used as an absolute value in find_trim instead of being scaled relatively to the interpretation, therefore that's now multiplied as well.Instead of
.flatten()
ing the background color if the imageHasAlpha()
, the alpha value is now simply discarded. I believe that this should give the same result because of how find_trim uses the same background color when it flattens the image. At least all existing unit tests still pass.Finally, I've based this pull request on the
eagle
branch because e0d3c6e and cbf741c affect the same part of the code. And I also encountered some failing tests when running themain
branche on my M1 MacBook Pro.