|
import gradio as gr |
|
from PIL import Image |
|
import tempfile |
|
import os |
|
from image_processor import process_image |
|
|
|
|
|
def gradio_interface(image, crop, remove_bg, resize, padding, background): |
|
|
|
resize_dimensions = None |
|
if resize: |
|
try: |
|
width, height = map(int, resize.split('x')) |
|
resize_dimensions = (width, height) |
|
except ValueError: |
|
return "Invalid format for resize dimensions. Please use 'AxB'.", "original" |
|
|
|
|
|
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp_input: |
|
image.save(tmp_input, format="PNG") |
|
tmp_input_path = tmp_input.name |
|
|
|
|
|
tmp_output_path = tempfile.mktemp(suffix=".png") |
|
|
|
|
|
process_image(tmp_input_path, tmp_output_path, crop, |
|
remove_bg, resize_dimensions, padding, background) |
|
|
|
|
|
processed_image = Image.open(tmp_output_path) |
|
|
|
|
|
os.remove(tmp_input_path) |
|
os.remove(tmp_output_path) |
|
|
|
return processed_image |
|
|
|
|
|
|
|
interface = gr.Interface(fn=gradio_interface, |
|
inputs=[ |
|
gr.components.Image(type="pil"), |
|
gr.components.Checkbox(label="Crop"), |
|
gr.components.Checkbox( |
|
label="Remove Background"), |
|
gr.components.Textbox( |
|
label="Resize (WxH)", placeholder="Example: 100x100"), |
|
gr.components.Slider( |
|
minimum=0, maximum=200, label="Padding", default=0), |
|
gr.components.Textbox( |
|
label="Background", placeholder="Color name or hex code") |
|
], |
|
outputs=gr.components.Image(type="pil"), |
|
title="Image Processor", |
|
description="Upload an image and select processing options.") |
|
|
|
|
|
if __name__ == "__main__": |
|
interface.launch() |
|
|