備忘錄_20160105(定位) 修改 回首頁

程式 2025-01-14 00:10:31 1736784631 100
運用 nadermx/backgroundremover 來批次去背

運用 nadermx/backgroundremover 來批次去背


from backgroundremover.bg import remove

def remove_bg(src_img_path, out_img_path):
    model_choices = ["u2net", "u2net_human_seg", "u2netp"]
    f = open(src_img_path, "rb")
    data = f.read()
    img = remove(data, model_name=model_choices[0],
                 alpha_matting=False,
                 alpha_matting_foreground_threshold=240,
                 alpha_matting_background_threshold=10,
                 alpha_matting_erode_structure_size=10,
                 alpha_matting_base_size=1000)
    f.close()
    f = open(out_img_path, "wb")
    f.write(img)
    f.close()


srcPath="D:/temp/20250113/images001/"
dstPath="D:/temp/20250113/images003/"

for i in range(524):
    number=('0000'+str(i+1))[-4:]
    srcFile=srcPath+number+".png"
    dstFile=dstPath+number+".png"
    remove_bg(srcFile, dstFile)
    print(dstFile)