Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ import os
|
|
| 7 |
# Register HEIC opener
|
| 8 |
pillow_heif.register_heif_opener()
|
| 9 |
|
| 10 |
-
def process_image(image,
|
| 11 |
if image is None:
|
| 12 |
return None, None
|
| 13 |
|
|
@@ -15,70 +15,98 @@ def process_image(image, strength):
|
|
| 15 |
original_size = img.size
|
| 16 |
|
| 17 |
# ============================================================
|
| 18 |
-
# 🧮
|
| 19 |
-
#
|
|
|
|
| 20 |
# ============================================================
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
#
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
# 1. SCALE: As strength goes up, scale goes DOWN (0.15 at level 8)
|
| 27 |
-
scale_factor = max(0.05, 1.0 - (strength * 0.1))
|
| 28 |
-
if strength >= 8: # Fine tune for the "sweet spot"
|
| 29 |
-
scale_factor = 0.15 - ((strength - 8) * 0.05)
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
# 3. QUALITY
|
| 36 |
quality_val = int(max(1, 60 - (strength * 6)))
|
| 37 |
|
| 38 |
# ============================================================
|
| 39 |
-
# ⚙️
|
| 40 |
# ============================================================
|
| 41 |
|
| 42 |
-
#
|
| 43 |
if wax_size > 1:
|
| 44 |
img = img.filter(ImageFilter.MedianFilter(size=wax_size))
|
| 45 |
|
| 46 |
-
#
|
| 47 |
new_w = int(original_size[0] * scale_factor)
|
| 48 |
new_h = int(original_size[1] * scale_factor)
|
| 49 |
-
new_w, new_h = max(1, new_w), max(1, new_h)
|
| 50 |
|
| 51 |
img = img.resize((new_w, new_h), resample=Image.BILINEAR)
|
| 52 |
|
| 53 |
-
#
|
| 54 |
-
unique_name = f"
|
| 55 |
img.save(unique_name, format="HEIF", quality=quality_val)
|
| 56 |
|
| 57 |
-
#
|
| 58 |
preview_img = Image.open(unique_name)
|
| 59 |
preview_img = preview_img.resize(original_size, resample=Image.NEAREST)
|
| 60 |
|
| 61 |
return preview_img, unique_name
|
| 62 |
|
| 63 |
# ==========================================
|
| 64 |
-
#
|
| 65 |
# ==========================================
|
| 66 |
|
|
|
|
| 67 |
css = """
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
"""
|
| 70 |
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
with gr.Column(elem_id="col-container"):
|
| 73 |
-
gr.Markdown("#
|
| 74 |
|
| 75 |
with gr.Row():
|
| 76 |
input_img = gr.Image(type="pil", label="Upload", sources=["upload", "clipboard"])
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
btn = gr.Button("
|
| 82 |
|
| 83 |
result_img = gr.Image(label="Preview", interactive=False)
|
| 84 |
download_file = gr.File(label="Download HEIC")
|
|
|
|
| 7 |
# Register HEIC opener
|
| 8 |
pillow_heif.register_heif_opener()
|
| 9 |
|
| 10 |
+
def process_image(image, user_strength):
|
| 11 |
if image is None:
|
| 12 |
return None, None
|
| 13 |
|
|
|
|
| 15 |
original_size = img.size
|
| 16 |
|
| 17 |
# ============================================================
|
| 18 |
+
# 🧮 LOGIC MAPPING
|
| 19 |
+
# User Input: 1 to 10
|
| 20 |
+
# Old Logic: 5 to 10 (Since <5 wasn't compressed enough)
|
| 21 |
# ============================================================
|
| 22 |
|
| 23 |
+
# Map 1-10 input to the 5-10 strength range
|
| 24 |
+
# Input 1 -> 5.0
|
| 25 |
+
# Input 10 -> 10.0
|
| 26 |
+
strength = 5 + ((user_strength - 1) * (5/9))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
# 1. SCALE CALCULATION
|
| 29 |
+
# At old strength 5, scale was ~0.5. At 10, scale was 0.05.
|
| 30 |
+
scale_factor = max(0.05, 1.0 - (strength * 0.1))
|
| 31 |
+
|
| 32 |
+
# 2. WAX CALCULATION
|
| 33 |
+
wax_size = int(strength / 2)
|
| 34 |
+
if wax_size % 2 == 0: wax_size += 1
|
| 35 |
|
| 36 |
+
# 3. QUALITY CALCULATION
|
| 37 |
quality_val = int(max(1, 60 - (strength * 6)))
|
| 38 |
|
| 39 |
# ============================================================
|
| 40 |
+
# ⚙️ ENGINE
|
| 41 |
# ============================================================
|
| 42 |
|
| 43 |
+
# Wax
|
| 44 |
if wax_size > 1:
|
| 45 |
img = img.filter(ImageFilter.MedianFilter(size=wax_size))
|
| 46 |
|
| 47 |
+
# Crush
|
| 48 |
new_w = int(original_size[0] * scale_factor)
|
| 49 |
new_h = int(original_size[1] * scale_factor)
|
| 50 |
+
new_w, new_h = max(1, new_w), max(1, new_h)
|
| 51 |
|
| 52 |
img = img.resize((new_w, new_h), resample=Image.BILINEAR)
|
| 53 |
|
| 54 |
+
# Save
|
| 55 |
+
unique_name = f"compressed_{uuid.uuid4().hex[:8]}.heic"
|
| 56 |
img.save(unique_name, format="HEIF", quality=quality_val)
|
| 57 |
|
| 58 |
+
# Preview
|
| 59 |
preview_img = Image.open(unique_name)
|
| 60 |
preview_img = preview_img.resize(original_size, resample=Image.NEAREST)
|
| 61 |
|
| 62 |
return preview_img, unique_name
|
| 63 |
|
| 64 |
# ==========================================
|
| 65 |
+
# 🎨 STYLE & INTERFACE
|
| 66 |
# ==========================================
|
| 67 |
|
| 68 |
+
# Force white background, bigger text, sans-serif
|
| 69 |
css = """
|
| 70 |
+
body, .gradio-container {
|
| 71 |
+
background-color: white !important;
|
| 72 |
+
color: black !important;
|
| 73 |
+
font-family: 'Helvetica', 'Arial', sans-serif !important;
|
| 74 |
+
}
|
| 75 |
+
h1 {
|
| 76 |
+
color: black !important;
|
| 77 |
+
font-size: 2.5em !important;
|
| 78 |
+
font-weight: bold !important;
|
| 79 |
+
}
|
| 80 |
+
span, p, label {
|
| 81 |
+
font-size: 1.2em !important;
|
| 82 |
+
color: #333 !important;
|
| 83 |
+
}
|
| 84 |
+
/* Hide the footer */
|
| 85 |
+
footer {display: none !important;}
|
| 86 |
"""
|
| 87 |
|
| 88 |
+
# Clean theme with sans-serif font
|
| 89 |
+
theme = gr.themes.Default(
|
| 90 |
+
font=[gr.themes.GoogleFont("Helvetica"), "Arial", "sans-serif"],
|
| 91 |
+
text_size=gr.themes.sizes.text_lg
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
with gr.Blocks(css=css, theme=theme) as demo:
|
| 95 |
with gr.Column(elem_id="col-container"):
|
| 96 |
+
gr.Markdown("# Image Compressor")
|
| 97 |
|
| 98 |
with gr.Row():
|
| 99 |
input_img = gr.Image(type="pil", label="Upload", sources=["upload", "clipboard"])
|
| 100 |
|
| 101 |
+
strength_slider = gr.Slider(
|
| 102 |
+
minimum=1,
|
| 103 |
+
maximum=10,
|
| 104 |
+
value=5,
|
| 105 |
+
step=0.5,
|
| 106 |
+
label="Compression Strength"
|
| 107 |
+
)
|
| 108 |
|
| 109 |
+
btn = gr.Button("COMPRESS", variant="primary", size="lg")
|
| 110 |
|
| 111 |
result_img = gr.Image(label="Preview", interactive=False)
|
| 112 |
download_file = gr.File(label="Download HEIC")
|