Commit
·
3bdb73a
1
Parent(s):
351c6da
Compress image fix
Browse files- app.py +29 -7
- src/utils/compress.py +21 -11
app.py
CHANGED
@@ -1,17 +1,38 @@
|
|
1 |
import gradio as gr
|
2 |
from src.utils.add_text import add_text_to_image_base64
|
3 |
-
from src.utils.
|
4 |
from src.utils.remove_background import remove_background_from_url
|
5 |
from src.utils.visualize_image import visualize_base64_image
|
6 |
from src.utils.generate_image import generate_image
|
7 |
from src.utils.apply_filter import apply_filter_direct
|
8 |
from src.utils.watermark import add_watermark, remove_watermark
|
9 |
from src.utils.describe import describe_image
|
10 |
-
from src.utils.compress import compress_image
|
11 |
import base64
|
12 |
from PIL import Image
|
13 |
import io
|
14 |
import requests
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
def image_to_base64(image):
|
17 |
if image is None:
|
@@ -70,7 +91,6 @@ def gradio_generate_image(prompt, width=512, height=512):
|
|
70 |
print(f"Error generating image: {e}")
|
71 |
return None
|
72 |
|
73 |
-
|
74 |
def gradio_apply_filter(image, filter_type, intensity=1.0):
|
75 |
if image is None:
|
76 |
print("No image provided")
|
@@ -120,12 +140,14 @@ def gradio_remove_watermark(image):
|
|
120 |
return image
|
121 |
|
122 |
def gradio_compress_image(image, quality=80):
|
|
|
|
|
|
|
123 |
if image is None:
|
124 |
return None
|
125 |
try:
|
126 |
-
|
127 |
-
|
128 |
-
return base64_to_image(result)
|
129 |
except Exception as e:
|
130 |
print(f"Error compressing image: {e}")
|
131 |
return image
|
@@ -279,7 +301,7 @@ def create_gradio_interface():
|
|
279 |
with gr.Row():
|
280 |
compress_input = gr.Image(label="Upload Image", type="pil")
|
281 |
with gr.Column():
|
282 |
-
quality_slider = gr.Slider(
|
283 |
compress_output = gr.Image(label="Compressed Image")
|
284 |
|
285 |
compress_input.change(gradio_compress_image, [compress_input, quality_slider], compress_output)
|
|
|
1 |
import gradio as gr
|
2 |
from src.utils.add_text import add_text_to_image_base64
|
3 |
+
from src.utils.compress import compress_image_memory, compress_image_file
|
4 |
from src.utils.remove_background import remove_background_from_url
|
5 |
from src.utils.visualize_image import visualize_base64_image
|
6 |
from src.utils.generate_image import generate_image
|
7 |
from src.utils.apply_filter import apply_filter_direct
|
8 |
from src.utils.watermark import add_watermark, remove_watermark
|
9 |
from src.utils.describe import describe_image
|
|
|
10 |
import base64
|
11 |
from PIL import Image
|
12 |
import io
|
13 |
import requests
|
14 |
+
from io import BytesIO
|
15 |
+
from typing import Union
|
16 |
+
|
17 |
+
def change_format(image: Union[str, BytesIO], target_format: str) -> str:
|
18 |
+
"""
|
19 |
+
Change the format of an image from a URL to the specified target format.
|
20 |
+
"""
|
21 |
+
|
22 |
+
if not isinstance(image, BytesIO):
|
23 |
+
response = requests.get(image, timeout=30)
|
24 |
+
response.raise_for_status()
|
25 |
+
img = Image.open(BytesIO(response.content))
|
26 |
+
else:
|
27 |
+
img = Image.open(image)
|
28 |
+
|
29 |
+
output = BytesIO()
|
30 |
+
img.save(output, format=target_format)
|
31 |
+
output.seek(0)
|
32 |
+
|
33 |
+
encoded_image = base64.b64encode(output.getvalue()).decode('utf-8')
|
34 |
+
|
35 |
+
return encoded_image
|
36 |
|
37 |
def image_to_base64(image):
|
38 |
if image is None:
|
|
|
91 |
print(f"Error generating image: {e}")
|
92 |
return None
|
93 |
|
|
|
94 |
def gradio_apply_filter(image, filter_type, intensity=1.0):
|
95 |
if image is None:
|
96 |
print("No image provided")
|
|
|
140 |
return image
|
141 |
|
142 |
def gradio_compress_image(image, quality=80):
|
143 |
+
"""
|
144 |
+
Compress image for Gradio interface - FUNCIÓN CORREGIDA
|
145 |
+
"""
|
146 |
if image is None:
|
147 |
return None
|
148 |
try:
|
149 |
+
compressed_image = compress_image_memory(image, quality, "JPEG")
|
150 |
+
return compressed_image
|
|
|
151 |
except Exception as e:
|
152 |
print(f"Error compressing image: {e}")
|
153 |
return image
|
|
|
301 |
with gr.Row():
|
302 |
compress_input = gr.Image(label="Upload Image", type="pil")
|
303 |
with gr.Column():
|
304 |
+
quality_slider = gr.Slider(0, 100, 80, label="Quality %")
|
305 |
compress_output = gr.Image(label="Compressed Image")
|
306 |
|
307 |
compress_input.change(gradio_compress_image, [compress_input, quality_slider], compress_output)
|
src/utils/compress.py
CHANGED
@@ -2,7 +2,7 @@ from PIL import Image
|
|
2 |
import os
|
3 |
from typing import Literal, Optional
|
4 |
|
5 |
-
def
|
6 |
input_path: str,
|
7 |
output_path: str,
|
8 |
quality: int = 85,
|
@@ -11,15 +11,7 @@ def compress_image(
|
|
11 |
max_height: Optional[int] = None
|
12 |
) -> str:
|
13 |
"""
|
14 |
-
Compress an image file.
|
15 |
-
|
16 |
-
Args:
|
17 |
-
input_path: Path to input image
|
18 |
-
output_path: Path for compressed output
|
19 |
-
quality: Compression quality 1-95 (for JPEG/WEBP)
|
20 |
-
format: Output format
|
21 |
-
max_width: Maximum width (optional)
|
22 |
-
max_height: Maximum height (optional)
|
23 |
"""
|
24 |
try:
|
25 |
if not os.path.splitext(output_path)[1]:
|
@@ -46,4 +38,22 @@ def compress_image(
|
|
46 |
return f"✅ Compressed successfully!\nOriginal: {original_size:.2f}MB → Compressed: {compressed_size:.2f}MB\nReduction: {reduction:.1f}%"
|
47 |
|
48 |
except Exception as e:
|
49 |
-
return f"❌ Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import os
|
3 |
from typing import Literal, Optional
|
4 |
|
5 |
+
def compress_image_file(
|
6 |
input_path: str,
|
7 |
output_path: str,
|
8 |
quality: int = 85,
|
|
|
11 |
max_height: Optional[int] = None
|
12 |
) -> str:
|
13 |
"""
|
14 |
+
Compress an image file from disk.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
"""
|
16 |
try:
|
17 |
if not os.path.splitext(output_path)[1]:
|
|
|
38 |
return f"✅ Compressed successfully!\nOriginal: {original_size:.2f}MB → Compressed: {compressed_size:.2f}MB\nReduction: {reduction:.1f}%"
|
39 |
|
40 |
except Exception as e:
|
41 |
+
return f"❌ Error: {str(e)}"
|
42 |
+
|
43 |
+
def compress_image_memory(image: Image.Image, quality: int = 80, format: str = "JPEG") -> Image.Image:
|
44 |
+
"""
|
45 |
+
Compress an image in memory and return the compressed image.
|
46 |
+
"""
|
47 |
+
if format == "JPEG" and image.mode in ("RGBA", "P"):
|
48 |
+
image = image.convert("RGB")
|
49 |
+
|
50 |
+
output = BytesIO()
|
51 |
+
save_kwargs = {"format": format, "optimize": True}
|
52 |
+
|
53 |
+
if format in ["JPEG", "WEBP"]:
|
54 |
+
save_kwargs["quality"] = quality
|
55 |
+
|
56 |
+
image.save(output, **save_kwargs)
|
57 |
+
output.seek(0)
|
58 |
+
|
59 |
+
return Image.open(output)
|