Is there a way to automatically remove an image background?

I'd like to copy an image and then in the clipboard, or in some other way, automatically remove the background. Any ideas on how to do this?

To do this, you'd need a tool that can automatically remove image backgrounds. Until I went looking, I wasn't aware of such a tool—perhaps Photoshop and similar have such a feature that could be scripted, but I'm not a Photoshop user.

What I did find is rembg, a Python tool to remove backgrounds using an AI library called the ONNX Runtime. After some issues getting it all set up, it does work.

The detailed setup work is pretty low-level, so this probably isn't a solution that would be workable for you unless you're comfortable in Terminal, and with Unix apps like homebrew and python. But once installed, removing a background is as simple as this:

rembg i /path/to/source_image.jpg /path/to/new_no_background_image.jpg

So if you had it all installed and working, your macro would have to do the following:

  1. Save the image on the clipboard to a file on your disk—the /tmp folder works well for this.
  2. Use the Execute a Shell Script action to remove the background, and save to a new file in /tmp.
  3. Set the contents of the clipboard to the newly-written file.

You could also, I think, write the above as a new Clipboard Filter, in which case it'd be accessible from the gear menu in the Clipboard History window.

If you have another tool that can do what rembg does via shell script (or AppleScript), then you'd replace step two with that tool's commands. I know this isn't a full solution, but maybe enough to get started?

-rob.

Another totally different approach is to have KM open a web page such as this one:

...then paste the image into that website, then copy the result back to the clipboard. Should be just a few lines of KM code.

Found a simple option.
Take screenshot as a file and manually add to a folder "removebkgd"
Trigger on adding to a folder "removebkgd".
Open Preview
Remove background.

3 Likes