简体   繁体   中英

How can I let users download an image file onto their computer

So I have this project that I want to build for users to remove a background from an image. The process is supposed to be simple: User uploads an image -> The image got processed(with background removed) -> User download the processed image.

Now the problem I am facing is the downloading part. When the image the user uploaded is finished processing, the processed version is saved onto the program directory but I want it to be downloadable to the user's computer/device instead.

The processed image is gotten as a response. I am able to write the response.content to a file but my goal is to allow a user to download it on their device. I have tried using the Pillow(PIL) library but i could't find a way still.

Please help out.

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)

I managed to find a way to solve the problem. I used cloudinary api to upload the processed image and there allow the users to download the image through the api response

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM