import gradio as gr from PIL import Image def merge_images(image1, image2, method="concatenate"): """ Merges two images using the specified method. Args: image1 (PIL.Image): The first image. image2 (PIL.Image): The second image. method (str, optional): The method for merging. Defaults to "concatenate". Supported methods: - "concatenate": Concatenates the images horizontally. - "average": Creates an average blend of the two images. - "custom": Allows for custom logic using (image1, image2) as input. Returns: PIL.Image: The merged image. """ if method == "concatenate": # Concatenate images horizontally width, height = image1.size merged_image = Image.new(image1.mode, (width * 2, height)) merged_image.paste(image1, (0, 0)) merged_image.paste(image2, (width, 0)) elif method == "average": # Create an average blend merged_image = Image.blend(image1, image2, 0.5) elif method == "custom": # Implement your custom logic here, using image1 and image2 # This allows for more advanced merging techniques pass else: raise ValueError(f"Unsupported merge method: {method}") return merged_image # Define Gradio interface interface = gr.Interface( fn=merge_images, inputs=[gr.Image(label="Image 1", type="pil"), gr.Image(label="Image 2", type="pil")], outputs="image", # Allow selection of merge method with a dropdown elem_id="merge-method", elem_attrs={"merge-method": {"type": "dropdown", "choices": ["concatenate", "average", "custom"]}}, title="Image Merger", description="Merge two uploaded images using the selected method." ) interface.launch()