(input_image, rembg_session=session, background_color=255)
| 280 | |
| 281 | |
| 282 | def simple_preprocess(input_image, rembg_session=session, background_color=255): |
| 283 | RES = 2048 |
| 284 | input_image.thumbnail([RES, RES], Image.Resampling.LANCZOS) |
| 285 | if input_image.mode != 'RGBA': |
| 286 | image_rem = input_image.convert('RGBA') |
| 287 | input_image = remove(image_rem, alpha_matting=False, session=rembg_session) |
| 288 | |
| 289 | arr = np.asarray(input_image) |
| 290 | alpha = np.asarray(input_image)[:, :, -1] |
| 291 | x_nonzero = np.nonzero((alpha > 60).sum(axis=1)) |
| 292 | y_nonzero = np.nonzero((alpha > 60).sum(axis=0)) |
| 293 | x_min = int(x_nonzero[0].min()) |
| 294 | y_min = int(y_nonzero[0].min()) |
| 295 | x_max = int(x_nonzero[0].max()) |
| 296 | y_max = int(y_nonzero[0].max()) |
| 297 | arr = arr[x_min: x_max, y_min: y_max] |
| 298 | input_image = Image.fromarray(arr) |
| 299 | input_image = expand2square(input_image, (background_color, background_color, background_color, 0)) |
| 300 | return input_image |
| 301 | |
| 302 | def init_target(img_pils, new_bkgd=(0., 0., 0.), device="cuda"): |
| 303 | # Convert the background color to a PyTorch tensor |
no test coverage detected