Building an AI-Powered Product Image Generation and Validation Workflow
The Challenge: Scaling Product Visualization for Thousands of Fabrics
At KOVI Fabrics, we face a unique challenge: how do you create consistent, high-quality product mockups for over 7,000 fabric patterns? Traditional product photography would be prohibitively expensive and time-consuming. Enter AI image generation – but as we discovered, getting it right requires more than just prompting an AI model.
The Initial Approach: AI-Generated Pillow Mockups
We started with a straightforward goal: apply fabric patterns to pillow templates using AI. Using Replicate's google/nano-banana model, we built a system that:
- Fetches product data from our MySQL database
- Retrieves fabric images from AWS S3
- Generates pillow mockups using AI
- Saves optimized images for web use
The Technical Stack
// Core configuration
const config = {
replicate: {
model: 'google/nano-banana',
pillowTemplateImage: 'https://path-to-pillow-template.png',
prompt: 'Apply the given fabric pattern to the pillow...'
},
output: {
format: 'jpeg',
quality: 85
}
};
The Problem: Pattern Scaling Accuracy
While the AI generated beautiful images, we quickly discovered a critical issue: pattern scaling was often 2-3x larger than reality. A fabric with 2-inch stripes would appear with 4-6 inch stripes on the pillow. This misrepresentation could mislead customers about the actual appearance of the fabric.
Understanding the Math
For an 18" × 18" pillow:
- 2" stripes should show ~9 repeats across the width
- 0.5" diamonds should show ~36 repeats
- 7" damask patterns should show ~2-3 complete motifs
But the AI was consistently producing fewer, larger repeats.
The Solution: Multi-Stage Validation and Refinement
Our AI-powered product image generation system follows a systematic approach with multiple validation and refinement stages:
flowchart TD
A[Database Query: Fetch Product Data] --> B[Retrieve Fabric Image from S3]
B --> C[Initial AI Generation]
C --> D{Pattern Scaling Check}
D -->|Scaling Accurate| E[Optimize to JPEG]
D -->|Scaling Issues| F[Refinement Process]
F --> G[Upload Current Image to Replicate]
G --> H[Apply Correction Prompts]
H --> I[Generate Refined Image]
I --> J{Validation Check}
J -->|Still Issues| F
J -->|Acceptable| E
E --> K[Save to Storage]
K --> L[Update Database Status]
M[Analyze Command] --> N[Calculate Expected Repeats]
N --> O[Check Pattern Type]
O --> P[Generate Analysis Report]
P --> Q{Issues Found?}
Q -->|Yes| R[Recommend Refinement]
Q -->|No| S[Mark as Complete]
style A fill:#e1f5fe
style C fill:#fff3e0
style F fill:#fce4ec
style E fill:#e8f5e8
style M fill:#f3e5f5
Stage 1: Measurement-Based Prompt Engineering
We first tried to solve this through more precise prompts:
// Calculate expected repeats based on actual measurements
const repeatSize = parseFloat(product.repeats);
const idealRepeats = Math.round(18 / repeatSize);
const scaleGuidance = `CRITICAL: The pattern is TOO BIG.
Make it MUCH smaller. Each repeat is ONLY ${repeatSize}" wide.
You MUST fit EXACTLY ${idealRepeats} complete repeats across
the 18" pillow.`;
Using emphatic language ("CRITICAL", "MUST", "EXACTLY") improved results, but wasn't consistent enough.
Stage 2: Post-Processing Refinement
When prompt engineering alone proved insufficient, we implemented a refinement system:
async refineImage(imagePath, product) {
// Upload existing image to Replicate
const imageUrl = await this.uploadImageToReplicate(imagePath);
// Apply correction with specific scaling instructions
const output = await this.replicate.run(model, {
input: {
prompt: refinementPrompt,
image_input: [imageUrl],
output_format: 'jpg'
}
});
return output;
}
This two-pass approach allowed us to:
- Generate an initial image
- Identify scaling issues
- Apply targeted corrections
Stage 3: Systematic Validation Workflow
The breakthrough came when we built a systematic validation system:
# Analyze any pillow image
node ai_image_gen.js analyze 1071
# Output:
Fabric Analysis for 1071:
==================================================
Pattern: K2182
Name: LINEN STRIPE
Pattern Category: Stripe
Repeat Size: Horizontal: 2"
Pattern Analysis:
- Repeat size: 2"
- Expected repeats on 18" pillow: ~9
- Pattern type: Stripe
- Pillow image exists: 1071_Pillow.jpg
To refine this image: node ai_image_gen.js refine K2182
This command:
- Queries the database for fabric specifications
- Calculates expected pattern repeats
- Identifies solid vs. patterned fabrics
- Provides actionable next steps
Key Learnings
1. AI Models Need Concrete Measurements
Vague instructions like "make the pattern smaller" don't work. Specific measurements ("fit exactly 9 repeats in 18 inches") produce better results.
2. Two-Pass Generation Often Beats Single-Pass
Rather than trying to get perfect results in one shot, a generate-then-refine approach proved more reliable and efficient.
3. Systematic Validation is Essential at Scale
With thousands of products, manual checking isn't feasible. Building automated validation that can identify issues programmatically was crucial.
4. File Format Optimization Matters
We reduced file sizes by 96% (1.5MB PNG → 80KB JPEG) without noticeable quality loss, significantly improving page load times.
The Results
By the Numbers
- 7,300+ products requiring images
- 96% file size reduction (PNG to JPEG)
- 15 patterns successfully refined with scaling corrections
- ~80KB average file size per image
Patterns That Benefited Most from Refinement
- 0.5" diamonds: Corrected from ~25 to ~35 repeats
- 1.5" stripes: Corrected from ~9 to ~12 repeats
- 2" stripes: Fine-tuned to show exactly 9 repeats
Implementation Tips for Your Own Workflow
1. Start with Database-Driven Configuration
Store pattern measurements and specifications in your database. This enables programmatic validation and automated scaling calculations.
2. Build Incrementally
- Start with basic generation
- Add validation tooling
- Implement refinement only where needed
- Document everything
3. Use the Right Tools
- Replicate API: Excellent for batch processing
- Files API: Essential for image upload/refinement
- Node.js: Perfect for scripting and automation
- MySQL/Knex: Robust data management
4. Create Clear Command-Line Interfaces
node ai_image_gen.js generate 10 # Process 10 products
node ai_image_gen.js analyze 1071 # Check specific pattern
node ai_image_gen.js refine K2023 # Fix scaling issues
The Code Architecture
Our final system consists of:
- Database Layer: Product specifications and pattern data
- AI Generation Layer: Initial image creation with Replicate
- Validation Layer: Pattern analysis and scaling verification
- Refinement Layer: Post-processing corrections
- Storage Layer: Optimized JPEG output
What's Next?
This workflow has opened up several exciting possibilities:
- Automated quality scoring: ML-based assessment of generated images
- Multi-angle generation: Creating lifestyle shots beyond pillows
- Dynamic refinement: Learning from corrections to improve future generations
- Customer personalization: Generate mockups with custom colors/settings
Conclusion
Building an AI-powered image generation workflow taught us that success lies not just in the AI model, but in the systematic approach to validation and refinement. By combining precise measurement-based prompts, post-processing refinement, and automated validation, we've created a scalable system that maintains quality across thousands of products.
The key takeaway? AI is powerful, but it works best when augmented with systematic validation and domain-specific knowledge. The magic happens when you build the right workflow around the AI, not just rely on the AI alone.
Have you built similar AI-powered workflows? What challenges did you face with scaling and quality control? Share your experiences in the comments below!