|
import gradio as gr |
|
from PIL import Image |
|
import tempfile |
|
import os |
|
from image_processor import process_image |
|
|
|
|
|
def apply_standard_settings(setting): |
|
"""Returns the parameters for the selected standard setting.""" |
|
settings_dict = { |
|
"S light": (True, True, "240x240", 48, "whitesmoke"), |
|
"M light": (True, True, "480x480", 96, "whitesmoke"), |
|
"L light": (True, True, "960x960", 128, "whitesmoke"), |
|
"S dark": (True, True, "240x240", 48, "dimgray"), |
|
"M dark": (True, True, "480x480", 96, "dimgray"), |
|
"L dark": (True, True, "960x960", 128, "dimgray"), |
|
} |
|
|
|
return settings_dict.get(setting, (None, None, None, None, None)) |
|
|
|
|
|
def settings_description(crop, remove_bg, resize, padding, background): |
|
"""Generate an HTML text description of the current settings in a smaller font and list format.""" |
|
description = f""" |
|
<ul style="font-size:small;"> |
|
<li>Crop: {crop}</li> |
|
<li>Remove Background: {remove_bg}</li> |
|
<li>Resize: {resize if resize else 'No resize'}</li> |
|
<li>Padding: {padding}</li> |
|
<li>Background: {background}</li> |
|
</ul> |
|
""" |
|
return description |
|
|
|
|
|
def gradio_interface(image, standard_settings, crop=False, remove_bg=False, resize=None, padding=0, background="white"): |
|
|
|
if image is None: |
|
|
|
standard_image_path = './data/examples/supermario.png' |
|
image = Image.open(standard_image_path) |
|
|
|
if standard_settings and standard_settings != "None": |
|
crop, remove_bg, resize, padding, background = apply_standard_settings( |
|
standard_settings) |
|
|
|
|
|
applied_settings = settings_description( |
|
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 'WxH'.", "original", applied_settings |
|
|
|
|
|
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, applied_settings |
|
|
|
|
|
|
|
interface = gr.Interface(fn=gradio_interface, |
|
inputs=[ |
|
gr.components.Image(type="pil", examples=[ |
|
"data/examples/supermario.png"]), |
|
gr.components.Radio(choices=[ |
|
"None", "S light", "M light", "L light", "S dark", "M dark", "L dark"], label="Settings"), |
|
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"), |
|
gr.components.Textbox( |
|
label="Background", placeholder="Color name or hex code") |
|
], |
|
outputs=[ |
|
gr.components.Image(type="pil"), |
|
gr.components.HTML(label="Applied Settings") |
|
], |
|
title="IMAGER ___ Image Processor", |
|
description="Upload an image and select processing options or choose a standard setting. Supports crop, autoremove background, resize, add padding, and set the background color.",) |
|
|
|
if __name__ == "__main__": |
|
interface.launch() |
|
|