unjs / ipx

🖼️ High performance, secure and easy-to-use image optimizer.
MIT License
2.05k stars 67 forks source link

Background removal / custom handlers #181

Open TorbjornHoltmon opened 1 year ago

TorbjornHoltmon commented 1 year ago

Describe the feature

I have tested adding a remove background handler with: https://github.com/imgly/background-removal-js/tree/main/packages/node

It uses sharp, so it introduces the possibility of dependency duplication. It also has a Microsoft machine learning library that's quite big.

The POC worked fine, and I think background removal is advantageous. There were some bugs, some images were made 100% transparent, so it is not flawless.

But I am unsure if IPX should have it.

Adding custom handlers would be nice, so I can add it myself. But it probably would not be used very often. A lot of effort for something less useful.

What does team unjs think?

Additional information

pi0 commented 1 year ago

Thanks for making the issue. It seems a nice idea to support optional custom sharp handlers.

I haven't looked in depth of your code but usage seems a high level removeBackground utility only. Does it also expose a lower level API that accepts sharp instance to apply transform?

TorbjornHoltmon commented 1 year ago

It is only a high level API as you said.

I might see if I can expose a lower level API and open a pull request to their library, and then get back to this issue.

Even with the lower API exposed, it still requires the machine learning models that are between ~40MB to ~80MB. So that's something to consider.

I can open a pull request with a first iteration for you to look at when I get the time.

pi0 commented 1 year ago

Sure Thanks 🙏 and feel free to also ping me if opened a PR in upstream. I mainly want to make sure we cane make use of any new custom api in ipx when we introduce .

TorbjornHoltmon commented 1 year ago

I had a look at the remove background library and attempted to refactor it so that it could be used with IPX. It ended up being too complex for me. I have to forfeit.