import gradio as gr from PIL import Image import cv2 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. """ # img1 = cv2.resize(image1, (512,512)) # img2 = cv2.resize(image2, (512,512)) # img = img1*0.5 + img2*0.5 # img1 = image1.astype('float32') # img2 = image2.astype('float32') # img = 0.5*img1 + 0.5*img2 # img = img.astype('uint8') image1 = image1.resize((426, 240)) image1_size = image1.size image2_size = image2.size new_image = Image.new('RGB',(2*image1_size[0], image1_size[1]), (250,250,250)) new_image.paste(image1,(0,0)) new_image.paste(image2,(image1_size[0],0)) return new_image #attribute add # Allow selection of merge method with a dropdown # elem_id="merge-method", # elem_attrs={"merge-method": {"type": "dropdown", "choices": ["concatenate", "average", "custom"]}}, # 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", title="Image Merger", description="Merge two uploaded images using the selected method." ) interface.launch()