| 266 | |
| 267 | |
| 268 | def expand2square(pil_img, background_color): |
| 269 | width, height = pil_img.size |
| 270 | if width == height: |
| 271 | return pil_img |
| 272 | elif width > height: |
| 273 | result = Image.new(pil_img.mode, (width, width), background_color) |
| 274 | result.paste(pil_img, (0, (width - height) // 2)) |
| 275 | return result |
| 276 | else: |
| 277 | result = Image.new(pil_img.mode, (height, height), background_color) |
| 278 | result.paste(pil_img, ((height - width) // 2, 0)) |
| 279 | return result |
| 280 | |
| 281 | |
| 282 | def simple_preprocess(input_image, rembg_session=session, background_color=255): |