skelon commited on
Commit
be5be5e
·
verified ·
1 Parent(s): 1dd0a9d

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +284 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,284 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image
3
+
4
+ MAX_SIDE = 2048 # keeps processing fast on free CPU hardware
5
+
6
+
7
+ def pixelate(img, block_size, colors):
8
+ """Pixelate an image.
9
+
10
+ block_size: size of each pixel block in source pixels (higher = chunkier).
11
+ colors: number of palette colors (0 keeps the original colors).
12
+ """
13
+ if img is None:
14
+ return None
15
+
16
+ img = img.convert("RGB")
17
+
18
+ # Downscale very large uploads so the Space stays responsive
19
+ if max(img.size) > MAX_SIDE:
20
+ img.thumbnail((MAX_SIDE, MAX_SIDE), Image.LANCZOS)
21
+
22
+ w, h = img.size
23
+ block_size = max(1, int(block_size))
24
+ small_w = max(1, w // block_size)
25
+ small_h = max(1, h // block_size)
26
+
27
+ # Shrink (averaging each block), optionally reduce colors, then scale back up
28
+ small = img.resize((small_w, small_h), Image.BOX)
29
+
30
+ colors = max(0, min(256, int(colors)))
31
+ if colors > 0:
32
+ small = small.quantize(colors=colors, method=Image.Quantize.MEDIANCUT).convert("RGB")
33
+
34
+ return small.resize((w, h), Image.NEAREST)
35
+
36
+
37
+ CSS = """
38
+ @import url('https://fonts.googleapis.com/css2?family=DM+Sans:wght@400;500;600;700&family=Space+Grotesk:wght@500;600;700&display=swap');
39
+
40
+ :root {
41
+ --pixel-purple: #8b5cf6;
42
+ --pixel-pink: #ec4899;
43
+ --pixel-ink: #171322;
44
+ }
45
+ .gradio-container {
46
+ max-width: none !important;
47
+ min-height: 100vh;
48
+ padding: 0 !important;
49
+ background:
50
+ radial-gradient(circle at 8% 8%, rgba(139, 92, 246, 0.18), transparent 28rem),
51
+ radial-gradient(circle at 92% 18%, rgba(236, 72, 153, 0.12), transparent 24rem),
52
+ #faf9ff;
53
+ font-family: "DM Sans", sans-serif;
54
+ }
55
+ .gradio-container h1,
56
+ .gradio-container h2,
57
+ .gradio-container h3 {
58
+ font-family: "Space Grotesk", sans-serif;
59
+ }
60
+ .app-shell {
61
+ max-width: 1180px;
62
+ min-height: 100vh;
63
+ padding: 38px 24px 24px;
64
+ margin: 0 auto;
65
+ }
66
+ .hero {
67
+ position: relative;
68
+ overflow: hidden;
69
+ padding: 42px 44px;
70
+ border: 1px solid rgba(255, 255, 255, 0.18);
71
+ border-radius: 28px;
72
+ background:
73
+ radial-gradient(circle at 90% 15%, rgba(236, 72, 153, 0.55), transparent 13rem),
74
+ linear-gradient(120deg, #24133f 0%, #42227a 55%, #6d287d 100%);
75
+ box-shadow: 0 24px 70px rgba(49, 24, 91, 0.22);
76
+ color: white;
77
+ margin-bottom: 24px;
78
+ }
79
+ .hero::after {
80
+ position: absolute;
81
+ right: -40px;
82
+ bottom: -90px;
83
+ width: 240px;
84
+ height: 240px;
85
+ border: 1px solid rgba(255, 255, 255, 0.15);
86
+ border-radius: 34px;
87
+ transform: rotate(28deg);
88
+ content: "";
89
+ }
90
+ .hero-content {
91
+ position: relative;
92
+ z-index: 1;
93
+ max-width: 650px;
94
+ }
95
+ .eyebrow {
96
+ display: inline-block;
97
+ margin-bottom: 16px;
98
+ padding: 6px 11px;
99
+ border: 1px solid rgba(255, 255, 255, 0.25);
100
+ border-radius: 99px;
101
+ background: rgba(255, 255, 255, 0.1);
102
+ color: #f5d0fe;
103
+ font-size: 0.75rem;
104
+ font-weight: 700;
105
+ letter-spacing: 0.12em;
106
+ text-transform: uppercase;
107
+ }
108
+ .hero h1 {
109
+ margin: 0 0 8px;
110
+ font-size: clamp(2.2rem, 5vw, 4rem);
111
+ letter-spacing: -0.06em;
112
+ }
113
+ .hero p {
114
+ margin: 0;
115
+ opacity: 0.86;
116
+ font-size: 1.15rem;
117
+ line-height: 1.6;
118
+ }
119
+ .panel {
120
+ border: 1px solid rgba(139, 92, 246, 0.14);
121
+ border-radius: 22px;
122
+ padding: 22px;
123
+ background: rgba(255, 255, 255, 0.78);
124
+ box-shadow: 0 14px 40px rgba(51, 35, 92, 0.08);
125
+ backdrop-filter: blur(12px);
126
+ }
127
+ .panel h3 {
128
+ margin-top: 0;
129
+ color: var(--pixel-ink);
130
+ }
131
+ .result-panel {
132
+ margin-top: 24px;
133
+ padding: 22px;
134
+ border: 1px solid rgba(139, 92, 246, 0.14);
135
+ border-radius: 22px;
136
+ background: rgba(255, 255, 255, 0.82);
137
+ box-shadow: 0 14px 40px rgba(51, 35, 92, 0.08);
138
+ }
139
+ .section-label {
140
+ margin: 0 0 12px;
141
+ color: #5b21b6;
142
+ font-size: 0.78rem;
143
+ font-weight: 700;
144
+ letter-spacing: 0.1em;
145
+ text-transform: uppercase;
146
+ }
147
+ .drop-zone {
148
+ border-radius: 16px !important;
149
+ }
150
+ .action-row {
151
+ margin-top: 12px;
152
+ }
153
+ .primary-action {
154
+ border: 0 !important;
155
+ border-radius: 12px !important;
156
+ background: linear-gradient(100deg, var(--pixel-purple), var(--pixel-pink)) !important;
157
+ box-shadow: 0 8px 18px rgba(139, 92, 246, 0.24);
158
+ }
159
+ .reset-action {
160
+ border-radius: 12px !important;
161
+ }
162
+ .footer {
163
+ padding: 24px 0 0;
164
+ color: #716b80;
165
+ text-align: center;
166
+ font-size: 0.86rem;
167
+ }
168
+ .tip {
169
+ color: var(--body-text-color-subdued);
170
+ font-size: 0.9rem;
171
+ line-height: 1.5;
172
+ }
173
+ @media (max-width: 700px) {
174
+ .app-shell {
175
+ padding: 18px 12px;
176
+ }
177
+ .hero {
178
+ padding: 30px 24px;
179
+ border-radius: 22px;
180
+ }
181
+ .panel,
182
+ .result-panel {
183
+ padding: 16px;
184
+ border-radius: 18px;
185
+ }
186
+ }
187
+ """
188
+
189
+ with gr.Blocks(
190
+ title="Bangu - Image Pixelator",
191
+ ) as demo:
192
+ with gr.Column(elem_classes="app-shell"):
193
+ gr.HTML(
194
+ """
195
+ <section class="hero">
196
+ <div class="hero-content">
197
+ <span class="eyebrow">Image remix studio</span>
198
+ <h1>Bangu</h1>
199
+ <p>Turn any image into beautiful pixel art in seconds.
200
+ Tune the detail, choose your palette, and make it yours.</p>
201
+ </div>
202
+ </section>
203
+ """
204
+ )
205
+
206
+ with gr.Row(equal_height=False):
207
+ with gr.Column(scale=1, elem_classes="panel"):
208
+ gr.HTML('<p class="section-label">01 / Start here</p>')
209
+ gr.Markdown("### Choose an image")
210
+ inp = gr.Image(
211
+ type="pil",
212
+ label="Source image",
213
+ sources=["upload", "clipboard", "webcam"],
214
+ height=350,
215
+ elem_classes="drop-zone",
216
+ )
217
+ gr.Markdown(
218
+ '<p class="tip">Upload, paste, or take a photo. '
219
+ "Your image is processed locally and is not stored.</p>"
220
+ )
221
+
222
+ with gr.Column(scale=1, elem_classes="panel"):
223
+ gr.HTML('<p class="section-label">02 / Make it yours</p>')
224
+ gr.Markdown("### Adjust the effect")
225
+ block = gr.Slider(
226
+ minimum=2,
227
+ maximum=128,
228
+ value=16,
229
+ step=1,
230
+ label="Pixel size",
231
+ info="Small values keep more detail; large values create chunky pixels.",
232
+ )
233
+ colors = gr.Slider(
234
+ minimum=0,
235
+ maximum=64,
236
+ value=0,
237
+ step=1,
238
+ label="Color palette",
239
+ info="0 keeps the original colors. Lower values create a retro look.",
240
+ )
241
+ with gr.Row(elem_classes="action-row"):
242
+ btn = gr.Button(
243
+ "✨ Pixelate image",
244
+ variant="primary",
245
+ elem_classes="primary-action",
246
+ scale=2,
247
+ )
248
+ clear_btn = gr.Button(
249
+ "Reset",
250
+ elem_classes="reset-action",
251
+ scale=1,
252
+ )
253
+
254
+ with gr.Column(elem_classes="result-panel"):
255
+ gr.HTML('<p class="section-label">03 / Your creation</p>')
256
+ gr.Markdown("### Pixelated result")
257
+ out = gr.Image(
258
+ type="pil",
259
+ label="Result",
260
+ format="png",
261
+ interactive=False,
262
+ height=500,
263
+ )
264
+ gr.HTML('<p class="footer">Bangu &middot; Image Pixelator</p>')
265
+
266
+ # Live preview as the user moves the sliders or uploads a new image
267
+ for component in (inp, block, colors):
268
+ component.change(pixelate, [inp, block, colors], out, show_progress="hidden")
269
+ btn.click(pixelate, [inp, block, colors], out)
270
+ clear_btn.click(
271
+ lambda: (None, 16, 0, None),
272
+ outputs=[inp, block, colors, out],
273
+ show_progress="hidden",
274
+ )
275
+
276
+ if __name__ == "__main__":
277
+ demo.launch(
278
+ theme=gr.themes.Soft(
279
+ primary_hue="violet",
280
+ secondary_hue="purple",
281
+ neutral_hue="slate",
282
+ ),
283
+ css=CSS,
284
+ )
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio
2
+ pillow