Due to relatively simple, direct on the code ( removebg Interface official website ), more gadgets get
(1) official website API, registration is required to obtain X-Api-Key: removebg_ official website api.py
1 import requests 2 response = requests.post( 3 'https://api.remove.bg/v1.0/removebg', 4 files={'image_file': open('/path/to/file.jpg', 'rb')}, 5 data={'size': 'auto'}, 6 headers={'X-Api-Key': 'INSERT_YOUR_API_KEY_HERE'}, 7 ) 8 9 if response.status_code == requests.codes.ok: 10 with open('no-bg.png', 'wb') as out: 11 out.write(response.content) 12 else: 13 print("Error:", response.status_code, response.text)
(2) a single picture matting removebg_one.py
. 1 # https://www.remove.bg/api 2 # PIP removebg the install . 3 . 4 from removebg Import RemoveBg . 5 rmbg = RemoveBg ( " DG2WMZrZNnU2oG8fb7mzv6Ja " , " the error.log " ) # quotation marks your acquired the API . 6 rmbg. remove_background_from_img_file (r " E: \ Python project \ summary review \ matting removebg_ \ ImagesRF Royalty Free \ 1.jpg " , size = " 4K " ) # picture address
(3) bulk matting removebg_more.py
. 1 from removebg Import RemoveBg 2 Import OS . 3 . 4 rmbg = RemoveBg ( " DG2WMZrZNnU2oG8fb7mzv6Ja " , " the error.log " ) # quotation marks your acquired the API . 5 path = the os.path.join (The os.getcwd (), ' Images ' ) # picture into a program similar images folder inside 6 # Print (os.listdir (path)) 7 for PIC in os.listdir (path): 8 rmbg.remove_background_from_img_file (f " {path} \ {PIC} " )