Nodes/h4_Live/h4 // Image Compressor
ComfyUI Node

h4 // Image Compressor

Squeeze images to JPEG, PNG, or WebP bytes without leaving the graph

By m3rr·Created 9 months ago·Updated 10 days ago· 1
h4 // Image Compressor
  • image
  • show_preview
  • preview_only
  • save_mode
  • IMAGE
  • BYTES
  • STRING
quality85
formatJPEG
save_path
background_color255,255,255

H4_ImageCompressor is the "shrink this image" node for when you want to ship a result somewhere that doesn't love a 12MB PNG. It takes an image in, re-encodes it to JPEG, PNG, or WebP at a quality you pick, and hands you back the compressed image and the raw bytes and a string (the saved path or metadata). Save mode is on by default, so it can write the compressed file to disk for you - which makes it the natural terminal node for "generate a preview, compress it, push it somewhere" pipelines.

Under the hood it's straightforward Pillow work: _save_image_to_bytes encodes with the format's quality settings (JPEG quality, WebP method 6, PNG with compress_level 9), a background_color flattens alpha onto a solid color when you go to JPEG (which has no alpha channel), and the whole thing runs through the pack's preview/temp-asset system so you can see what you're compressing before you save. Nothing exotic - and that's the appeal: it's a boring, reliable re-encoder with a bytes output, which is rarer in ComfyUI than it should be.

The settings that matter

  • image - the input image.
  • quality - 1–100, default 85. Standard JPEG thinking: 85 is a great starting point, 70 for smaller files, 95+ for near-lossless.
  • format - JPEG, PNG, or WebP. JPEG is the smallest for photos; PNG for anything needing lossless or transparency; WebP a good middle ground (the code even renders previews as WebP).
  • show_preview / preview_only - look before you save; preview_only skips writing to disk entirely.
  • save_mode / save_path - write the compressed file out (default on; save_path empty = the pack's default output location). If you just want bytes, turn save off.
  • background_color - 255,255,255 by default. This is what alpha pixels get flattened to in formats without transparency, so if your WebP/PNG with transparency looks wrong after a JPEG conversion, this is why.

The outputs

  • IMAGE - the compressed image tensor (for display or further use).
  • BYTES - the encoded file bytes. This is the one you want if you're feeding an HTTP request, a cloud upload, or any node that accepts raw bytes.
  • STRING - the saved path (or metadata string), for logging or feeding a filename downstream.

Installing

ComfyUI Manager → search "h4_Live", or:

cd ComfyUI/custom_nodes
git clone https://github.com/m3rr/h4_Live

restart. Needs Pillow, which every ComfyUI install already has.

Where people get burned

Two easy trips. First, going JPEG on an image with transparency: pick background_color deliberately or your transparent areas come back as white blocks. Second, forgetting save_mode is on by default - run a batch through it and your output folder quietly fills with compressed files you didn't ask for. And one honest note: quality 85 to 100 on a photorealistic image is usually invisible; the filesize difference is where you feel it. If you're compressing to post online, start at 85, and don't let anyone sell you on "quality 100 or it's trash."

Categoryh4

Inputs (8)

NameTypeDefaultDescription
imageIMAGE
qualityoptINT851–100
formatoptSTRINGJPEG
show_previewoptBOOLtrue
preview_onlyoptBOOLfalse
save_modeoptBOOLtrue
save_pathoptSTRING
background_coloroptSTRING255,255,255

Outputs (3)

NameTypeDescription
IMAGEIMAGE
BYTESBYTES
STRINGSTRING