1. 在https://www.remove.bg/注册账号
2. 获取API Key
from removebg import RemoveBg from PIL import Image BACKGROUND_COLOR = { "RED": (255, 0, 0, 255), "BLUE": (67, 142, 219, 255), "WHITE": (255, 255, 255, 255) } def retouchImg(original_img, retouched_img, color): rmbg = RemoveBg("NhUopVeTDJ4RgbMJtu246me6", "error.log") rmbg.remove_background_from_img_file(original_img) foreground = Image.open(original_img+"_no_bg.png") background = Image.new("RGBA", foreground.size, color) background.paste(foreground, mask=foreground) background.save(retouched_img) if __name__ == '__main__': retouchImg('test.png', 'test_blue.png', BACKGROUND_COLOR["BLUE"]) retouchImg('test.png', 'test_red.png', BACKGROUND_COLOR["RED"])