Advertisement
Rnery

Remove BG

Jan 3rd, 2024 (edited)
1,111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.72 KB | Source Code | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3.  
  4. from cv2 import imread, imwrite, IMREAD_UNCHANGED
  5.  
  6. def apply_white_background_removal(img):
  7.     alpha_channel = img[:, :, 3]
  8.     white_background_mask = (alpha_channel == 255)
  9.     img[white_background_mask] = [0, 0, 0, 0]
  10.     return img
  11.  
  12. def remove_background(img_path, output_path, background_removal_func):
  13.     img = imread(img_path, IMREAD_UNCHANGED)
  14.     img = background_removal_func(img)
  15.     imwrite(output_path, img)
  16.  
  17. if __name__ == "__main__":
  18.     input_image_path = "caminho/para/sua/imagem.png"
  19.     output_image_path = "caminho/para/salvar/nova/imagem_sem_fundo.png"
  20.  
  21.     remove_background(input_image_path, output_image_path, apply_white_background_removal)
  22.  
Tags: python
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement