Nodes/RUI-Nodes/像素化 / Pixelate
ComfyUI Node

像素化 / Pixelate

Real sprites from photos, and fake pixel art back to real

By rui40000·Created 3 years ago·Updated about 8 hours ago· 17
像素化 / Pixelate
  • image
  • mask
  • image
  • mask
  • info
mode按目标宽度
target_width64
pixel_size8
downsample主导色 dominant(像素画首选)
palette自适应 k-means(质量优先)
palette_size16
dither无(默认)
output_scale1
dither_strength1.00
mask_threshold0.50
seed0

A mosaic filter is a lie. It smears your image into big squares and hands back a same-size JPEG that'll look terrible in an actual game. 像素化 / Pixelate is the honest version: it outputs a genuinely small image with controlled color counts and hard, aliased edges - a sprite you can drop straight into an engine. Pure numpy/PIL, no model, no weights.

The headline feature is the third mode. AI image models love producing "pixel art" at 1024×1024 with a fake grid, anti-aliased edges, and thousands of colors - which is to say, not pixel art at all. The auto grid-detection mode finds the actual pixel grid hidden in the image and reconstructs the true pixel art underneath.

Three modes, three jobs

  • 按目标宽度 (target width) - photo or illustration → pixel art. Give it the output width (16/32/48/64 are the classic sprite sizes); height follows the aspect ratio.
  • 按像素块大小 (pixel block size) - every N×N source pixels become one output pixel. The precise choice when you know your downscale factor.
  • 自动检测网格 (auto-detect grid) - the fake-pixel-art restorer. It scores candidate grids with within-cell variance (not edge diffs, which anti-aliased AI output destroys), kills harmonic octave errors, and checks that the winning period is phase-sensitive - a genuine pixel grid is, a repeating pattern in the content isn't.

The detection is genuinely good on clean upscaled images (27/27 in the README's test, zero octave errors) and roughly two-thirds on heavily blurred "AI pixel" inputs - when it fails, the info output tells you the confidence and you fall back to mode two.

Inputs that matter

  • mode / target_width / pixel_size - the three-way choice above.
  • downsample - dominant (the block's most common color, never invents new ones) is the pixel-art pick. median resists noise, mean softens edges, center is the sharpest but noisiest.
  • palette - the style lever. Adaptive k-means in CIELAB space (quality) or median cut (speed), or fixed retro palettes: PICO-8 16-color, Game Boy 4-green, 1-bit black & white, and 4/8/16-level greys.
  • palette_size - 8–16 for that strong retro look, 32–64 to keep detail.
  • dither - Bayer (regular dot pattern, very retro, tiles well), Floyd-Steinberg (smoothest), or off. Hand-drawn style usually wants none.
  • output_scale - the one that will bite you. 1 = true pixel size, and that's what you export. Anything above 1 is nearest-neighbor upscaling just so you can see the result in the ComfyUI preview - export before you forget and get a 32× sprite inflated to 512.

Outputs: image, plus mask (the pixelated alpha if you fed one in) and info (detected grid size, phase, and confidence).

The workflow that makes sense of it

The intended pairing is matting → pixelate. Wire a Lucida/FeyNobg alpha into the optional mask input and it downsamples the alpha on the same grid and binarizes it to a hard edge - which is exactly what a sprite's transparency needs. If you're pixelating a cutout, always feed the mask; a soft matte edge does not survive palette quantization gracefully.

CategoryRui-Node🐶/图像调节🎨

Inputs (13)

NameTypeDefaultDescription
imageIMAGE待像素化的图像。批量输入时若各图比例不同, 会统一到首张的输出尺寸(ComfyUI 的 IMAGE 必须同形状)。
modeCOMBO按目标宽度按目标宽度:普通图转像素画,直接给输出宽度 按像素块大小:每 N×N 原像素合成一个像素 自动检测网格:探测图中隐含的像素网格并还原, 专治 AI 生成的模糊伪像素图(网格歪、带抗锯齿)
target_widthINT648–2048仅「按目标宽度」模式生效。高度按原图比例自动计算。 常见 sprite 尺寸:16 / 32 / 48 / 64 / 96 / 128
pixel_sizeINT81–256仅「按像素块大小」模式生效:每 N×N 原像素 → 1 像素。
downsampleCOMBO主导色 dominant(像素画首选)每个单元如何定色。 主导色:取块内出现最多的颜色,不会凭空造出新颜色(首选) 中位数:抗噪,偶尔比主导色更稳 均值:会产生新颜色并糊边,只在想要柔和过渡时用 中心像素:等价最近邻,最锐利但受噪点影响
paletteCOMBO自适应 k-means(质量优先)颜色数受控是像素画风格的一部分,也方便整套素材统一改色。 自适应:从画面自身聚类出调色板(k-means 在 CIELAB 空间, 比 RGB 更贴合人眼,暗部层次保留更好) 固定盘:PICO-8 / Game Boy 等复古机型的真实调色板
palette_sizeINT162–256仅自适应调色板生效。选固定调色板时其颜色数已定,此项忽略。 参考:8~16 复古感强,32~64 细节保留更多。
ditherCOMBO无(默认)颜色数很少时用抖动能换回一些层次,代价是引入噪点。 Bayer:规则网点,复古感强、可平铺,像素画最常用 Floyd-Steinberg:过渡最自然,但纹理不规则、不利于后期手改 手绘风像素画通常不抖动,先试「无」。
output_scaleINT11–32输出放大倍数(最近邻,不插值)。 1 = 真实像素尺寸,直接可用作 sprite(推荐) >1 仅为了在 ComfyUI 里看清效果,导出素材前记得改回 1
maskoptMASK可选。抠图得到的 alpha 接进来会按同一网格降采样, 并按 mask_threshold 二值化成硬边 —— sprite 需要硬边 alpha。
dither_strengthoptFLOAT1.000–3抖动幅度。已按调色板的平均色距归一化, 所以同一数值在 4 色盘和 64 色盘上观感接近。
mask_thresholdoptFLOAT0.500–1遮罩二值化阈值,高于它算不透明。 设为 0 则保留灰度遮罩(不二值化)。
seedoptINT00–4294967295随机种子。影响 k-means 调色板的初始化与随机噪声抖动; 同一张图换种子会得到略有差异的配色,可多试几个挑顺眼的。 固定调色板与 Bayer 抖动不受它影响。

Outputs (3)

NameTypeDescription
imageIMAGE
maskMASK
infoSTRING