I need to know how to send image back in aiohttp. I coding a server for image resizing. I used aiohttp.web.FileResponse but it required saved file and it's a bit problem (a lot of files need to be saved on harddrive).
Is there any way to do it flexibly (without saving file)? From image bytes maybe or something? I read aiohttp documentation but it didn't do much.
Here what I tried to do:
- FileResponse
Here I've got to save it to send response back
image = tasks.get(key) # PIL.Image
image.save('im_server/pil_{}.jpg'.format(key))
resp = web.FileResponse(f'im_server/pil_{key}.jpg')
return resp
- StreamResponse
When I make request with this code, I've got file back (it's uploading on my computer) but it's not an image. If I try to open it as image, it says file damaged and cannot be opened :(
image = tasks.get(key) # PIL.Image
resp = web.StreamResponse(status=200)
resp.headers['Content-Type'] = 'Image/JPG'
await resp.prepare(request)
await resp.write(image.tobytes())
return resp