question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to convert the uploaded image to Numpy array?

See original GitHub issue

Example 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:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
tiangolocommented, Dec 27, 2020

Thanks for the help here @includeamin ! 👏 🙇

Thanks for reporting back and closing the issue @pavitrashah 👍

0reactions
maazbincommented, Nov 18, 2022

Thanks for the code. It worked and solved my problem.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found