Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Here I am uploading & downloading the color image but am unable to convert multiple color images to grey scale.
It shows some error.

AttributeError: 'function' object has no attribute 'read'


How to do this ?

What I have tried:

from fileinput import filename
from re import L
from fastapi import FastAPI, File, UploadFile
import uvicorn
import aiofiles
from PIL import Image
from fastapi.responses import FileResponse
from typing import List

app = FastAPI()

@app.post("/upload-files")
async def create_upload_files(files: List[UploadFile] = File(...)):
    for file in files:
        destination_file_path = "C:/Users/SMRUTI/Desktop/grey/"+file.filename #storing location
        async with aiofiles.open(destination_file_path, 'wb') as out_file:
            while content := await file.read(1024):  #async read file
                await out_file.write(content)  #async write file
    return FileResponse(destination_file_path)

@app.post("/grey_image/")
async def create_grey_img(files:List[UploadFile]= File(...)):
    for file in files:
        destination_file_path = "C:/Users/SMRUTI/Desktop/grey/"+file.filename #output file path
        async with aiofiles.open(destination_file_path, 'wb') as out_file:
            file= Image.open(filename) 
            file.convert(L)
            while content := await file.read(1024):
                await out_file.write(content)  
    return FileResponse(destination_file_path)
    
if __name__ == '__main__':
    uvicorn.run(app, host='127.0.0.1', port=8000)
    print("running")
Posted
Updated 18-Jul-22 17:12pm
v2
Comments
Richard MacCutchan 13-Jul-22 4:03am    
What is the actual problem?
Member 15667238 14-Jul-22 11:11am    
here am getting the error that attribute error: list object doesnt find filename .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900