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