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()