File size: 10,936 Bytes
338d95d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
"""
Image processing utilities for CompI Phase 2.E: Style Reference/Example Image Integration

This module provides utilities for:
- Image loading from files and URLs
- Image validation and preprocessing
- Style analysis and feature extraction
- Image format conversion and optimization
"""

import os
import io
import requests
import hashlib
from typing import Optional, Tuple, Dict, Any, Union, List
from pathlib import Path
import logging

import torch
import numpy as np
from PIL import Image, ImageStat, ImageFilter
import cv2

from src.utils.logging_utils import setup_logger

logger = setup_logger(__name__)

class ImageProcessor:
    """
    Handles image loading, validation, and preprocessing for style reference
    """
    
    def __init__(self, max_size: Tuple[int, int] = (1024, 1024)):
        self.max_size = max_size
        self.supported_formats = {'.jpg', '.jpeg', '.png', '.bmp', '.tiff', '.webp'}
        
    def load_image_from_url(
        self, 
        url: str, 
        timeout: int = 10,
        max_file_size: int = 10 * 1024 * 1024  # 10MB
    ) -> Optional[Image.Image]:
        """
        Load image from URL with validation and error handling
        
        Args:
            url: Image URL
            timeout: Request timeout in seconds
            max_file_size: Maximum file size in bytes
            
        Returns:
            PIL Image or None if failed
        """
        try:
            logger.info(f"Loading image from URL: {url}")
            
            # Validate URL format
            if not url.startswith(('http://', 'https://')):
                logger.error(f"Invalid URL format: {url}")
                return None
                
            # Make request with headers to avoid blocking
            headers = {
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
            }
            
            response = requests.get(url, timeout=timeout, headers=headers, stream=True)
            response.raise_for_status()
            
            # Check content type
            content_type = response.headers.get('content-type', '').lower()
            if not any(img_type in content_type for img_type in ['image/', 'jpeg', 'png', 'webp']):
                logger.error(f"Invalid content type: {content_type}")
                return None
                
            # Check file size
            content_length = response.headers.get('content-length')
            if content_length and int(content_length) > max_file_size:
                logger.error(f"File too large: {content_length} bytes")
                return None
                
            # Load image data
            image_data = io.BytesIO()
            downloaded_size = 0
            
            for chunk in response.iter_content(chunk_size=8192):
                downloaded_size += len(chunk)
                if downloaded_size > max_file_size:
                    logger.error(f"File too large during download: {downloaded_size} bytes")
                    return None
                image_data.write(chunk)
                
            image_data.seek(0)
            
            # Open and validate image
            image = Image.open(image_data)
            image = image.convert('RGB')
            
            logger.info(f"Successfully loaded image: {image.size}")
            return image
            
        except requests.exceptions.RequestException as e:
            logger.error(f"Request error loading image from {url}: {e}")
            return None
        except Exception as e:
            logger.error(f"Error loading image from {url}: {e}")
            return None
            
    def load_image_from_file(self, file_path: Union[str, Path]) -> Optional[Image.Image]:
        """
        Load image from local file with validation
        
        Args:
            file_path: Path to image file
            
        Returns:
            PIL Image or None if failed
        """
        try:
            file_path = Path(file_path)
            
            if not file_path.exists():
                logger.error(f"File does not exist: {file_path}")
                return None
                
            if file_path.suffix.lower() not in self.supported_formats:
                logger.error(f"Unsupported format: {file_path.suffix}")
                return None
                
            image = Image.open(file_path)
            image = image.convert('RGB')
            
            logger.info(f"Successfully loaded image from file: {image.size}")
            return image
            
        except Exception as e:
            logger.error(f"Error loading image from {file_path}: {e}")
            return None
            
    def preprocess_image(
        self, 
        image: Image.Image, 
        target_size: Optional[Tuple[int, int]] = None,
        maintain_aspect_ratio: bool = True
    ) -> Image.Image:
        """
        Preprocess image for stable diffusion
        
        Args:
            image: Input PIL Image
            target_size: Target size (width, height)
            maintain_aspect_ratio: Whether to maintain aspect ratio
            
        Returns:
            Preprocessed PIL Image
        """
        if target_size is None:
            target_size = (512, 512)  # Default SD size
            
        try:
            # Resize image
            if maintain_aspect_ratio:
                image.thumbnail(target_size, Image.Resampling.LANCZOS)
                
                # Create new image with target size and paste resized image
                new_image = Image.new('RGB', target_size, (255, 255, 255))
                paste_x = (target_size[0] - image.width) // 2
                paste_y = (target_size[1] - image.height) // 2
                new_image.paste(image, (paste_x, paste_y))
                image = new_image
            else:
                image = image.resize(target_size, Image.Resampling.LANCZOS)
                
            logger.info(f"Preprocessed image to size: {image.size}")
            return image
            
        except Exception as e:
            logger.error(f"Error preprocessing image: {e}")
            return image
            
    def analyze_image_properties(self, image: Image.Image) -> Dict[str, Any]:
        """
        Analyze image properties for style reference
        
        Args:
            image: PIL Image to analyze
            
        Returns:
            Dictionary of image properties
        """
        try:
            # Basic properties
            width, height = image.size
            aspect_ratio = width / height
            
            # Color analysis
            stat = ImageStat.Stat(image)
            avg_brightness = sum(stat.mean) / len(stat.mean)
            avg_contrast = sum(stat.stddev) / len(stat.stddev)
            
            # Convert to numpy for additional analysis
            img_array = np.array(image)
            
            # Color distribution
            r_mean, g_mean, b_mean = np.mean(img_array, axis=(0, 1))
            color_variance = np.var(img_array, axis=(0, 1))
            
            # Edge detection for complexity
            gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
            edges = cv2.Canny(gray, 50, 150)
            edge_density = np.sum(edges > 0) / (width * height)
            
            properties = {
                'dimensions': (width, height),
                'aspect_ratio': aspect_ratio,
                'brightness': avg_brightness,
                'contrast': avg_contrast,
                'color_means': (float(r_mean), float(g_mean), float(b_mean)),
                'color_variance': color_variance.tolist(),
                'edge_density': float(edge_density),
                'file_size_pixels': width * height
            }
            
            logger.info(f"Analyzed image properties: {properties}")
            return properties
            
        except Exception as e:
            logger.error(f"Error analyzing image properties: {e}")
            return {}
            
    def generate_image_hash(self, image: Image.Image) -> str:
        """
        Generate hash for image deduplication
        
        Args:
            image: PIL Image
            
        Returns:
            MD5 hash string
        """
        try:
            # Convert image to bytes
            img_bytes = io.BytesIO()
            image.save(img_bytes, format='PNG')
            img_bytes = img_bytes.getvalue()
            
            # Generate hash
            hash_md5 = hashlib.md5(img_bytes)
            return hash_md5.hexdigest()
            
        except Exception as e:
            logger.error(f"Error generating image hash: {e}")
            return ""

class StyleAnalyzer:
    """
    Analyzes style characteristics of reference images
    """
    
    def __init__(self):
        self.style_keywords = {
            'realistic': ['photo', 'realistic', 'detailed', 'sharp'],
            'artistic': ['painting', 'artistic', 'brushstrokes', 'canvas'],
            'anime': ['anime', 'manga', 'cartoon', 'stylized'],
            'abstract': ['abstract', 'geometric', 'surreal', 'conceptual'],
            'vintage': ['vintage', 'retro', 'aged', 'classic'],
            'modern': ['modern', 'contemporary', 'clean', 'minimal']
        }
        
    def suggest_style_keywords(self, image_properties: Dict[str, Any]) -> List[str]:
        """
        Suggest style keywords based on image analysis
        
        Args:
            image_properties: Properties from analyze_image_properties
            
        Returns:
            List of suggested style keywords
        """
        suggestions = []
        
        try:
            brightness = image_properties.get('brightness', 128)
            contrast = image_properties.get('contrast', 50)
            edge_density = image_properties.get('edge_density', 0.1)
            
            # Brightness-based suggestions
            if brightness < 100:
                suggestions.extend(['dark', 'moody', 'dramatic'])
            elif brightness > 180:
                suggestions.extend(['bright', 'light', 'airy'])
                
            # Contrast-based suggestions
            if contrast > 80:
                suggestions.extend(['high contrast', 'bold', 'striking'])
            elif contrast < 30:
                suggestions.extend(['soft', 'gentle', 'muted'])
                
            # Edge density-based suggestions
            if edge_density > 0.2:
                suggestions.extend(['detailed', 'complex', 'intricate'])
            elif edge_density < 0.05:
                suggestions.extend(['smooth', 'simple', 'minimalist'])
                
            return list(set(suggestions))  # Remove duplicates
            
        except Exception as e:
            logger.error(f"Error suggesting style keywords: {e}")
            return []