Spaces:
Sleeping
Sleeping
Update app.py
Browse filestry to crop background
app.py
CHANGED
|
@@ -44,9 +44,6 @@ def load_image_sources():
|
|
| 44 |
|
| 45 |
image_sources = load_image_sources()
|
| 46 |
|
| 47 |
-
# def get_image_url(image_type: str):
|
| 48 |
-
# return image_sources.get(image_type, None)
|
| 49 |
-
|
| 50 |
def get_image_url(image_type: str):
|
| 51 |
"""Finds the best match for the given image type using fuzzy matching."""
|
| 52 |
choices = list(image_sources.keys()) # Get all available keys
|
|
@@ -59,20 +56,6 @@ def get_image_url(image_type: str):
|
|
| 59 |
#return None # No good match found
|
| 60 |
return "https://editor.p5js.org/kfahn/full/2XD5Y8MiV"
|
| 61 |
|
| 62 |
-
# def get_image_url(image_type: str):
|
| 63 |
-
# """Finds an approximate match using simple substring matching."""
|
| 64 |
-
# image_type = image_type.lower()
|
| 65 |
-
|
| 66 |
-
# # Check for exact match first
|
| 67 |
-
# if image_type in image_sources:
|
| 68 |
-
# return image_sources[image_type]
|
| 69 |
-
|
| 70 |
-
# # If no exact match, look for a partial match
|
| 71 |
-
# for key in image_sources:
|
| 72 |
-
# if image_type in key.lower(): # Check if input is a substring of a key
|
| 73 |
-
# return image_sources[key]
|
| 74 |
-
# return None # No match found
|
| 75 |
-
|
| 76 |
async def capture_screenshot(image_type: str):
|
| 77 |
"""Launches Playwright and uses user input, if any, to captures a screenshot of an image from p5.js."""
|
| 78 |
print("Launching Playwright...")
|
|
@@ -98,6 +81,32 @@ async def capture_screenshot(image_type: str):
|
|
| 98 |
await page.screenshot(path="img.png")
|
| 99 |
await browser.close()
|
| 100 |
print("Screenshot saved!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
@tool
|
| 103 |
def grab_image(image_type: str) -> Image:
|
|
@@ -118,16 +127,15 @@ def grab_image(image_type: str) -> Image:
|
|
| 118 |
loop.run_until_complete(capture_screenshot(image_type))
|
| 119 |
|
| 120 |
print("Loading image for Gradio...")
|
| 121 |
-
img = Image.open("img.png")
|
| 122 |
header_height = 50
|
| 123 |
left_crop = 0 # Adjust to remove background
|
| 124 |
right_crop = img.width - 0
|
| 125 |
|
| 126 |
-
cropped_img = img.crop((left_crop, header_height, right_crop, img.height))
|
|
|
|
| 127 |
return cropped_img
|
| 128 |
|
| 129 |
-
|
| 130 |
-
|
| 131 |
@tool
|
| 132 |
def get_joke() -> str:
|
| 133 |
"""
|
|
|
|
| 44 |
|
| 45 |
image_sources = load_image_sources()
|
| 46 |
|
|
|
|
|
|
|
|
|
|
| 47 |
def get_image_url(image_type: str):
|
| 48 |
"""Finds the best match for the given image type using fuzzy matching."""
|
| 49 |
choices = list(image_sources.keys()) # Get all available keys
|
|
|
|
| 56 |
#return None # No good match found
|
| 57 |
return "https://editor.p5js.org/kfahn/full/2XD5Y8MiV"
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
async def capture_screenshot(image_type: str):
|
| 60 |
"""Launches Playwright and uses user input, if any, to captures a screenshot of an image from p5.js."""
|
| 61 |
print("Launching Playwright...")
|
|
|
|
| 81 |
await page.screenshot(path="img.png")
|
| 82 |
await browser.close()
|
| 83 |
print("Screenshot saved!")
|
| 84 |
+
|
| 85 |
+
def crop_based_on_bg(img: Image, bg_color=(59, 59, 59)):
|
| 86 |
+
# img = Image.open(image_path).convert("RGB")
|
| 87 |
+
# pixels = img.load()
|
| 88 |
+
|
| 89 |
+
# Define fixed crop for top header
|
| 90 |
+
top_crop = 50
|
| 91 |
+
width, height = img.size
|
| 92 |
+
|
| 93 |
+
# Find leftmost and rightmost non-background pixels
|
| 94 |
+
left_crop, right_crop = 0, width
|
| 95 |
+
for x in range(width):
|
| 96 |
+
column = [pixels[x, y] for y in range(top_crop, height)]
|
| 97 |
+
if any(pixel != bg_color for pixel in column):
|
| 98 |
+
left_crop = x
|
| 99 |
+
break
|
| 100 |
+
|
| 101 |
+
for x in range(width - 1, -1, -1):
|
| 102 |
+
column = [pixels[x, y] for y in range(top_crop, height)]
|
| 103 |
+
if any(pixel != bg_color for pixel in column):
|
| 104 |
+
right_crop = x
|
| 105 |
+
break
|
| 106 |
+
|
| 107 |
+
# Crop and save
|
| 108 |
+
cropped_img = img.crop((left_crop, top_crop, right_crop, height))
|
| 109 |
+
return cropped_img
|
| 110 |
|
| 111 |
@tool
|
| 112 |
def grab_image(image_type: str) -> Image:
|
|
|
|
| 127 |
loop.run_until_complete(capture_screenshot(image_type))
|
| 128 |
|
| 129 |
print("Loading image for Gradio...")
|
| 130 |
+
img = Image.open("img.png").RGB
|
| 131 |
header_height = 50
|
| 132 |
left_crop = 0 # Adjust to remove background
|
| 133 |
right_crop = img.width - 0
|
| 134 |
|
| 135 |
+
#cropped_img = img.crop((left_crop, header_height, right_crop, img.height))
|
| 136 |
+
cropped_img = crop_based_on_bg(img, left_crop, header_height, right_crop, img.height)
|
| 137 |
return cropped_img
|
| 138 |
|
|
|
|
|
|
|
| 139 |
@tool
|
| 140 |
def get_joke() -> str:
|
| 141 |
"""
|