Spaces:
Sleeping
Sleeping
Update app.py
Browse filestry to remove background by specifying viewport width and height
app.py
CHANGED
@@ -79,37 +79,11 @@ async def capture_screenshot(image_type: str):
|
|
79 |
# await page.wait_for_selector("img", timeout=120000) # Wait for the <img> to be visible
|
80 |
await page.wait_for_timeout(5000) # Allow sketch to fully render
|
81 |
print("Capturing screenshot...")
|
82 |
-
await page.
|
|
|
83 |
await browser.close()
|
84 |
print("Screenshot saved!")
|
85 |
-
|
86 |
-
# def crop_based_on_bg(image_path: str, bg_color=(59, 59, 59), tolerance=10):
|
87 |
-
# img = Image.open(image_path).convert("RGB")
|
88 |
-
# img_array = np.array(img)
|
89 |
-
|
90 |
-
# # Define fixed crop for top header
|
91 |
-
# top_crop = 50
|
92 |
-
# #width, height = img.size
|
93 |
-
|
94 |
-
# # Get height & width
|
95 |
-
# height, width, _ = img_array.shape
|
96 |
-
|
97 |
-
# # Extract the portion of the image below the fixed header
|
98 |
-
# img_no_header = img_array[top_crop:, :, :]
|
99 |
-
|
100 |
-
# # Compute a mask for pixels that are NOT within the background tolerance
|
101 |
-
# lower_bound = np.array(bg_color) - tolerance
|
102 |
-
# upper_bound = np.array(bg_color) + tolerance
|
103 |
-
# mask = np.any((img_no_header < lower_bound) | (img_no_header > upper_bound), axis=2)
|
104 |
-
|
105 |
-
# # Find leftmost and rightmost non-background pixels
|
106 |
-
# col_sums = mask.sum(axis=0)
|
107 |
-
# left_crop = np.argmax(col_sums > 0) # First column with content
|
108 |
-
# right_crop = width - np.argmax(col_sums[::-1] > 0) # Last column with content
|
109 |
-
|
110 |
-
# # Crop and save
|
111 |
-
# cropped_img = img.crop((left_crop, top_crop, right_crop, height))
|
112 |
-
# return cropped_img
|
113 |
|
114 |
@tool
|
115 |
def grab_image(image_type: str) -> Image:
|
@@ -131,14 +105,10 @@ def grab_image(image_type: str) -> Image:
|
|
131 |
|
132 |
print("Loading image for Gradio...")
|
133 |
|
134 |
-
#cropped_img = crop_based_on_bg("img.png", (59, 59, 59), 10)
|
135 |
img = Image.open("img.png")
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
cropped_img = img.crop((left_crop, header_height, right_crop, img.height))
|
141 |
-
return cropped_img
|
142 |
|
143 |
@tool
|
144 |
def get_joke() -> str:
|
|
|
79 |
# await page.wait_for_selector("img", timeout=120000) # Wait for the <img> to be visible
|
80 |
await page.wait_for_timeout(5000) # Allow sketch to fully render
|
81 |
print("Capturing screenshot...")
|
82 |
+
await page.set_viewport_size({"width": 800, "height": 800})
|
83 |
+
await page.locator("iframe").screenshot(path="img.png")
|
84 |
await browser.close()
|
85 |
print("Screenshot saved!")
|
86 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
|
88 |
@tool
|
89 |
def grab_image(image_type: str) -> Image:
|
|
|
105 |
|
106 |
print("Loading image for Gradio...")
|
107 |
|
|
|
108 |
img = Image.open("img.png")
|
109 |
+
|
110 |
+
|
111 |
+
return img
|
|
|
|
|
|
|
112 |
|
113 |
@tool
|
114 |
def get_joke() -> str:
|