Spaces:
Running
on
L40S
Running
on
L40S
Update app.py
Browse files
app.py
CHANGED
@@ -44,6 +44,19 @@ def remove_background(input_url):
|
|
44 |
image_path = os.path.join(temp_dir, 'input_image.png')
|
45 |
try:
|
46 |
image = Image.open(input_url)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
image.save(image_path)
|
48 |
except Exception as e:
|
49 |
shutil.rmtree(temp_dir)
|
|
|
44 |
image_path = os.path.join(temp_dir, 'input_image.png')
|
45 |
try:
|
46 |
image = Image.open(input_url)
|
47 |
+
|
48 |
+
# Ensure the image has an alpha channel
|
49 |
+
if image.mode != 'RGBA':
|
50 |
+
image = image.convert('RGBA')
|
51 |
+
|
52 |
+
# Resize the image to a max width of 512 pixels while maintaining aspect ratio
|
53 |
+
max_width = 512
|
54 |
+
if image.width > max_width:
|
55 |
+
aspect_ratio = image.height / image.width
|
56 |
+
new_height = int(max_width * aspect_ratio)
|
57 |
+
image = image.resize((max_width, new_height), Image.ANTIALIAS)
|
58 |
+
|
59 |
+
# Save the resized image
|
60 |
image.save(image_path)
|
61 |
except Exception as e:
|
62 |
shutil.rmtree(temp_dir)
|