Spaces:
Runtime error
Runtime error
Commit
·
b19d00b
1
Parent(s):
b9c6875
app.py added
Browse files
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
|
| 4 |
+
def merge_images(image1, image2, method="concatenate"):
|
| 5 |
+
"""
|
| 6 |
+
Merges two images using the specified method.
|
| 7 |
+
|
| 8 |
+
Args:
|
| 9 |
+
image1 (PIL.Image): The first image.
|
| 10 |
+
image2 (PIL.Image): The second image.
|
| 11 |
+
method (str, optional): The method for merging. Defaults to "concatenate".
|
| 12 |
+
Supported methods:
|
| 13 |
+
- "concatenate": Concatenates the images horizontally.
|
| 14 |
+
- "average": Creates an average blend of the two images.
|
| 15 |
+
- "custom": Allows for custom logic using (image1, image2) as input.
|
| 16 |
+
|
| 17 |
+
Returns:
|
| 18 |
+
PIL.Image: The merged image.
|
| 19 |
+
"""
|
| 20 |
+
|
| 21 |
+
if method == "concatenate":
|
| 22 |
+
# Concatenate images horizontally
|
| 23 |
+
width, height = image1.size
|
| 24 |
+
merged_image = Image.new(image1.mode, (width * 2, height))
|
| 25 |
+
merged_image.paste(image1, (0, 0))
|
| 26 |
+
merged_image.paste(image2, (width, 0))
|
| 27 |
+
elif method == "average":
|
| 28 |
+
# Create an average blend
|
| 29 |
+
merged_image = Image.blend(image1, image2, 0.5)
|
| 30 |
+
elif method == "custom":
|
| 31 |
+
# Implement your custom logic here, using image1 and image2
|
| 32 |
+
# This allows for more advanced merging techniques
|
| 33 |
+
pass
|
| 34 |
+
else:
|
| 35 |
+
raise ValueError(f"Unsupported merge method: {method}")
|
| 36 |
+
|
| 37 |
+
return merged_image
|
| 38 |
+
|
| 39 |
+
# Define Gradio interface
|
| 40 |
+
interface = gr.Interface(
|
| 41 |
+
fn=merge_images,
|
| 42 |
+
inputs=[gr.Image(label="Image 1", type="pil"), gr.Image(label="Image 2", type="pil")],
|
| 43 |
+
outputs="image",
|
| 44 |
+
# Allow selection of merge method with a dropdown
|
| 45 |
+
elem_id="merge-method",
|
| 46 |
+
elem_attrs={"merge-method": {"type": "dropdown", "choices": ["concatenate", "average", "custom"]}},
|
| 47 |
+
title="Image Merger",
|
| 48 |
+
description="Merge two uploaded images using the selected method."
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
interface.launch()
|