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"), } # Default to no special settings 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""" """ return description def gradio_interface(image, standard_settings, crop=False, remove_bg=False, resize=None, padding=0, background="white"): # Apply standard settings if selected and not "None" if image is None: # Load the standard image from the specified path if no image is uploaded 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) # Generate settings description applied_settings = settings_description( crop, remove_bg, resize, padding, background) # Convert resize string to tuple (if provided) 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 # Use a temporary file to save the input image from Gradio with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp_input: image.save(tmp_input, format="PNG") tmp_input_path = tmp_input.name # Prepare a temporary file for the output image tmp_output_path = tempfile.mktemp(suffix=".png") # Process the image process_image(tmp_input_path, tmp_output_path, crop, remove_bg, resize_dimensions, padding, background) # Load and return the processed image processed_image = Image.open(tmp_output_path) # Clean up temporary files os.remove(tmp_input_path) os.remove(tmp_output_path) return processed_image, applied_settings # Define the Gradio interface 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()