I'm trying to send two images to my API and then write them to a folder. However when I try to save the image, I get the following error:
AttributeError: type object 'Image' has no attribute 'fromarray'
This is my API function that should receive an image and save it to a specific folder.
@app.route("/inpaint", methods=["POST"])
async def inpaint(request):
data = await request.form()
img_bytes = await(data["file"].read())
mask_bytes = await(data["mask"].read())
img = open_image(BytesIO(img_bytes))
mask = open_image(BytesIO(mask_bytes))
print("Inpainting...")
current_time = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
img_dir = os.path.join(os.getcwd(), current_time)
print(img_dir)
mask_dir = os.path.join(os.getcwd(), current_time + '_mask')
create_dir(img_dir)
create_dir(mask_dir)
result_image = Image.fromarray((img * 255).astype(numpy.uint8))
result_mask = Image.fromarray((mask * 255).astype(numpy.uint8))
result_image.save(img_dir, 'PNG')
Any idea what I'm doing wrong?
Regards
EDIT1:
Full code:
from starlette.applications import Starlette
from starlette.responses import HTMLResponse, JSONResponse
from starlette.staticfiles import StaticFiles
from starlette.middleware.cors import CORSMiddleware
import uvicorn, aiohttp, asyncio
import json
from io import BytesIO
from src.config import Config as newConfig
from src.edge_connect import EdgeConnect
from src.utils import create_dir, imsave
from test import main
import os, datetime
import time
from PIL import Image as pimage
from fastai import *
from fastai.vision import *
from fastai.version import __version__
print(dir(Image))
app = Starlette()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_headers=["X-Requested-With", "Content-Type"],
)
app.mount("/static", StaticFiles(directory="app/static"))
@app.route("/")
def index(request):
html = path / "view" / "index.html"
return HTMLResponse(html.open().read())
@app.route("/inpaint", methods=["POST"])
async def inpaint(request):
data = await request.form()
img_bytes = await(data["file"].read())
mask_bytes = await(data["mask"].read())
img = open_image(BytesIO(img_bytes))
mask = open_image(BytesIO(mask_bytes))
print("Inpainting...")
current_time = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
img_dir = os.path.join(os.getcwd(), current_time)
print(img_dir)
mask_dir = os.path.join(os.getcwd(), current_time + '_mask')
create_dir(img_dir)
create_dir(mask_dir)
result_image = pimage.fromarray((img * 255).astype(numpy.uint8))
result_mask = pimage.fromarray((mask * 255).astype(numpy.uint8))
result_image.save(img_dir, 'PNG')
#main(mode=2, image_path=img_dir, mask_path=mask_dir)
if __name__ == "__main__":
if "serve" in sys.argv:
uvicorn.run(app, host="0.0.0.0", port=5042)
result of print(dir(Image))
:
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_maybe_add_crop_pad', '_repr_image_format', '_repr_jpeg_', '_repr_png_', 'affine', 'affine_mat', 'apply_tfms', 'brightness', 'clone', 'contrast', 'coord', 'crop', 'crop_pad', 'cutout',
'data', 'device', 'dihedral', 'dihedral_affine', 'flip_affine', 'flip_lr', 'flow', 'jitter', 'lighting', 'logit_px', 'pad', 'perspective_warp', 'pixel', 'px', 'refresh', 'resize', 'rgb_randomize', 'rotate', 'save', 'set_sample', 'shape', 'show', 'size', 'skew', 'squish', 'symmetric_warp', 'tilt', 'zoom', 'zoom_squish']
I tried to from PIL import Image as pimage
and then do result_image = pimage.fromarray((img * 255).astype(numpy.uint8))
but still no success