jinkedon commited on
Commit
132b457
Β·
verified Β·
1 Parent(s): 94166a6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -10
app.py CHANGED
@@ -77,17 +77,23 @@ def remove_background():
77
  # Remove background using withoutBG
78
  logger.info("πŸ”„ Removing background...")
79
  result = model.remove_background(img)
80
- logger.info("βœ… Background removed successfully!")
81
 
82
- # Ensure output is RGB with white background
83
- if result.mode == 'RGBA':
84
- # Create white background
85
- background = Image.new('RGB', result.size, (255, 255, 255))
86
- # Paste the image with alpha as mask
87
- background.paste(result, mask=result.split()[3])
88
- result = background
89
- elif result.mode != 'RGB':
90
- result = result.convert('RGB')
 
 
 
 
 
 
91
 
92
  # Convert to PNG bytes
93
  output_buffer = io.BytesIO()
 
77
  # Remove background using withoutBG
78
  logger.info("πŸ”„ Removing background...")
79
  result = model.remove_background(img)
80
+ logger.info(f"βœ… Background removed successfully! Mode: {result.mode}, Size: {result.size}")
81
 
82
+ # Convert to RGBA first if not already
83
+ if result.mode != 'RGBA':
84
+ result = result.convert('RGBA')
85
+ logger.info(f"πŸ”„ Converted to RGBA mode")
86
+
87
+ # Create white background and composite
88
+ logger.info("🎨 Creating white background composite...")
89
+ white_bg = Image.new('RGBA', result.size, (255, 255, 255, 255))
90
+
91
+ # Composite the image onto white background
92
+ output = Image.alpha_composite(white_bg, result)
93
+
94
+ # Convert to RGB (remove alpha channel)
95
+ result = output.convert('RGB')
96
+ logger.info(f"βœ… Final image mode: {result.mode}")
97
 
98
  # Convert to PNG bytes
99
  output_buffer = io.BytesIO()