Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,51 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from PIL import Image, ImageEnhance
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
# --- 1. Processing Logic (
|
| 6 |
def process_image(image, split_count, should_enhance, format_type):
|
| 7 |
-
if image is None: return None
|
|
|
|
|
|
|
| 8 |
if should_enhance:
|
| 9 |
enhancer = ImageEnhance.Sharpness(image)
|
| 10 |
image = enhancer.enhance(1.5)
|
| 11 |
contrast = ImageEnhance.Contrast(image)
|
| 12 |
image = contrast.enhance(1.1)
|
|
|
|
| 13 |
img_width, img_height = image.size
|
| 14 |
split_width = img_width // int(split_count)
|
| 15 |
output_files = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
if not os.path.exists("outputs"): os.makedirs("outputs")
|
|
|
|
| 17 |
for i in range(int(split_count)):
|
| 18 |
left = i * split_width
|
| 19 |
right = (i + 1) * split_width
|
|
|
|
|
|
|
| 20 |
part = image.crop((left, 0, right, img_height))
|
|
|
|
|
|
|
| 21 |
if part.mode in ('RGBA', 'LA') or (part.mode == 'P' and 'transparency' in part.info):
|
| 22 |
part = part.convert('RGB')
|
|
|
|
|
|
|
| 23 |
ext = "png" if format_type == "PNG (HQ)" else "jpg"
|
| 24 |
save_path = f"outputs/ts_part_{i+1}.{ext}"
|
| 25 |
part.save(save_path, quality=95)
|
| 26 |
output_files.append(save_path)
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
# --- 2. New Premium Lighting CSS ---
|
| 30 |
custom_css = """
|
| 31 |
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600;900&display=swap');
|
| 32 |
|
|
@@ -38,19 +57,18 @@ body, .gradio-container {
|
|
| 38 |
|
| 39 |
/* The Main Card with Lighting Effect */
|
| 40 |
#main_card {
|
| 41 |
-
background: rgba(30, 30, 40, 0.8);
|
| 42 |
backdrop-filter: blur(30px);
|
| 43 |
-webkit-backdrop-filter: blur(30px);
|
| 44 |
-
border: 1px solid rgba(255, 255, 255, 0.15);
|
| 45 |
border-radius: 30px;
|
| 46 |
-
padding: 50px;
|
| 47 |
-
/* THE LIGHTING EFFECT GLOW */
|
| 48 |
box-shadow: 0 0 50px rgba(100, 200, 255, 0.15), inset 0 0 20px rgba(255, 255, 255, 0.05);
|
| 49 |
-
max-width:
|
| 50 |
margin: 40px auto;
|
| 51 |
}
|
| 52 |
|
| 53 |
-
/* New TS Logo Style
|
| 54 |
#logo_text h1 {
|
| 55 |
color: #ffffff;
|
| 56 |
font-size: 5rem !important;
|
|
@@ -58,7 +76,6 @@ body, .gradio-container {
|
|
| 58 |
text-align: center;
|
| 59 |
margin-bottom: 0px;
|
| 60 |
letter-spacing: -2px;
|
| 61 |
-
/* White Lighting Glow on Text */
|
| 62 |
text-shadow: 0 0 25px rgba(150, 220, 255, 0.8);
|
| 63 |
}
|
| 64 |
|
|
@@ -72,30 +89,27 @@ body, .gradio-container {
|
|
| 72 |
letter-spacing: 1px;
|
| 73 |
}
|
| 74 |
|
| 75 |
-
/* Input Controls Styling
|
| 76 |
.block, .svelte-12cmxck, .form {
|
| 77 |
background: rgba(255,255,255,0.03) !important;
|
| 78 |
border: 1px solid rgba(255,255,255,0.1) !important;
|
| 79 |
border-radius: 20px !important;
|
| 80 |
-
padding: 20px !important;
|
| 81 |
-
}
|
| 82 |
-
/* Fixing the cutting issue */
|
| 83 |
-
.gradio-row {
|
| 84 |
-
gap: 25px !important; /* Space between elements */
|
| 85 |
-
flex-wrap: wrap; /* Ensure things don't cut off on small screens */
|
| 86 |
}
|
|
|
|
|
|
|
| 87 |
label span { color: #fff !important; font-weight: 600; font-size: 1rem;}
|
| 88 |
|
| 89 |
/* The Button - Glowing */
|
| 90 |
.primary-btn {
|
| 91 |
background: linear-gradient(135deg, #ffffff, #d0d0d0) !important;
|
| 92 |
-
color: #000 !important;
|
| 93 |
border: none !important;
|
| 94 |
font-size: 1.3rem !important;
|
| 95 |
font-weight: 900 !important;
|
| 96 |
padding: 20px !important;
|
| 97 |
border-radius: 20px !important;
|
| 98 |
-
box-shadow: 0 0 30px rgba(255, 255, 255, 0.3);
|
| 99 |
transition: all 0.3s ease;
|
| 100 |
}
|
| 101 |
.primary-btn:hover {
|
|
@@ -111,27 +125,27 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as app:
|
|
| 111 |
gr.Markdown("# TS", elem_id="logo_text")
|
| 112 |
gr.Markdown("### PREMIUM CAROUSEL STUDIO", elem_id="subtitle_text")
|
| 113 |
|
| 114 |
-
# Using Row without 'panel' variant for cleaner look
|
| 115 |
with gr.Row():
|
| 116 |
-
# Left Side (Inputs)
|
| 117 |
-
with gr.Column(scale=1
|
| 118 |
inp_img = gr.Image(type="pil", label="1. Drop Your Wide Image", sources=["upload", "clipboard"], height=300)
|
| 119 |
|
| 120 |
-
|
| 121 |
-
with gr.Column(): # Removed Group for more space
|
| 122 |
slider = gr.Slider(2, 5, value=3, step=1, label="2. How many parts?")
|
| 123 |
|
| 124 |
-
# Checkboxes in their own spacious row
|
| 125 |
with gr.Row():
|
| 126 |
-
enhance_opt = gr.Checkbox(label="Magic Enhance
|
| 127 |
-
fmt_opt = gr.Radio(["JPG", "PNG (HQ)"], label="
|
| 128 |
|
| 129 |
-
btn = gr.Button("⚡ GENERATE
|
| 130 |
|
| 131 |
-
# Right Side (Output)
|
| 132 |
-
with gr.Column(scale=
|
| 133 |
-
|
|
|
|
|
|
|
| 134 |
|
| 135 |
-
|
|
|
|
| 136 |
|
| 137 |
app.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from PIL import Image, ImageEnhance, ImageDraw
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# --- 1. Processing Logic (Updated with Preview) ---
|
| 6 |
def process_image(image, split_count, should_enhance, format_type):
|
| 7 |
+
if image is None: return None, None # Return 2 nones (Preview & Gallery)
|
| 8 |
+
|
| 9 |
+
# A. Enhancement
|
| 10 |
if should_enhance:
|
| 11 |
enhancer = ImageEnhance.Sharpness(image)
|
| 12 |
image = enhancer.enhance(1.5)
|
| 13 |
contrast = ImageEnhance.Contrast(image)
|
| 14 |
image = contrast.enhance(1.1)
|
| 15 |
+
|
| 16 |
img_width, img_height = image.size
|
| 17 |
split_width = img_width // int(split_count)
|
| 18 |
output_files = []
|
| 19 |
+
|
| 20 |
+
# B. Creating the "Insta-Preview" (Stitching parts together)
|
| 21 |
+
# Hum parts ko wapas jod kar dikhayenge taaki user ko 'Seamless' feel aaye
|
| 22 |
+
preview_img = Image.new('RGB', (img_width, img_height))
|
| 23 |
+
|
| 24 |
if not os.path.exists("outputs"): os.makedirs("outputs")
|
| 25 |
+
|
| 26 |
for i in range(int(split_count)):
|
| 27 |
left = i * split_width
|
| 28 |
right = (i + 1) * split_width
|
| 29 |
+
|
| 30 |
+
# Crop
|
| 31 |
part = image.crop((left, 0, right, img_height))
|
| 32 |
+
|
| 33 |
+
# Transparency Fix
|
| 34 |
if part.mode in ('RGBA', 'LA') or (part.mode == 'P' and 'transparency' in part.info):
|
| 35 |
part = part.convert('RGB')
|
| 36 |
+
|
| 37 |
+
# Save for Gallery
|
| 38 |
ext = "png" if format_type == "PNG (HQ)" else "jpg"
|
| 39 |
save_path = f"outputs/ts_part_{i+1}.{ext}"
|
| 40 |
part.save(save_path, quality=95)
|
| 41 |
output_files.append(save_path)
|
| 42 |
+
|
| 43 |
+
# Add to Preview (Paste side by side)
|
| 44 |
+
preview_img.paste(part, (left, 0))
|
| 45 |
+
|
| 46 |
+
return preview_img, output_files
|
| 47 |
|
| 48 |
+
# --- 2. New Premium Lighting CSS (SAME AS YOURS) ---
|
| 49 |
custom_css = """
|
| 50 |
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600;900&display=swap');
|
| 51 |
|
|
|
|
| 57 |
|
| 58 |
/* The Main Card with Lighting Effect */
|
| 59 |
#main_card {
|
| 60 |
+
background: rgba(30, 30, 40, 0.8);
|
| 61 |
backdrop-filter: blur(30px);
|
| 62 |
-webkit-backdrop-filter: blur(30px);
|
| 63 |
+
border: 1px solid rgba(255, 255, 255, 0.15);
|
| 64 |
border-radius: 30px;
|
| 65 |
+
padding: 50px;
|
|
|
|
| 66 |
box-shadow: 0 0 50px rgba(100, 200, 255, 0.15), inset 0 0 20px rgba(255, 255, 255, 0.05);
|
| 67 |
+
max-width: 1100px;
|
| 68 |
margin: 40px auto;
|
| 69 |
}
|
| 70 |
|
| 71 |
+
/* New TS Logo Style */
|
| 72 |
#logo_text h1 {
|
| 73 |
color: #ffffff;
|
| 74 |
font-size: 5rem !important;
|
|
|
|
| 76 |
text-align: center;
|
| 77 |
margin-bottom: 0px;
|
| 78 |
letter-spacing: -2px;
|
|
|
|
| 79 |
text-shadow: 0 0 25px rgba(150, 220, 255, 0.8);
|
| 80 |
}
|
| 81 |
|
|
|
|
| 89 |
letter-spacing: 1px;
|
| 90 |
}
|
| 91 |
|
| 92 |
+
/* Input Controls Styling */
|
| 93 |
.block, .svelte-12cmxck, .form {
|
| 94 |
background: rgba(255,255,255,0.03) !important;
|
| 95 |
border: 1px solid rgba(255,255,255,0.1) !important;
|
| 96 |
border-radius: 20px !important;
|
| 97 |
+
padding: 20px !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
}
|
| 99 |
+
|
| 100 |
+
.gradio-row { gap: 25px !important; flex-wrap: wrap; }
|
| 101 |
label span { color: #fff !important; font-weight: 600; font-size: 1rem;}
|
| 102 |
|
| 103 |
/* The Button - Glowing */
|
| 104 |
.primary-btn {
|
| 105 |
background: linear-gradient(135deg, #ffffff, #d0d0d0) !important;
|
| 106 |
+
color: #000 !important;
|
| 107 |
border: none !important;
|
| 108 |
font-size: 1.3rem !important;
|
| 109 |
font-weight: 900 !important;
|
| 110 |
padding: 20px !important;
|
| 111 |
border-radius: 20px !important;
|
| 112 |
+
box-shadow: 0 0 30px rgba(255, 255, 255, 0.3);
|
| 113 |
transition: all 0.3s ease;
|
| 114 |
}
|
| 115 |
.primary-btn:hover {
|
|
|
|
| 125 |
gr.Markdown("# TS", elem_id="logo_text")
|
| 126 |
gr.Markdown("### PREMIUM CAROUSEL STUDIO", elem_id="subtitle_text")
|
| 127 |
|
|
|
|
| 128 |
with gr.Row():
|
| 129 |
+
# Left Side (Inputs)
|
| 130 |
+
with gr.Column(scale=1):
|
| 131 |
inp_img = gr.Image(type="pil", label="1. Drop Your Wide Image", sources=["upload", "clipboard"], height=300)
|
| 132 |
|
| 133 |
+
with gr.Column():
|
|
|
|
| 134 |
slider = gr.Slider(2, 5, value=3, step=1, label="2. How many parts?")
|
| 135 |
|
|
|
|
| 136 |
with gr.Row():
|
| 137 |
+
enhance_opt = gr.Checkbox(label="Magic Enhance", value=True)
|
| 138 |
+
fmt_opt = gr.Radio(["JPG", "PNG (HQ)"], label="Format", value="JPG")
|
| 139 |
|
| 140 |
+
btn = gr.Button("⚡ GENERATE & PREVIEW", variant="primary", elem_classes=["primary-btn"])
|
| 141 |
|
| 142 |
+
# Right Side (Output - Now with Preview)
|
| 143 |
+
with gr.Column(scale=1.5):
|
| 144 |
+
# Ye hai wo naya FEATURE:
|
| 145 |
+
preview_out = gr.Image(label="📱 Insta-Preview (Check Seamless Look)", interactive=False)
|
| 146 |
+
out_gal = gr.Gallery(label="⬇️ Download Parts Here", columns=3, height="auto", object_fit="contain")
|
| 147 |
|
| 148 |
+
# Connect both outputs (Preview + Gallery)
|
| 149 |
+
btn.click(process_image, inputs=[inp_img, slider, enhance_opt, fmt_opt], outputs=[preview_out, out_gal])
|
| 150 |
|
| 151 |
app.launch()
|