A1yCE commited on
Commit
86c3cf3
·
verified ·
1 Parent(s): affdd0f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -33
app.py CHANGED
@@ -1,46 +1,48 @@
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()
 
 
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()