Search Docs
Loading...
Skip to content

Background Removal

Add a background removal action to your Android editor so users can remove an image background and continue editing the result.

Remove Background button in the Android Design Editor dock

8 mins
estimated time
GitHub

The Background Removal plugin adds a “Remove Background” button to the dock, and offers one for the inspector bar. The dock button works on the current page’s image. The inspector bar button, which you add with inspectorBarModifier, works on the selected image or page. This guide explores the IMG.LY ONNX implementation, the Google ML Kit implementation, and a custom remover implementation.

This guide builds on the Design Editor starter kit. The same plugin works with any editor configuration that hosts a dock or an inspector bar.

IMG.LY Implementation#

The IMG.LY implementation runs an ONNX segmentation model on the device. Use it when you want to control the model variant, model asset location, and model loading behavior.

dependencies {
implementation("ly.img:plugin-background-removal-imgly:1.86.2")
}

Minimal Implementation#

Compose IMGLYBackgroundRemovalPlugin with your editor configuration. The plugin inserts the dock button and uses the default IMG.LY configuration. The configuration options below add the inspector bar button.

Editor(
license = license,
configuration = {
EditorConfiguration
.remember(::DesignConfigurationBuilder)
.then(::IMGLYBackgroundRemovalPlugin)
},
onClose = onClose,
)

When the user taps a button, the plugin:

  1. Reads the image fill of the current page (dock) or of the selected block (inspector bar).
  2. Runs segmentation on the image.
  3. Composites the foreground over a transparent background.
  4. Replaces the image, and each resolution in its source set, with the result as one undo step.

The plugin writes each result as a PNG file into the app’s cache directory, and the image refers to that local file. Android can clear the cache, so persist the results before you save the scene. Upload each file:// URI that engine.editor.findAllMediaURIs() returns, including each resolution of a source set. Then call engine.editor.relocateResource() with the URI of the upload. The Resources guide covers both calls.

The user can start removals on several images. The model runs on one image at a time, in the order of the taps.

Configuration Options#

Configure IMGLYBackgroundRemovalPlugin when you need to select a model, change where model files are loaded from, control eager or lazy loading, customize network timeouts, add the inspector bar button, or move the buttons.

Editor(
license = license,
configuration = {
EditorConfiguration
.remember(::DesignConfigurationBuilder)
.then(::IMGLYBackgroundRemovalPlugin) {
config = IMGLYBackgroundRemovalConfig(
model = IMGLYBackgroundRemovalConfig.Model.FP16,
modelBaseUri = "https://staticimgly.com/imgly/plugin-mobile-background-removal/1.0.0".toUri(),
loadMode = IMGLYBackgroundRemovalConfig.LoadMode.EAGER,
httpClient = OkHttpClient.Builder()
.connectTimeout(15, TimeUnit.SECONDS)
.readTimeout(120, TimeUnit.SECONDS)
.writeTimeout(120, TimeUnit.SECONDS)
.build(),
)
dockModifier = {
addFirst { Dock.Button.rememberBackgroundRemoval(config = it) }
}
inspectorBarModifier = {
addFirst { InspectorBar.Button.rememberBackgroundRemoval(config = it) }
}
}
},
onClose = onClose,
)
Option Default Description
config.model IMGLYBackgroundRemovalConfig.Model.FP16 Chooses the ONNX model variant. FP32 favors quality and file size is largest, FP16 is the default balance, and QUINT8 is smaller and faster with a stronger quality trade-off.
config.modelBaseUri https://staticimgly.com/imgly/plugin-mobile-background-removal/1.0.0 Base URI used to resolve the selected model file. The plugin appends config.model.key, for example isnet_fp16.onnx. You can download the models and store them in your own CDN, or download the files into the assets folder of your app and set modelBaseUri = "file:///android_asset".toUri().
config.loadMode IMGLYBackgroundRemovalConfig.LoadMode.EAGER EAGER starts loading the model during plugin initialization. LAZY waits until the first removal action.
config.httpClient Default OkHttpClient HTTP client used to load source images and remote model assets. Override it for app-specific timeouts or interceptors.
dockModifier Adds the button first Changes where Dock.Button.rememberBackgroundRemoval() is inserted in the dock.
inspectorBarModifier null Inserts InspectorBar.Button.rememberBackgroundRemoval() into the inspector bar. When it is null, the plugin leaves the inspector bar unchanged.
  • When a removal fails, the plugin logs the error with the tag BackgroundRemovalPlugin and leaves the image unchanged.
  • The model URI is modelBaseUri, a /, and the file name of config.model. When the model does not load, check that this URI resolves to a readable .onnx file.

Google Implementation#

The Google implementation uses Google’s on-device ML Kit Selfie Segmentation. Use it for photos of people.

dependencies {
implementation("ly.img:plugin-background-removal-google:1.86.2")
}

Minimal Implementation#

Compose GoogleBackgroundRemovalPlugin with your editor configuration.

Editor(
license = license,
configuration = {
EditorConfiguration
.remember(::DesignConfigurationBuilder)
.then(::GoogleBackgroundRemovalPlugin)
},
onClose = onClose,
)

Configuration Options#

Configure GoogleBackgroundRemovalPlugin when you need to customize the HTTP client, add the inspector bar button, or move the buttons.

Editor(
license = license,
configuration = {
EditorConfiguration
.remember(::DesignConfigurationBuilder)
.then(::GoogleBackgroundRemovalPlugin) {
config = GoogleBackgroundRemovalConfig(
httpClient = OkHttpClient.Builder()
.connectTimeout(15, TimeUnit.SECONDS)
.readTimeout(120, TimeUnit.SECONDS)
.writeTimeout(120, TimeUnit.SECONDS)
.build(),
)
dockModifier = {
addFirst { Dock.Button.rememberBackgroundRemoval(config = it) }
}
inspectorBarModifier = {
addFirst { InspectorBar.Button.rememberBackgroundRemoval(config = it) }
}
}
},
onClose = onClose,
)
Option Default Description
config.httpClient Default OkHttpClient HTTP client used by the plugin when it loads source images. Override it for app-specific timeouts or interceptors.
dockModifier Adds the button first Changes where Dock.Button.rememberBackgroundRemoval() is inserted in the dock.
inspectorBarModifier null Inserts InspectorBar.Button.rememberBackgroundRemoval() into the inspector bar. When it is null, the plugin leaves the inspector bar unchanged.

Custom Background Remover#

For custom segmentation, include the base plugin package and implement BackgroundRemover. Your config object selects the remover, and BackgroundRemovalPlugin keeps the same buttons and editor integration.

dependencies {
implementation("ly.img:plugin-background-removal:1.86.2")
}

Minimal Implementation#

Implement BackgroundRemovalConfig to select your remover, then compose the base plugin with your custom config. initialize() prepares your backend, and processImage() returns the foreground mask.

private data class CustomBackgroundRemovalConfig(
override val httpClient: OkHttpClient = OkHttpClient(),
) : BackgroundRemovalConfig {
override val remover: BackgroundRemover<*> = CustomBackgroundRemover()
}
private class CustomBackgroundRemover : BackgroundRemover<CustomBackgroundRemovalConfig> {
override fun EditorScope.initialize() {
// Prepare local models, SDK clients, or service credentials here.
}
override suspend fun EditorScope.processImage(bitmap: Bitmap): BackgroundRemovalMask {
val width = bitmap.width
val height = bitmap.height
val buffer = ByteBuffer
.allocateDirect(width * height * Float.SIZE_BYTES)
.order(ByteOrder.nativeOrder())
// Dummy data filled with 1s.
repeat(width * height) {
buffer.putFloat(1f)
}
buffer.rewind()
return BackgroundRemovalMask(
buffer = buffer,
width = width,
height = height,
)
}
}
Editor(
license = license,
configuration = {
EditorConfiguration
.remember(::DesignConfigurationBuilder)
.then(::BackgroundRemovalPlugin) {
config = CustomBackgroundRemovalConfig()
}
},
onClose = onClose,
)

Configuration Options#

Configure BackgroundRemovalPlugin when you need to add the inspector bar button or move the buttons. This example moves the dock button to the end of the dock and adds the inspector bar button at the end of the inspector bar:

Editor(
license = license,
configuration = {
EditorConfiguration
.remember(::DesignConfigurationBuilder)
.then(::BackgroundRemovalPlugin) {
config = CustomBackgroundRemovalConfig()
dockModifier = {
addLast { Dock.Button.rememberBackgroundRemoval(config = it) }
}
inspectorBarModifier = {
addLast { InspectorBar.Button.rememberBackgroundRemoval(config = it) }
}
}
},
onClose = onClose,
)
Option Default Description
dockModifier Adds the button first Changes where Dock.Button.rememberBackgroundRemoval() is inserted in the dock. A modifier that does not insert the button removes it from the dock.
inspectorBarModifier null Inserts InspectorBar.Button.rememberBackgroundRemoval() into the inspector bar. When it is null, the plugin leaves the inspector bar unchanged.

API Reference#

API Purpose
IMGLYBackgroundRemovalPlugin Adds the background removal action with the IMG.LY ONNX Runtime backend.
IMGLYBackgroundRemovalConfig Configures IMG.LY model selection, model URI, loading behavior, and HTTP loading.
GoogleBackgroundRemovalPlugin Adds the background removal action with Google’s on-device segmentation backend.
GoogleBackgroundRemovalConfig Configures HTTP loading for the Google implementation.
BackgroundRemovalPlugin Base plugin used with a custom BackgroundRemovalConfig.
BackgroundRemover.initialize() Prepares a custom remover before use.
BackgroundRemover.processImage(bitmap=_) Produces the foreground mask for an image.
Dock.Button.rememberBackgroundRemoval(config=_) Creates the reusable background removal dock button.
InspectorBar.Button.rememberBackgroundRemoval(config=_) Creates the reusable background removal inspector bar button.
EditorConfiguration.then() Composes the selected plugin with the base editor configuration.

Next Steps#