Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,48 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from PIL import Image
|
| 3 |
|
| 4 |
-
def
|
| 5 |
"""
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
2. Downsampling the image using a high-quality filter.
|
| 9 |
-
3. Upscaling back using nearest-neighbor interpolation to preserve blocky pixels.
|
| 10 |
-
4. Quantizing the image to reduce the number of colors.
|
| 11 |
"""
|
| 12 |
-
# Ensure image is in RGB
|
| 13 |
if img.mode != "RGB":
|
| 14 |
img = img.convert("RGB")
|
| 15 |
|
| 16 |
-
|
| 17 |
-
im_blur = img.filter(ImageFilter.GaussianBlur(radius=blur_radius))
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
im_down = im_blur.resize(new_size, Image.LANCZOS)
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
|
|
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
)
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
# Launch the demo. (In a Hugging Face Space, this script will be run automatically.)
|
| 45 |
if __name__ == "__main__":
|
| 46 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
|
| 4 |
+
def pixel_block_downsample(img, block_size):
|
| 5 |
"""
|
| 6 |
+
Downsample the image by shrinking it and then re-upscaling
|
| 7 |
+
with nearest neighbor to create large, chunky 'pixel blocks'.
|
|
|
|
|
|
|
|
|
|
| 8 |
"""
|
| 9 |
+
# Ensure the image is in RGB
|
| 10 |
if img.mode != "RGB":
|
| 11 |
img = img.convert("RGB")
|
| 12 |
|
| 13 |
+
w, h = img.size
|
|
|
|
| 14 |
|
| 15 |
+
# Avoid zero or negative
|
| 16 |
+
if block_size < 1:
|
| 17 |
+
block_size = 1
|
|
|
|
| 18 |
|
| 19 |
+
# Compute the size to downscale to
|
| 20 |
+
new_w = max(1, w // block_size)
|
| 21 |
+
new_h = max(1, h // block_size)
|
| 22 |
|
| 23 |
+
# Downscale using a high-quality filter (e.g. BICUBIC or LANCZOS)
|
| 24 |
+
img_small = img.resize((new_w, new_h), resample=Image.BICUBIC)
|
| 25 |
+
|
| 26 |
+
# Upscale back using nearest-neighbor for the chunky pixel look
|
| 27 |
+
img_blocky = img_small.resize((w, h), resample=Image.NEAREST)
|
| 28 |
+
return img_blocky
|
| 29 |
|
| 30 |
+
def gradio_app():
|
| 31 |
+
# Gradio interface with a slider for the "Pixel Block Size"
|
| 32 |
+
iface = gr.Interface(
|
| 33 |
+
fn=pixel_block_downsample,
|
| 34 |
+
inputs=[
|
| 35 |
+
gr.Image(type="pil", label="Upload Image"),
|
| 36 |
+
gr.Slider(
|
| 37 |
+
minimum=1, maximum=64, step=1, value=8,
|
| 38 |
+
label="Choose Pixel Block Size"
|
| 39 |
+
)
|
| 40 |
+
],
|
| 41 |
+
outputs=gr.Image(type="pil", label="Downsampled Output"),
|
| 42 |
+
title="Pixel Block Downsampler"
|
| 43 |
+
)
|
| 44 |
+
return iface
|
| 45 |
|
|
|
|
| 46 |
if __name__ == "__main__":
|
| 47 |
+
# For local testing; on Hugging Face Spaces, this script is run automatically.
|
| 48 |
+
gradio_app().launch()
|