Render NumPy array in FastAPI

Option 1 – Return image as bytes The below examples show how to convert an image loaded from disk, or an in-memory image (numpy array), into bytes (using either PIL or OpenCV libraries) and return them using a custom Response. For the purposes of this demo, the below code is used to create the in-memory … Read more

PIL cannot identify image file for io.BytesIO object

(This solution is from the author himself. I have just moved it here.) SOLUTION: # This portion is part of my test code byteImgIO = io.BytesIO() byteImg = Image.open(“some/location/to/a/file/in/my/directories.png”) byteImg.save(byteImgIO, “PNG”) byteImgIO.seek(0) byteImg = byteImgIO.read() # Non test code dataBytesIO = io.BytesIO(byteImg) Image.open(dataBytesIO) The problem was with the way that Image.tobytes()was returning the byte object. … Read more