I have a torch tensor which I need to convert to a byte object so that I can pass it to starlette's StreamingResponse which will return a reconstructed image from the byte object. I am trying to convert the tensor and return it like so:
def some_unimportant_function(params):
return_image = io.BytesIO()
torch.save(some_img, return_image)
return_image.seek(0)
return_img = return_image.read()
return StreamingResponse(content=return_img, media_type="image/jpeg")
Run Code Online (Sandbox Code Playgroud)
The below works fine on regular byte objects and my API returns the reconstructed image:
def some_unimportant_function(params):
image = Image.open(io.BytesIO(some_image))
return_image …Run Code Online (Sandbox Code Playgroud)