Guides
Separate foreground & background
֍

Generate an image, remove the background, and fill the foreground

In this example, we'll create a branching image generation workflow that:

  • Generates an image using GenerateImage
  • Removes the background from the image using RemoveBackground
  • Generates a masked foreground from the image using RemoveBackground, with return_mask enabled
  • Fills the masked foreground using EraseImage

First, initialize Substrate:

Python
TypeScript

from substrate import (
Substrate,
GenerateImage,
RemoveBackground,
EraseImage,
sb,
)
s = Substrate(api_key=YOUR_API_KEY)

Create and connect four nodes and run them:

Python
TypeScript

image = GenerateImage(
prompt="a dark red chesterfield leather wing chair in a dark majestic room, pillars, celestial galaxy wallpaper",
)
bg = RemoveBackground(image_uri=image.future.image_uri)
mask = RemoveBackground(
image_uri=image.future.image_uri,
return_mask=True,
)
erase = EraseImage(
image_uri=image.future.image_uri,
mask_image_uri=mask.future.image_uri,
)
res = substrate.run(erase)

Run this example
image

res.get(image)

no background

res.get(removeBg)

image

res.get(removeBgMask)

image

res.get(fillMask)