Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,46 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
-
def
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from PIL import Image, ImageFilter
|
| 3 |
|
| 4 |
+
def enhance_pixel_art(img, downscale_factor, num_colors, blur_radius):
|
| 5 |
+
"""
|
| 6 |
+
Enhances pixel art by:
|
| 7 |
+
1. Blurring the image slightly to remove high-frequency artifacts.
|
| 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 mode
|
| 13 |
+
if img.mode != "RGB":
|
| 14 |
+
img = img.convert("RGB")
|
| 15 |
+
|
| 16 |
+
# Apply a slight Gaussian blur to smooth out artifacts
|
| 17 |
+
im_blur = img.filter(ImageFilter.GaussianBlur(radius=blur_radius))
|
| 18 |
+
|
| 19 |
+
# Downscale using a high-quality resampling filter (Lanczos)
|
| 20 |
+
new_size = (max(1, im_blur.width // downscale_factor),
|
| 21 |
+
max(1, im_blur.height // downscale_factor))
|
| 22 |
+
im_down = im_blur.resize(new_size, Image.LANCZOS)
|
| 23 |
+
|
| 24 |
+
# Upscale back using nearest-neighbor to maintain a pixelated look
|
| 25 |
+
im_up = im_down.resize(img.size, Image.NEAREST)
|
| 26 |
+
|
| 27 |
+
# Quantize to reduce the number of colors (using median cut)
|
| 28 |
+
im_quant = im_up.quantize(colors=num_colors, method=Image.MEDIANCUT, dither=Image.NONE)
|
| 29 |
+
return im_quant
|
| 30 |
|
| 31 |
+
# Create a Gradio interface with image input, sliders for parameters, and image output.
|
| 32 |
+
iface = gr.Interface(
|
| 33 |
+
fn=enhance_pixel_art,
|
| 34 |
+
inputs=[
|
| 35 |
+
gr.Image(type="pil", label="Input Image"),
|
| 36 |
+
gr.Slider(2, 8, value=4, step=1, label="Downscale Factor"),
|
| 37 |
+
gr.Slider(2, 32, value=16, step=1, label="Number of Colors"),
|
| 38 |
+
gr.Slider(0.0, 5.0, value=0.5, step=0.1, label="Blur Radius")
|
| 39 |
+
],
|
| 40 |
+
outputs=gr.Image(type="pil", label="Enhanced Pixel Art"),
|
| 41 |
+
title="Pixel Art Enhancer"
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
# Launch the demo. (In a Hugging Face Space, this script will be run automatically.)
|
| 45 |
+
if __name__ == "__main__":
|
| 46 |
+
iface.launch()
|