

Another possible option would be to use ombytes(), similar to this (), however, without creating a new image object. If you prefer using async def, creating a new BytesIO() stream after reading the file bytes using await file.read() would also be feasible. In that case, please make sure to define the endpoint with normal def instead of async def (see this answer for more details on that topic). file attribute of UploadFile (see the linked answers above for more details). You could instead pass the actual file object to Image.open(), using the. Note that using a BytesIO stream, as shown in your question, would not be necessary.

Heavily based on this answer and this answer.
#PIL IMAGE RESIZE COMMANG CODE#
If you guys have some recommendations for my code please let me know. I tried to use image.resize, but it doesn't work. Image = read_file_as_image(await file.read()) Image = np.array(Image.open(BytesIO(data))) MODEL = tf._model("D:/Works/M.6/New Soil/Classification/Model/SoilModel.h5")ĬLASS_NAMES = ["Clay", "Loam", def ping():ĭef read_file_as_image(data) -> np.ndarray: How can I resize the image after uploading and before getting predictions? from fastapi import FastAPI, File, UploadFileįrom import CORSMiddleware But when I upload the image file, it can not make predictions, because my model is setting the image as (256,256), but the image that gets uploaded is not (256,256). I would like to create a FastAPI application for my image classification model.
