1

我正在尝试将两个图像发送到我的 API,然后将它们写入一个文件夹。但是,当我尝试保存图像时,出现以下错误:

AttributeError: type object 'Image' has no attribute 'fromarray'

这是我的 API 函数,它应该接收图像并将其保存到特定文件夹。

@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')

知道我做错了什么吗?

问候

编辑1:

完整代码:

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)

结果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']

我试过from PIL import Image as pimage然后做result_image = pimage.fromarray((img * 255).astype(numpy.uint8))但仍然没有成功

4

0 回答 0