| name | ai-image-effects |
| description | Apply AI visual effects including Illusion Diffusion ($0.006), FLUX Fill Pro accessory replacement ($0.05), and SAM object detection (<$0.01). Use when adding AI effects, replacing image elements, detecting objects, or applying visual transformations. |
| allowed-tools | Read, Write, Bash(python:*) |
| model | claude-sonnet-4-20250514 |
AI Image Effects
Professional AI-powered image effects using Replicate models - Illusion Diffusion, FLUX Fill Pro, and Meta SAM-2.
Overview
Three powerful AI effects for image transformation:
| Effect | Model | Cost | Use Case |
|---|
| Illusion Diffusion | ControlNet | $0.006/image | Visual illusions, patterns |
| FLUX Fill Pro | Black Forest Labs | $0.05/image | Smart inpainting, accessory replacement |
| SAM Detector | Meta SAM-2 | <$0.01/detection | Object detection, segmentation |
Quick Start
1. Configure Replicate API
export REPLICATE_API_TOKEN="r8_xxxxxxxxxxxxx"
python scripts/check_credit.py
2. Run Effects
from src.illusion_diffusion import IllusionDiffusion
illusion = IllusionDiffusion()
result = illusion.apply(
image_path="input.png",
prompt="spiral pattern",
strength=1.5
)
from src.flux_fill_pro import FluxFillPro
flux = FluxFillPro()
result = flux.replace_accessory(
image_path="milady.png",
accessory_type="hat",
new_description="red baseball cap"
)
from src.sam_detector import SAMDetector
sam = SAMDetector()
bbox = sam.detect_accessory(
image_path="milady.png",
accessory_type="glasses"
)
Effect 1: Illusion Diffusion
What It Does
Creates optical illusions and pattern effects using ControlNet. The AI reshapes the image while maintaining recognizability from a distance.
Use Cases
- Spiral patterns - Make image appear as spiral
- QR codes - Embed QR codes in artwork
- Hidden text - Text visible from afar
- Geometric patterns - Hexagons, triangles, waves
- Artistic effects - Creative distortions
Basic Usage
from src.illusion_diffusion import IllusionDiffusion
illusion = IllusionDiffusion()
result = illusion.apply(
image_path="milady.png",
prompt="spiral optical illusion"
)
Advanced Parameters
result = illusion.apply(
image_path="milady.png",
prompt="concentric circles optical illusion",
control_guidance=1.5,
controlnet_conditioning=1.2,
steps=50,
guidance_scale=7.5
)
Parameter Guide:
control_guidance: Higher = stronger illusion effect
controlnet_conditioning: Higher = more visible pattern
steps: More steps = higher quality (but slower)
guidance_scale: Higher = follows prompt more closely
Pattern Ideas
illusion.apply(image, "fibonacci spiral pattern")
illusion.apply(image, "hexagonal tessellation")
illusion.apply(image, "concentric wave pattern")
illusion.apply(image, "QR code pattern", control_guidance=2.0)
illusion.apply(image, "hidden text 'WAGMI' optical illusion")
Cost Optimization
result = illusion.apply(
image,
prompt="spiral",
steps=20,
control_guidance=1.0
)
results = illusion.batch_apply(
images=["img1.png", "img2.png", "img3.png"],
prompt="spiral pattern"
)
Effect 2: FLUX Fill Pro
What It Does
AI inpainting and accessory replacement. Intelligently fills/replaces parts of an image with new content matching the description.
Use Cases
- Replace accessories - Swap hats, glasses, earrings
- Change clothes - Modify shirts, dresses
- Add elements - Insert new objects
- Remove elements - Inpaint to remove objects
- Style transfer - Change artistic style of regions
Basic Usage
from src.flux_fill_pro import FluxFillPro
flux = FluxFillPro()
result = flux.replace_accessory(
image_path="milady.png",
accessory_type="hat",
new_description="red baseball cap"
)
Accessory Types
Supports 8 predefined accessory categories:
ACCESSORY_TYPES = {
"hat": (300, 180, 80),
"glasses": (420, 60, 50),
"earrings": (480, 80, 70),
"necklace": (640, 120, 60),
"scarf": (550, 150, 70),
"clothes": (700, 400, 90),
"background": (0, 1250, 100),
"other": None
}
Advanced Usage
result = flux.replace_region(
image_path="milady.png",
bbox=(100, 200, 300, 400),
prompt="cool futuristic visor"
)
result = flux.inpaint(
image_path="milady.png",
mask_path="mask.png",
prompt="blue sunglasses"
)
result = flux.batch_replace(
image_path="milady.png",
replacements=[
{"type": "hat", "description": "cowboy hat"},
{"type": "glasses", "description": "round sunglasses"}
]
)
Quality Parameters
result = flux.replace_accessory(
image_path="milady.png",
accessory_type="hat",
new_description="red beanie",
strength=0.8,
guidance=7.5,
steps=30,
seed=42
)
Best Practices
-
Be specific in descriptions
"hat"
"red baseball cap with white logo"
-
Use consistent lighting
"blue sunglasses with same lighting as original image"
-
Match art style
"anime-style pink beret matching the character's aesthetic"
Effect 3: SAM Detector
What It Does
Meta's Segment Anything Model (SAM-2) for precise object detection and bounding box extraction. Used to automatically find accessories before replacement.
Use Cases
- Auto-detect accessories - Find hats, glasses automatically
- Precise segmentation - Get exact object boundaries
- Multi-object detection - Detect all objects in image
- Smart cropping - Crop to detected objects
- Integration with FLUX - Auto-detect then replace
Basic Usage
from src.sam_detector import SAMDetector
sam = SAMDetector()
bbox = sam.detect_accessory(
image_path="milady.png",
accessory_type="hat"
)
Supported Accessories
ACCESSORY_CATEGORIES = {
"hat": ["帽子", "hat", "cap", "beanie", "beret"],
"glasses": ["眼镜", "glasses", "sunglasses", "spectacles"],
"earrings": ["耳环", "earring", "earrings"],
"necklace": ["项链", "necklace", "chain"],
"scarf": ["围巾", "scarf", "bandana"],
"clothes": ["衣服", "shirt", "dress", "top"],
"face_decoration": ["面部装饰", "sticker", "mark"],
"overlay": ["特效", "effect", "overlay"]
}
Smart Detection
result = sam.smart_detect(
image_path="milady.png",
accessory_type="glasses",
use_position_hints=True
)
bbox, confidence = sam.detect_with_confidence(
image_path="milady.png",
accessory_type="hat",
min_confidence=0.7
)
Caching System
SAM results are cached for 7 days to save costs:
bbox1 = sam.detect_accessory("milady.png", "hat")
bbox2 = sam.detect_accessory("milady.png", "hat")
sam.clear_cache()
stats = sam.get_cache_stats()
Integration with FLUX
from src.sam_detector import SAMDetector
from src.flux_fill_pro import FluxFillPro
sam = SAMDetector()
flux = FluxFillPro()
bbox = sam.detect_accessory("milady.png", "hat")
if bbox:
result = flux.replace_region(
image_path="milady.png",
bbox=bbox,
prompt="red baseball cap"
)
Advanced Features
all_objects = sam.detect_all_objects("milady.png")
mask = sam.get_segmentation_mask("milady.png", "glasses")
bboxes = sam.batch_detect(
images=["img1.png", "img2.png"],
accessory_type="hat"
)
Complete Workflow Example
Example 1: Smart Accessory Replacement
from src.sam_detector import SAMDetector
from src.flux_fill_pro import FluxFillPro
sam = SAMDetector()
flux = FluxFillPro()
def smart_replace(image_path, accessory_type, new_description):
"""Detect accessory with SAM, replace with FLUX"""
print(f"Detecting {accessory_type}...")
bbox = sam.detect_accessory(image_path, accessory_type)
if not bbox:
print(f"No {accessory_type} detected, using default position")
return flux.replace_accessory(
image_path, accessory_type, new_description
)
print(f"Replacing {accessory_type} with: {new_description}")
result = flux.replace_region(
image_path=image_path,
bbox=bbox,
prompt=new_description
)
return result
result = smart_replace(
"milady_5050.png",
"glasses",
"cool futuristic visor with neon blue glow"
)
result.save("output.png")
Example 2: Multi-Effect Pipeline
from src.illusion_diffusion import IllusionDiffusion
from src.flux_fill_pro import FluxFillPro
illusion = IllusionDiffusion()
flux = FluxFillPro()
print("Replacing hat...")
img = flux.replace_accessory(
"milady.png",
"hat",
"red baseball cap"
)
img.save("step1.png")
print("Applying spiral effect...")
final = illusion.apply(
"step1.png",
prompt="spiral optical illusion"
)
final.save("final.png")
print("Total cost: $0.05 + $0.006 = $0.056")
Example 3: Batch Processing
from src.flux_fill_pro import FluxFillPro
flux = FluxFillPro()
images = ["milady_1.png", "milady_2.png", "milady_3.png"]
accessories = ["hat", "glasses", "hat"]
descriptions = [
"cowboy hat",
"heart-shaped sunglasses",
"blue beanie"
]
results = []
for img, acc, desc in zip(images, accessories, descriptions):
print(f"Processing {img}...")
result = flux.replace_accessory(img, acc, desc)
results.append(result)
Configuration
Replicate Config
Located in: config/replicate_config.py
MODELS = {
"illusion": "monster-labs/control_v1p_sd15_qrcode_monster:...",
"flux_fill": "black-forest-labs/flux-fill-pro",
"sam": "meta/sam-2:..."
}
ILLUSION_DEFAULTS = {
"control_guidance": 1.5,
"steps": 50
}
FLUX_DEFAULTS = {
"strength": 0.8,
"guidance": 7.5,
"steps": 30
}
Cost Management
python scripts/check_credit.py
Rate Limiting
flux = FluxFillPro(max_concurrent=3)
Cost Calculator
Estimate Costs
from src.cost_calculator import CostCalculator
calc = CostCalculator()
cost = calc.estimate(
effect="flux_fill",
count=1
)
cost = calc.estimate_batch([
{"effect": "illusion", "count": 10},
{"effect": "flux_fill", "count": 5},
{"effect": "sam", "count": 20}
])
Monthly Budget Planning
monthly_plan = {
"illusion": 100,
"flux_fill": 50,
"sam": 200
}
cost = calc.monthly_estimate(monthly_plan)
Error Handling
Common Issues
1. Insufficient credits
try:
result = flux.replace_accessory(...)
except InsufficientCreditsError:
print("Please add credits to Replicate account")
2. Model timeout
flux = FluxFillPro(timeout=300)
3. Invalid bounding box
bbox = sam.detect_accessory(image, "hat")
if bbox and flux.validate_bbox(bbox):
result = flux.replace_region(image, bbox, prompt)
Best Practices
- Use SAM before FLUX - Auto-detection is more accurate than hardcoded positions
- Cache SAM results - Saves 50-70% on detection costs
- Batch when possible - Process multiple images in one session
- Monitor spending - Check balance regularly
- Test with low steps - Use steps=20 for testing, steps=50 for production
- Be specific with prompts - Better descriptions = better results
Advanced Tips
Combine Multiple Effects
def create_custom_illusion(image_path):
img = flux.replace_accessory(image_path, "hat", "cool hat")
final = illusion.apply(img, "spiral pattern")
return final
Quality vs Cost Trade-off
result = flux.replace_accessory(
image, "hat", "red cap",
steps=50,
guidance=10.0
)
result = flux.replace_accessory(
image, "hat", "red cap",
steps=20,
guidance=7.0
)
Related Documentation
Related Skills
Total Costs Summary:
- Illusion Diffusion: $0.006 per image
- FLUX Fill Pro: $0.05 per image
- SAM Detection: <$0.01 per detection
- Combined (SAM + FLUX): ~$0.05 per image