muhammadsalmanalfaridzi
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -52,30 +52,30 @@ def text_image_to_image(image, prompt):
|
|
52 |
modified_image.save(image_path)
|
53 |
return modified_image, image_path
|
54 |
|
55 |
-
def process_images(image_paths):
|
56 |
with ThreadPoolExecutor() as executor:
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
58 |
return results
|
59 |
|
60 |
def get_bounding_box_with_threshold(image, threshold):
|
61 |
-
# Convert image to numpy array
|
62 |
img_array = np.array(image)
|
|
|
63 |
|
64 |
-
# Get alpha channel
|
65 |
-
alpha = img_array[:,:,3]
|
66 |
-
|
67 |
-
# Find rows and columns where alpha > threshold
|
68 |
rows = np.any(alpha > threshold, axis=1)
|
69 |
cols = np.any(alpha > threshold, axis=0)
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
|
|
74 |
|
75 |
-
if left < right and top < bottom
|
76 |
-
return (left, top, right, bottom)
|
77 |
-
else:
|
78 |
-
return None
|
79 |
|
80 |
def check_cropped_sides(image, tolerance):
|
81 |
cropped_sides = []
|
@@ -493,21 +493,19 @@ def create_canvas(canvas_size, bg_choice, custom_color):
|
|
493 |
else: # transparent
|
494 |
return Image.new("RGBA", canvas_size, (0, 0, 0, 0))
|
495 |
|
496 |
-
|
|
|
497 |
watermark = Image.open(watermark_path).convert("RGBA")
|
498 |
-
|
499 |
-
# Get dimensions of both the canvas and watermark
|
500 |
-
canvas_width, canvas_height = canvas.size
|
501 |
-
watermark_width, watermark_height = watermark.size
|
502 |
-
|
503 |
-
# Calculate the position to center the watermark
|
504 |
-
x = (canvas_width - watermark_width) // 2
|
505 |
-
y = (canvas_height - watermark_height) // 2
|
506 |
|
507 |
-
#
|
508 |
-
|
|
|
|
|
|
|
509 |
|
510 |
-
|
|
|
511 |
|
512 |
def save_image(canvas, output_folder, filename, output_format):
|
513 |
output_ext = 'jpg' if output_format == 'JPG' else 'png'
|
|
|
52 |
modified_image.save(image_path)
|
53 |
return modified_image, image_path
|
54 |
|
55 |
+
def process_images(image_paths, remove_bg_method):
|
56 |
with ThreadPoolExecutor() as executor:
|
57 |
+
if remove_bg_method == 'rembg':
|
58 |
+
results = list(executor.map(remove_background_rembg, image_paths))
|
59 |
+
elif remove_bg_method == 'bria':
|
60 |
+
results = list(executor.map(remove_background_bria, image_paths))
|
61 |
+
else: # No background removal
|
62 |
+
results = image_paths # Just return the original paths
|
63 |
return results
|
64 |
|
65 |
def get_bounding_box_with_threshold(image, threshold):
|
|
|
66 |
img_array = np.array(image)
|
67 |
+
alpha = img_array[:, :, 3]
|
68 |
|
|
|
|
|
|
|
|
|
69 |
rows = np.any(alpha > threshold, axis=1)
|
70 |
cols = np.any(alpha > threshold, axis=0)
|
71 |
|
72 |
+
try:
|
73 |
+
top, bottom = np.where(rows)[0][[0, -1]]
|
74 |
+
left, right = np.where(cols)[0][[0, -1]]
|
75 |
+
except IndexError:
|
76 |
+
return None # No non-transparent pixels found
|
77 |
|
78 |
+
return (left, top, right, bottom) if left < right and top < bottom else None
|
|
|
|
|
|
|
79 |
|
80 |
def check_cropped_sides(image, tolerance):
|
81 |
cropped_sides = []
|
|
|
493 |
else: # transparent
|
494 |
return Image.new("RGBA", canvas_size, (0, 0, 0, 0))
|
495 |
|
496 |
+
# Function to add watermark
|
497 |
+
def add_watermark(image, watermark_path):
|
498 |
watermark = Image.open(watermark_path).convert("RGBA")
|
499 |
+
image = image.convert("RGBA")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
500 |
|
501 |
+
# Center the watermark
|
502 |
+
img_width, img_height = image.size
|
503 |
+
wm_width, wm_height = watermark.size
|
504 |
+
x = (img_width - wm_width) // 2
|
505 |
+
y = (img_height - wm_height) // 2
|
506 |
|
507 |
+
image.paste(watermark, (x, y), watermark)
|
508 |
+
return image
|
509 |
|
510 |
def save_image(canvas, output_folder, filename, output_format):
|
511 |
output_ext = 'jpg' if output_format == 'JPG' else 'png'
|