如何让用户将图像文件下载到他们的计算机上



所以我有一个项目,我想为用户构建它,以便从图像中删除背景。该过程应该很简单:用户上传图像->已处理图像(去除背景(->用户下载处理后的图像。

现在我面临的问题是下载部分。当用户上传的图像完成处理后,处理后的版本会保存到程序目录中,但我希望它可以下载到用户的计算机/设备上。

得到处理后的图像作为响应。我可以将response.content写入文件,但我的目标是允许用户在设备上下载它。我试过使用Pillow(PIL(库,但仍然找不到方法。

请帮忙。

response = requests.post(
'https://api.remove.bg/v1.0/removebg',
files={'image_file': open(filepath, 'rb')},
data={'size': 'auto'},
headers={'X-Api-Key': 'tMxQo9fLnFG6hrMoiKaezVwH'},
)
if response.status_code == requests.codes.ok:
// this saves the image as file(no-bg.png) onto my working directory
with open('no-bg.png', 'wb') as out:
out.write(response.content)

我设法找到了解决问题的方法。我使用cloudinaryapi上传处理后的图像,并允许用户通过api响应下载图像

相关内容

最新更新