quantumesoteric commited on
Commit
19d9c24
·
verified ·
1 Parent(s): d18221f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -29
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, strength):
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
- # 🧮 THE "STRENGTH" MATH
19
- # Mapping the 1-10 slider to your specific code settings
 
20
  # ============================================================
21
 
22
- # STRENGTH 1 (Light) -> Scale 1.0, Wax 0, Quality 50
23
- # STRENGTH 8 (Sweet) -> Scale 0.15, Wax 3, Quality 8 <-- Your Settings
24
- # STRENGTH 10 (Nuke) -> Scale 0.05, Wax 9, Quality 1
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
- # 2. WAX: As strength goes up, wax goes UP
32
- wax_size = int(strength / 2) # Level 8 = 4, Level 10 = 5
33
- if wax_size % 2 == 0: wax_size += 1 # Wax size must be odd number (1, 3, 5...)
 
 
 
 
34
 
35
- # 3. QUALITY: As strength goes up, quality goes DOWN
36
  quality_val = int(max(1, 60 - (strength * 6)))
37
 
38
  # ============================================================
39
- # ⚙️ THE ENGINE (Your exact code)
40
  # ============================================================
41
 
42
- # A. Wax Effect
43
  if wax_size > 1:
44
  img = img.filter(ImageFilter.MedianFilter(size=wax_size))
45
 
46
- # B. Resolution Crush (Downscale)
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) # Safety check
50
 
51
  img = img.resize((new_w, new_h), resample=Image.BILINEAR)
52
 
53
- # C. Save as HEIC (Compression Artifacts)
54
- unique_name = f"waxy_{uuid.uuid4().hex[:8]}.heic"
55
  img.save(unique_name, format="HEIF", quality=quality_val)
56
 
57
- # D. Reload and Upscale (For Preview)
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
- # 📱 THE SIMPLE INTERFACE
65
  # ==========================================
66
 
 
67
  css = """
68
- #col-container {max-width: 700px; margin-left: auto; margin-right: auto;}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  """
70
 
71
- with gr.Blocks(css=css, theme=gr.themes.Monochrome()) as demo:
 
 
 
 
 
 
72
  with gr.Column(elem_id="col-container"):
73
- gr.Markdown("# 🫠 Waxy Crusher")
74
 
75
  with gr.Row():
76
  input_img = gr.Image(type="pil", label="Upload", sources=["upload", "clipboard"])
77
 
78
- # The One Slider to Rule Them All
79
- strength_slider = gr.Slider(1, 10, value=8, step=1, label="Destruction Level (8 = Sweet Spot)")
 
 
 
 
 
80
 
81
- btn = gr.Button("🔥 CRUSH IT", variant="primary", size="lg")
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")