File size: 1,728 Bytes
b19d00b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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()