|
|
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'. |
|
|
""" |
|
|
|
|
|
if img.mode != "RGB": |
|
|
img = img.convert("RGB") |
|
|
|
|
|
w, h = img.size |
|
|
|
|
|
|
|
|
if block_size < 1: |
|
|
block_size = 1 |
|
|
|
|
|
|
|
|
new_w = max(1, w // block_size) |
|
|
new_h = max(1, h // block_size) |
|
|
|
|
|
|
|
|
img_small = img.resize((new_w, new_h), resample=Image.BICUBIC) |
|
|
|
|
|
|
|
|
img_blocky = img_small.resize((w, h), resample=Image.NEAREST) |
|
|
return img_blocky |
|
|
|
|
|
def gradio_app(): |
|
|
|
|
|
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__": |
|
|
|
|
|
gradio_app().launch() |
|
|
|