Spaces:
Running
Running
trying like this
Browse files
app.py
CHANGED
|
@@ -27,9 +27,83 @@ def query(image, top_k=1):
|
|
| 27 |
results = index.query(embedding, k=top_k)
|
| 28 |
inx = results[0][0].tolist()
|
| 29 |
logits = results[1][0].tolist()
|
| 30 |
-
butterfly = ds.select(inx)["image"]
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
|
| 35 |
with gr.Blocks() as demo:
|
|
@@ -50,7 +124,7 @@ with gr.Blocks() as demo:
|
|
| 50 |
gr.Examples(
|
| 51 |
examples=["elton.jpg", "ken.jpg", "gaga.jpg", "taylor.jpg"],
|
| 52 |
inputs=inputs,
|
| 53 |
-
outputs=outputs,
|
| 54 |
fn=query,
|
| 55 |
cache_examples=True,
|
| 56 |
)
|
|
|
|
| 27 |
results = index.query(embedding, k=top_k)
|
| 28 |
inx = results[0][0].tolist()
|
| 29 |
logits = results[1][0].tolist()
|
| 30 |
+
butterfly = ds.select(inx)["image"][0]
|
| 31 |
+
return overlay_png_on_side_by_side_images(
|
| 32 |
+
image, butterfly, "cadre.png", "output.png", png_position=(50, 50)
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def overlay_png_on_side_by_side_images(
|
| 37 |
+
person_image_path,
|
| 38 |
+
insect_image_path,
|
| 39 |
+
overlay_image_path,
|
| 40 |
+
png_position=(0, 0),
|
| 41 |
+
):
|
| 42 |
+
"""
|
| 43 |
+
Overlays a PNG image onto two side-by-side images, resizing them to fit.
|
| 44 |
+
|
| 45 |
+
Args:
|
| 46 |
+
image1_path (str): Path to the first image.
|
| 47 |
+
image2_path (str): Path to the second image.
|
| 48 |
+
png_path (str): Path to the PNG image to overlay.
|
| 49 |
+
output_path (str): Path to save the resulting image.
|
| 50 |
+
png_position (tuple): (x, y) coordinates for the top-left corner of the PNG overlay.
|
| 51 |
+
"""
|
| 52 |
+
# Open images
|
| 53 |
+
img1 = Image.open(person_image_path).convert("RGBA")
|
| 54 |
+
img2 = Image.open(insect_image_path).convert("RGBA")
|
| 55 |
+
png_img = Image.open(overlay_image_path).convert("RGBA")
|
| 56 |
+
|
| 57 |
+
# Determine a common height for side-by-side images
|
| 58 |
+
min_height = 384
|
| 59 |
+
# Resize images to the common height, maintaining aspect ratio
|
| 60 |
+
img1 = img1.resize((288, 384), Image.LANCZOS)
|
| 61 |
+
img2 = img2.resize((288, 384), Image.LANCZOS)
|
| 62 |
+
combined_width = img1.width + img2.width
|
| 63 |
+
# Create a blank canvas for the combined image
|
| 64 |
+
|
| 65 |
+
combined_image = Image.new(
|
| 66 |
+
"RGBA", (combined_width, min_height), (0, 0, 0, 0)
|
| 67 |
+
) # Transparent background
|
| 68 |
+
|
| 69 |
+
# Paste images side by side
|
| 70 |
+
combined_image.paste(img1, (0, 0))
|
| 71 |
+
combined_image.paste(img2, (img1.width, 0))
|
| 72 |
+
|
| 73 |
+
# Resize PNG to fit within the combined image dimensions if necessary, or to a desired size
|
| 74 |
+
# For simplicity, let's resize the PNG to a quarter of the combined image's width, maintaining aspect ratio
|
| 75 |
+
target_png_width = combined_image.width
|
| 76 |
+
png_img = png_img.resize((combined_image.width, min_height), Image.LANCZOS)
|
| 77 |
+
|
| 78 |
+
# Overlay the PNG image
|
| 79 |
+
combined_image.paste(png_img, png_position, png_img)
|
| 80 |
+
|
| 81 |
+
# Save the result
|
| 82 |
+
combined_image.save(output_path)
|
| 83 |
+
# Determine a common height for side-by-side images
|
| 84 |
+
min_height = 384
|
| 85 |
+
# Resize images to the common height, maintaining aspect ratio
|
| 86 |
+
img1 = img1.resize((288, 384), Image.LANCZOS)
|
| 87 |
+
img2 = img2.resize((288, 384), Image.LANCZOS)
|
| 88 |
+
combined_width = img1.width + img2.width
|
| 89 |
+
# Create a blank canvas for the combined image
|
| 90 |
+
|
| 91 |
+
combined_image = Image.new(
|
| 92 |
+
"RGBA", (combined_width, min_height), (0, 0, 0, 0)
|
| 93 |
+
) # Transparent background
|
| 94 |
+
|
| 95 |
+
# Paste images side by side
|
| 96 |
+
combined_image.paste(img1, (0, 0))
|
| 97 |
+
combined_image.paste(img2, (img1.width, 0))
|
| 98 |
+
|
| 99 |
+
# Resize PNG to fit within the combined image dimensions if necessary, or to a desired size
|
| 100 |
+
# For simplicity, let's resize the PNG to a quarter of the combined image's width, maintaining aspect ratio
|
| 101 |
+
target_png_width = combined_image.width
|
| 102 |
+
png_img = png_img.resize((combined_image.width, min_height), Image.LANCZOS)
|
| 103 |
+
|
| 104 |
+
# Overlay the PNG image
|
| 105 |
+
combined_image.paste(png_img, png_position, png_img)
|
| 106 |
+
return combined_image
|
| 107 |
|
| 108 |
|
| 109 |
with gr.Blocks() as demo:
|
|
|
|
| 124 |
gr.Examples(
|
| 125 |
examples=["elton.jpg", "ken.jpg", "gaga.jpg", "taylor.jpg"],
|
| 126 |
inputs=inputs,
|
| 127 |
+
outputs=[outputs],
|
| 128 |
fn=query,
|
| 129 |
cache_examples=True,
|
| 130 |
)
|