0

I have two APIs written in Python with Flask. The first one sends a request to the second and is supposed to receive an image file (png). The file is sent from the second with send_file("example.png", mimetype='image/png'). How do I retrieve it from the response? I want to save it when it's received by the first API.

Right now I have a code that is sending a .csv file. On the receiving end it has the following code:

result_file_path = os.path.join(constants.RESULTS_PATH, f"{analysis_task.id}.csv")
with open(result_file_path, 'w', newline='') as result_file:
result_file.write(Response.text)   

I tried doing something like this with the image, but it doesn't work. Response.text is just a unicode text, to the best of my understanding, so how do I access the image file sent? There's no such thing as Response.image. I also tried to use cv2.imwrite on Response.text, but that doesn't work as well.

Plus, an additional question: In this example, the .text is read and then written into a file using Python's csv library (that is imported). So why did it have to be sent as .csv from the second API to begin with?

  • This might help: https://stackoverflow.com/questions/30229231/python-save-image-from-url – Oli Aug 20 '20 at 18:55
  • @oli Thanks, but how should I use it in my case? I don't have a URL of the image, I have it (or am supposed to have it) attached to the response. – Kira Ablamunits Aug 20 '20 at 19:14
  • Aren't your APIs working over HTTP? means are they web APIs – Oli Aug 20 '20 at 19:16
  • They are based on Flask, so yes, they work over HTTP. – Kira Ablamunits Aug 20 '20 at 19:40
  • Great, and if you're already returning image as the response (using image/png mime type) then you can use your APIs URL (which you were hitting to get the image) as your image URL and use `urllib.request.urlretrieve(, 'filename.png')`to get image and save it as filename.png – Oli Aug 20 '20 at 19:48

0 Answers0