Removing a background
Lesson "remove-background", in the group Pictures: techniques of the lesson list. Steps: 9.
What you will be able to do
A picture whose background is made transparent, saved as PNG.
What you need
- It goes on from A first picture from text. The card says so; it is a hint, not a lock.
- Nodes: Load Image, Load Background Removal Model, Remove Background, Invert Mask, Join Image with Alpha, Save Image.
- The app downloads and installs nothing. The step texts below say which files and folders, and which ComfyUI template names them.
The steps
The words of each step, as the lesson shows them. Node names are in bold.
How a background is removed (reading)
A background removal model looks at a picture and says which pixels belong to the subject. ComfyUI turns that into transparency: what is not the subject disappears.
The nodes for this are part of new versions of ComfyUI. The lesson builds the graph from an empty canvas: "File ▸ New project".
Load the picture
Add a Load Image node and choose the picture in its
imagefield.A clear subject against a background works best: a person, a product, an animal.
Load the model
Add a Load Background Removal Model node and choose the model in its
bg_removal_namefield, for example BiRefNet. ComfyUI looks for these files in themodels/background_removalfolder.If the file you need is not in the list, your ComfyUI does not have it. The app downloads nothing. The easiest way is to open, in ComfyUI itself, the official template "Remove Background (BiRefNet)": it shows which files are missing and where to put them. Then choose "Sync ▸ Sync now" in this app's menu, and the files appear in the lists.
Find the subject
Add a Remove Background node. The output of Load Background Removal Model goes into its
bg_removal_modelinput, the picture intoimage.Its
maskoutput is the subject: white where it is, black where the background is.Turn the mask inside out
Add an Invert Mask node and connect the
maskoutput of Remove Background to itsmaskinput.The next node reads a mask as "what to hide". The subject must stay, so the mask is turned inside out first, as ComfyUI's own template does.
Make the background transparent
Add a Join Image with Alpha node. The picture goes into its
imageinput, the output of Invert Mask intoalpha.The node adds the mask to the picture as transparency.
Save it
Add a Save Image node and connect the output of Join Image with Alpha to its
imagesinput.It writes PNG, and PNG keeps transparency. JPEG has none: the transparent part would turn into a colour.
Other ways (reading)
- To put the subject on another background, take an Image Composite Masked node: the new background, the subject and the mask go into it.
- The ComfyUI-RMBG pack holds many removal models, also for hair and clothes. A node pack runs other people's code on your computer. Install it yourself through ComfyUI's manager, from its official page, and only if you trust its authors. The app installs nothing.
- To take not the whole subject but one named object, see "A mask by words".
The graph is ready (reading)
Save the graph with "File ▸ Save workflow for ComfyUI…", open the file in ComfyUI and run it there. This app runs nothing in ComfyUI itself.
What to try: a picture with hair or fur against a busy background, the hardest case for any model.
What the lesson checks
A reading step is done when you press Got it, next. A doing step ticks itself off when the canvas holds:
- Step 2, Load the picture:
- a Load Image node.
imageof Load Image: a value is chosen.
- Step 3, Load the model:
- a Load Background Removal Model node.
bg_removal_nameof Load Background Removal Model: a value is chosen.
- Step 4, Find the subject:
- a Remove Background node.
bg_modelof Load Background Removal Model intobg_removal_modelof Remove Background.IMAGEof Load Image intoimageof Remove Background (a Upscale Image or Upscale Image By or Scale Image to Total Pixels or FluxKontextImageScale or Resize Image or ImageResizeKJv2 or Pad Image for Outpainting in between is accepted).
- Step 5, Turn the mask inside out:
- a Invert Mask node.
maskof Remove Background intomaskof Invert Mask.
- Step 6, Make the background transparent:
- a Join Image with Alpha node.
IMAGEof Load Image intoimageof Join Image with Alpha (a Upscale Image or Upscale Image By or Scale Image to Total Pixels or FluxKontextImageScale or Resize Image or ImageResizeKJv2 or Pad Image for Outpainting in between is accepted).MASKof Invert Mask intoalphaof Join Image with Alpha.
- Step 7, Save it:
- a Save Image node.
IMAGEof Join Image with Alpha intoimagesof Save Image.