A ComfyUI custom node that preserves image quality by selectively merging only the changed regions from AI-generated edits back into the original image. Perfect for use with Flux Kontext Dev to prevent quality degradation in unchanged areas.
- Adaptive Detection: Robust to global AI processing changes using LAB color space
- Area Filtering: Removes small noise areas that aren't real changes
- Multiple Detection Methods: Adaptive, color difference, SSIM, and combined approaches
- Multiple Blending Methods: Poisson, alpha, multi-band, and Gaussian blending
- Intelligent Masking: Automatic mask refinement with morphological operations
- Preview Capability: Shows detected changes with visual overlay
- Batch Processing: Processes full batches; broadcasts a single original over a multi-image edited batch
- Robust Poisson: Stable center selection and safe padding when masks touch image edges
cd ComfyUI/custom_nodes/git clone https://github.com/safzanpirani/flux-kontext-diff-merge.git- Launch ComfyUI.
- Create folder:
ComfyUI/custom_nodes/flux-kontext-diff-merge - Copy all files into this folder
- Restart ComfyUI
- Load your original image (before AI editing)
- Process with Flux Kontext Dev to get your edited image
- Add the Flux Kontext Diff Merge node
- Connect inputs:
original_image: Your original unedited imageedited_image: The AI-edited output from Flux Kontext Dev
- Use recommended settings (see below)
original_image(IMAGE): Connect from your FluxKontextImageScale nodeedited_image(IMAGE): Connect from your VAEDecode node
threshold(0.01-1.0, default: 0.02): Controls change detection threshold (increasing this reduces the size of the detected mask)detection_method: Choose detection approachadaptive: Best for AI-processed images (recommended)color_diff: Simple color differencesssim: Structural similaritycombined: Multiple detection methods
global_threshold(0.01-0.5, default: 0.15): Accounts for overall AI processing changesmin_change_area(0-5000, default: 250): Minimum area size for valid changesblend_method: Choose blending techniquepoisson: Best for seamless integration (recommended)alpha: Simple linear blendingmultiband: Multi-scale blending for complex changesgaussian: Smooth falloff blending
mask_blur(1-100, default: 15): Blur amount for mask edgesmask_expand(0-50, default: 8): Pixels to expand the change maskedge_feather(0-50, default: 15): Additional edge softening
manual_mask: Override automatic detection with your own mask
merged_image: The final quality-preserved imagedifference_mask: The detected change maskpreview_diff: Preview showing detected changes highlighted in red
This node fully supports batch workflows:
- If
original_imageandedited_imagehave the same batch size, items are processed pairwise - If
original_imagehas batch size 1 andedited_image> 1, the original is automatically broadcast to match the edited batch manual_maskmay be a single mask (broadcast) or a batch of masks; masks are resized per item to match image dimensions- All outputs (
merged_image,difference_mask,preview_diff) are returned as batches
threshold: 0.02
detection_method: "adaptive"
global_threshold: 0.15
min_change_area: 250
blend_method: "poisson"
mask_blur: 15
mask_expand: 8
edge_feather: 15
- Poisson now selects the blend center using a distance transform (most interior point) for stability across thresholds
- If the mask touches the image border, the node performs reflect-padding under the hood and crops back, avoiding ROI errors and position jumps
- Poisson internally treats masks as binary; for very soft edges, consider
multibandorgaussian
When Flux Kontext Dev processes an image, it introduces subtle changes throughout the entire image. This node:
- ✅ Preserves original image quality in unchanged areas
- ✅ Only applies AI changes where they're actually needed
- ✅ Handles global AI processing artifacts automatically
- ✅ Provides seamless blending between original and edited regions
- ComfyUI
- Python 3.8+
- OpenCV (opencv-python)
- scikit-image
- NumPy
- SciPy
- Pillow
MIT License - feel free to use and modify for your projects.
- Increase
global_thresholdto 0.20+ - Increase
thresholdto 0.03 - Use
detection_method: "adaptive" - Increase
min_change_areato 500+
- Decrease
global_thresholdto 0.08 - Decrease
thresholdto 0.01 - Decrease
min_change_areato 100 - Check the
preview_diffoutput for debugging
- If you pass a single original and a batched edited input, the node will broadcast the original automatically
- If you pass mismatched batch sizes otherwise, you will get an error; align batch sizes or use broadcasting as above
- Batch processing with original-image broadcasting when edited batch > 1
- Poisson blending is robust to edge-touching masks (reflect padding) and uses distance-transform center selection
- Per-parameter tooltips and improved in-node documentation for easier tuning


