File size: 9,339 Bytes
863cb78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import base64
from io import BytesIO
from PIL import Image, ImageChops
from PIL import ImageDraw
import math

class ImageUtils:
    def __init__(self):
        pass

    @staticmethod
    def crop_base64(base64_string, output_format='PNG') -> str:
        """

        Takes a base64 encoded image, crops it by removing uniform background,

        and returns the cropped image as base64.

        

        Args:

            base64_string (str or bytes): Base64 encoded image string or raw bytes

            output_format (str): Output image format ('PNG', 'JPEG', etc.)

        

        Returns:

            str: Base64 encoded cropped image, or empty string if cropping fails

        """
        try:
            # Handle both base64 strings and raw bytes
            if isinstance(base64_string, bytes):
                # If it's raw bytes, treat it as image data directly
                image_data = base64_string
            else:
                # If it's a string, decode base64 to image
                image_data = base64.b64decode(base64_string)
            
            im = Image.open(BytesIO(image_data))
            
            # Apply the original trim logic
            bg = Image.new(im.mode, im.size, im.getpixel((0,0)))
            diff = ImageChops.difference(im, bg)
            diff = ImageChops.add(diff, diff, 2.0, -100)
            bbox = diff.getbbox()
            
            if bbox:
                cropped_im = im.crop(bbox)
            else:
                cropped_im = im  # Return original if no cropping needed
            
            # Convert back to base64
            buffer = BytesIO()
            cropped_im.save(buffer, format=output_format)
            cropped_base64 = base64.b64encode(buffer.getvalue()).decode('utf-8')
            
            return cropped_base64
            
        except Exception as e:
            print(f"Error processing image: {e}")
            return ""

    @staticmethod
    def crop_image(im: Image.Image) -> Image.Image:
        """

        Original trim function for PIL Image objects

        """
        try:
            bg = Image.new(im.mode, im.size, im.getpixel((0,0)))
            diff = ImageChops.difference(im, bg)
            diff = ImageChops.add(diff, diff, 2.0, -100)
            bbox = diff.getbbox()
            if bbox:
                return im.crop(bbox)
            return im
        except Exception as e:
            print(f"Error cropping image: {e}")
            return im

    @staticmethod
    def draw_bounding_boxes(pil_image: Image.Image, boxes: list[tuple[int, int, int, int]], color: str = "red", width: int = 2) -> Image.Image:
        """

        Draw bounding boxes on a PIL image.

        

        Args:

            pil_image: A PIL.Image instance.

            boxes: A list of boxes, each specified as (x1, y1, x2, y2).

            color: The color for the bounding box outline.

            width: The width of the bounding box line.

            

        Returns:

            The PIL.Image with drawn bounding boxes.

        """
        try:
            draw = ImageDraw.Draw(pil_image)
            for box in boxes:
                draw.rectangle(box, outline=color, width=width)
            return pil_image
        except Exception as e:
            print(f"Error drawing bounding boxes: {e}")
            return pil_image

    @staticmethod
    def standardize_image_size(image: Image.Image, target_size: tuple = (1200, 1600), maintain_aspect_ratio: bool = True) -> Image.Image:
        """

        Resize image to target size while optionally maintaining aspect ratio.

        

        Args:

            image: PIL Image to resize

            target_size: Target (width, height) in pixels

            maintain_aspect_ratio: If True, fit within target size while maintaining aspect ratio

            

        Returns:

            Resized PIL Image

        """
        if maintain_aspect_ratio:
            # Calculate aspect ratios
            img_ratio = image.width / image.height
            target_ratio = target_size[0] / target_size[1]
            
            if img_ratio > target_ratio:
                # Image is wider than target, fit to width
                new_width = target_size[0]
                new_height = int(target_size[0] / img_ratio)
            else:
                # Image is taller than target, fit to height
                new_height = target_size[1]
                new_width = int(target_size[1] * img_ratio)
            
            # Resize image
            resized_image = image.resize((new_width, new_height), Image.Resampling.LANCZOS)
            
            # Create new image with target size and white background
            final_image = Image.new('RGB', target_size, 'white')
            
            # Calculate position to center the resized image
            x_offset = (target_size[0] - new_width) // 2
            y_offset = (target_size[1] - new_height) // 2
            
            # Paste the resized image onto the white background
            final_image.paste(resized_image, (x_offset, y_offset))
            
            return final_image
        else:
            # Direct resize to target size
            return image.resize(target_size, Image.Resampling.LANCZOS)

    @staticmethod
    def optimize_image_quality(image: Image.Image, max_size_bytes: int = 1024 * 1024, initial_quality: int = 95) -> tuple[Image.Image, int]:
        """

        Optimize image quality to fit within specified file size limit.

        

        Args:

            image: PIL Image to optimize

            max_size_bytes: Maximum file size in bytes (default 1MB)

            initial_quality: Starting quality (1-100) - not used for PNG but kept for compatibility

            

        Returns:

            Tuple of (optimized_image, final_quality)

        """
        # For PNG, we'll use compression levels instead of quality
        # PNG compression levels range from 0 (no compression) to 9 (maximum compression)
        compression_levels = [0, 1, 3, 5, 7, 9]  # Try different compression levels
        
        for compression in compression_levels:
            # Save image to buffer with current compression
            buffer = BytesIO()
            image.save(buffer, format='PNG', optimize=True, compress_level=compression)
            current_size = buffer.tell()
            
            # If size is within limit, return the image
            if current_size <= max_size_bytes:
                # Reset buffer position and load the optimized image
                buffer.seek(0)
                optimized_image = Image.open(buffer)
                return optimized_image, 95  # Return a default quality value for compatibility
        
        # If we can't get under the size limit, return the most compressed version
        buffer = BytesIO()
        image.save(buffer, format='PNG', optimize=True, compress_level=9)
        buffer.seek(0)
        optimized_image = Image.open(buffer)
        return optimized_image, 50  # Return a lower quality value for compatibility

    @staticmethod
    def process_image_for_comparison(image: Image.Image, target_size: tuple = (1200, 1600), max_size_bytes: int = 1024 * 1024) -> tuple[Image.Image, int, int]:
        """

        Process image for comparison: standardize size and optimize quality.

        

        Args:

            image: PIL Image to process

            target_size: Target size in pixels (width, height)

            max_size_bytes: Maximum file size in bytes (default 1MB)

            

        Returns:

            Tuple of (processed_image, final_quality, file_size_bytes)

        """
        # First, standardize the size
        sized_image = ImageUtils.standardize_image_size(image, target_size, maintain_aspect_ratio=True)
        
        # Then optimize quality to fit within size limit
        optimized_image, quality = ImageUtils.optimize_image_quality(sized_image, max_size_bytes)
        
        # Get final file size (using PNG format for consistency)
        buffer = BytesIO()
        optimized_image.save(buffer, format='PNG', optimize=True)
        file_size = buffer.tell()
        
        return optimized_image, quality, file_size

    @staticmethod
    def image_to_base64_optimized(image: Image.Image, target_size: tuple = (1200, 1600), max_size_bytes: int = 1024 * 1024) -> str:
        """

        Convert image to base64 with size and quality optimization.

        

        Args:

            image: PIL Image to convert

            target_size: Target size in pixels (width, height)

            max_size_bytes: Maximum file size in bytes (default 1MB)

            

        Returns:

            Base64 encoded string of the optimized image

        """
        processed_image, quality, file_size = ImageUtils.process_image_for_comparison(
            image, target_size, max_size_bytes
        )
        
        # Convert to base64 as PNG format
        buffer = BytesIO()
        processed_image.save(buffer, format='PNG', optimize=True)
        image_base64 = base64.b64encode(buffer.getvalue()).decode('utf-8')
        
        return image_base64