Nodes/AK Pack/AK Replace Alpha with Color
ComfyUI Node

AK Replace Alpha with Color

Swap a transparent background for a solid color (and back)

By akawana·Created 9 months ago·Updated 7 days ago· 2
AK Replace Alpha with Color
  • image
  • mask
  • IMAGE
color_pick_modeuser_color
threshold0
color_rgb8, 39, 245
pad_size0

Most diffusion models can't see an alpha channel - feed them an RGBA cutout and the transparent parts turn into noise or get ignored. The standard trick is to flatten transparency onto a solid color, generate, then strip that color back off at the end. This node is the first half of that round trip: it replaces the alpha channel with a color you choose, and its sibling AK Replace Color with Alpha reverses it before you save. It's a small, focused tool for people who work with transparent-background art - product shots, character cutouts, sticker sheets.

How it works

You hand it an image and it composites the transparent pixels onto your chosen background color, exactly like flattening a PNG in an editor:

  • color_pick_mode - user_color uses the color you type; auto_color lets the node pick one for you.
  • color_rgb - your color as R, G, B text (default 8, 39, 245). Only used in user_color mode.
  • threshold - a 0–255 tolerance used in auto mode: how far the chosen color must be from the image's existing pixels before the node accepts it. This is the "make sure the background doesn't collide with anything in the art" dial.
  • image - the RGBA image.
  • mask (optional) - if you pass a mask, the node blends toward the color by the mask instead of by the alpha channel.
  • pad_size - adds a border around the result, padded with the chosen color. The README's reasoning is practical: rendered edges often carry color artifacts, so you can pad out and crop away the outermost ring. The pad scales proportionally to the image aspect ratio rather than uniformly.

Output is a single IMAGE.

In auto_color mode the node actually analyzes the pixels: it samples the image, builds a coarse grid of candidate colors, and picks the one farthest from everything currently in the picture - so your flat background lands as far from the subject's palette as possible. If no candidate clears the threshold, it falls back to your color_rgb value.

The gotcha that catches everyone

If your image has no alpha channel and you don't provide a mask, this node does nothing. It needs something to drive the replacement - either the alpha channel of an RGBA image, or a mask on an RGB one. Feed it a plain opaque JPEG and you'll get it back unchanged, which reads as "the node is broken" the first time you hit it. Check your image actually has transparency before you blame the node.

Also worth knowing: if you pass a mask whose resolution doesn't match the image, the node bails and returns the image untouched rather than resampling. Match your mask size.

When you'd actually use it

The intended pipeline, straight from the README: replace alpha with color, run your generation on the flattened image (img2img, ControlNet, whatever), then before saving, run AK Replace Color with Alpha to pull that color back out into transparency. The color you flatten onto matters - pick something that's not in your subject, or use auto_color to let the node dodge your palette for you.

Install

AK Pack standard. ComfyUI Manager → search "AK Pack" → install → restart, or:

cd ComfyUI/custom_nodes
git clone https://github.com/akawana/ComfyUI-AK-Pack

No extra pip dependencies, no models. This one is pure PyTorch.

Honest verdict

It's a one-trick node and the trick is solid, but check whether you need it: if you're only flattening for a single pass, most image editors do this in three clicks, and several inpainting/background packs include their own flatten-and-restore pair. Where AK's version earns its place is inside a big transparent-art workflow where you want the flattening and un-flattening to happen in the graph, deterministically, with the same color on both ends. It's the same-author ecosystem playing nicely with itself.

CategoryAK/image

Inputs (6)

NameTypeDefaultDescription
color_pick_modeCOMBOuser_color2 options: user_color, auto_color
thresholdINT00–255
imageIMAGE
color_rgbSTRING8, 39, 245
pad_sizeINT0
maskoptMASK

Outputs (1)

NameTypeDescription
IMAGEIMAGE