大家好,我又来了,昨天分享了那个Python包去背景功能,但是效果不是特别好,于是我又去深挖深挖…今天来一个高级版的。

import http.client
import mimetypes
import os
import uuid

# 请替换为您自己的apiKey

apiKey = 'your keyapi'


def remove_background(input_image_path, output_image_path):
    # 定义multipart边界
    boundary = '----------{}'.format(uuid.uuid4().hex)

    # 获取图像的mimetype
    content_type, _ = mimetypes.guess_type(input_image_path)
    if content_type is None:
        content_type = 'application/octet-stream'  # 默认类型

    # 准备POST数据
    with open(input_image_path, 'rb') as f:
        image_data = f.read()
    filename = os.path.basename(input_image_path)
    body = (
               f"--{boundary}\r\n"
               f"Content-Disposition: form-data; name=\"image_file\"; filename=\"{filename}\"\r\n"
               f"Content-Type: {content_type}\r\n\r\n").encode('utf-8') + image_data + f"\r\n--{boundary}--\r\n".encode(
        'utf-8')

    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值