How to convert the uploaded image to Numpy array?
See original GitHub issueExample Code To Get an Image
from fastapi import FastAPI, UploadFile, File, Form
app = FastAPI()
@app.post("/")
def read_root(file: bytes = File(...)):
return {"Hello": "World"}
Or
from fastapi import FastAPI, UploadFile, File, Form
app = FastAPI()
@app.post("/")
def read_root(file: UploadFile = File(...)):
return {"Hello": "World"}
Environment
- OS: Windows 10
- FastAPI Version == 0.61.2
- Python version: 3.7
Question
How to convert the uploaded image to Numpy array? So it can be then used in libraries like openCV, tensorflow for Computer Vision or Deep Learning Applications.
Things I have already tried
from fastapi import FastAPI, UploadFile, File, Form
from PIL import Image
from io import BytesIO
import numpy as np
app = FastAPI()
def read_imagefile(data) -> Image.Image:
image = Image.open(BytesIO(data))
return image
@app.post("/")
async def read_root(file: UploadFile = File(...)):
image = read_imagefile(await file.read())
return {"Hello": "World"}
And
from fastapi import FastAPI, UploadFile, File, Form
from PIL import Image
from io import BytesIO
import numpy as np
app = FastAPI()
def load_image_into_numpy_array(data):
return np.array(Image.open(BytesIO(data)))
@app.post("/")
async def read_root(file: UploadFile = File(...)):
image = load_image_into_numpy_array(await file.read())
return {"Hello": "World"}
But I keep getting errors. And none of the solutions in any other thread works.
Errors: AttributeError: 'JpegImageFile' object has no attribute 'read'
or
AttributeError: 'bytes' object has no attribute 'read'
or
AttributeError: 'numpy.ndarray' object has no attribute 'read'
or
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
Issue Analytics
- State:
- Created 3 years ago
- Reactions:4
- Comments:7 (1 by maintainers)
Top Results From Across the Web
Importing Image Data into NumPy Arrays | Pluralsight
NumPy uses the asarray() class to convert PIL images into NumPy arrays. The np.array function also produce the same result. The type function ......
Read more >how to convert an RGB image to numpy array? - Stack Overflow
Try timing the options to load an image to numpy array, they are quite similar. Go for plt.
Read more >PIL Image to NumPy Array - Linux Hint
Open the image we want to convert into an array using the open() method. This function contains the address of the image as...
Read more >Fast import of Pillow images to NumPy / OpenCV arrays
There's an incredible technique that makes it possible to convert Pillow images to NumPy arrays with just two memory copies!
Read more >Convert Image to Array using Python | Aman Kharwal
We can use NumPy to convert images to arrays, but it has no function to read images. So first we need to use...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thanks for the help here @includeamin ! 👏 🙇
Thanks for reporting back and closing the issue @pavitrashah 👍
Thanks for the code. It worked and solved my problem.