# Sprite DX - Stage 4 - Automating Background Removal Process

Yesterday, we automated process of splitting shots and extracting clean loops. Next is background removal.

## Ingredients

We start with those loop videos that we created in previous [post](https://blog.sprited.app/sprite-dx-stage-3-adding-custom-comfyui-nodes).

**Idle Loop**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1754154276198/f1d0765a-e252-4cfb-9d7d-1695c7341bfe.webp align="center")

**Run Loop**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1754154280705/21f175c4-17ee-4230-989d-c56596ac0844.webp align="center")

---

## Approach

We explored few approaches before ([1](https://blog.sprited.app/sprite-animation-generation-background-removal)), ([2](https://blog.sprited.app/sprite-animation-generation-background-removal-2)), and landed on [an approach](https://blog.sprited.app/sprite-animation-generation-background-removal-2#heading-flux-kontext-pro-hack) that uses *FLUX.1 Kontext* to replace the background color to black and subtracting the original to get a alpha mask of the background. We have not extensively tested it but initial results showed promise.

So, in this section, we create a ComfyUI custom node that will repeat this process for every frame of the input video.

* We first load the video using `Load Video` node from VHS, and resize the all frames into 256×256.
    
* Then, we create a sprite sheet of 4 rows and 4 columns (total 1024×1024 pixels, which is one of the native resolutions for FLUX.1 Kontext).
    
* We then pass the image to FLUX.1 Kontext with a prompt “Chagne the background to black.“
    
* Then we subtract original whiteBG sheet and blackBG sheet to produce a mask.
    
* We post process the mask by applying a level.
    
* Then, apply the alpha mask
    
* Break the grid into the frames and slice empty frames.
    
* Then, we save it as a webp.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1754198633522/d7f7d637-721e-41c7-abdb-7c767722798b.png align="center")

## Result

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1754199043700/d3e099f4-9365-4337-8b46-5f676a98407f.webp align="center")

Gotta go.

—Sprited Dev
