FastAPI 图像 POST 并调整大小
FastAPI image POST and resize
我正在使用 FastAPI 创建一个应用程序,该应用程序应该生成上传图像的调整大小版本。
上传应通过 POST/images 完成,调用路径 /images/800x400 后应显示 800x400 大小的图像。
这是我目前所拥有的:
from fastapi import FastAPI, File, UploadFile
app = FastAPI()
@app.post("/images/")
async def create_upload_file(file: UploadFile = File(...)):
return {"filename": file.photo.jpg}
(photo.jpg 是与应用程序文件位于同一位置的图像)
如何查看这张上传的图片?当我打电话给 http://127.0.0.1:8000/images/ 时,我得到:
{“详细信息”:“不允许的方法”}
怎么可能 POST 多张图片,然后通过调用 /images/800x400 调整随机图片的大小以在 800x400 版本中查看它?
我正在考虑使用 Pillow。这种情况下可以吗?
编辑
我现在向前迈进了一步,但在尝试显示图像时仍然遇到错误。
from fastapi.responses import FileResponse
import uuid
app = FastAPI()
db = []
@app.post("/images/")
async def create_upload_file(file: UploadFile = File(...)):
file.filename = f"{uuid.uuid4()}.jpg"
contents = await file.read() # <-- Important!
db.append(file)
# example of how you can save the file
with open(file.filename, "wb") as f:
f.write(contents)
return {"filename": file.filename}
@app.get("/images/")
async def show_image():
return db[0]```
As a response I get:
{
"filename": "70188bdc-923c-4bd3-be15-8e71966cab31.jpg",
"content_type": "image/jpeg",
"file": {}
}
I would like to use: return FileResponse(some_file_path)
and in the file path put the filename from above. Is it right way of thinking?
我相信您需要了解一下 HTTP methods 的用途。
POST
The POST method is used to submit an entity (your image) to the specified resource, often causing a change in state or side effects on the server.
因此,使用 POST 将图像上传到您的后端。
得到
The GET method requests a representation of the specified (image) resource. Requests using GET should only retrieve data.
GET 是使用浏览器导航到站点时的默认方法。因此,如果您想在导航到 http://127.0.0.1:8000/images/
时看到图像,则需要为该端点定义一个函数(使用 FastAPI 的装饰器)。
了解这些,您可以定义实现目标所需的端点。
编辑
作为参考,这里是上传和保存图像的有效实现。我使用 Postman 来执行实际请求。
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import FileResponse
import uuid
app = FastAPI()
@app.post("/images/")
async def create_upload_file(file: UploadFile = File(...)):
file.filename = f"{uuid.uuid4()}.jpg"
contents = await file.read() # <-- Important!
# example of how you can save the file
with open(file.filename, "wb") as f:
f.write(contents)
return {"filename": file.filename}
这会将上传的图片以随机ID作为文件名保存在应用程序所在的目录中运行。当然,您应该使用适当的数据存储,但我认为它应该让您朝着正确的方向前进。
参考资料
https://www.starlette.io/requests/#request-files
我正在使用 FastAPI 创建一个应用程序,该应用程序应该生成上传图像的调整大小版本。 上传应通过 POST/images 完成,调用路径 /images/800x400 后应显示 800x400 大小的图像。 这是我目前所拥有的:
from fastapi import FastAPI, File, UploadFile
app = FastAPI()
@app.post("/images/")
async def create_upload_file(file: UploadFile = File(...)):
return {"filename": file.photo.jpg}
(photo.jpg 是与应用程序文件位于同一位置的图像)
如何查看这张上传的图片?当我打电话给 http://127.0.0.1:8000/images/ 时,我得到: {“详细信息”:“不允许的方法”}
怎么可能 POST 多张图片,然后通过调用 /images/800x400 调整随机图片的大小以在 800x400 版本中查看它? 我正在考虑使用 Pillow。这种情况下可以吗?
编辑
我现在向前迈进了一步,但在尝试显示图像时仍然遇到错误。
from fastapi.responses import FileResponse
import uuid
app = FastAPI()
db = []
@app.post("/images/")
async def create_upload_file(file: UploadFile = File(...)):
file.filename = f"{uuid.uuid4()}.jpg"
contents = await file.read() # <-- Important!
db.append(file)
# example of how you can save the file
with open(file.filename, "wb") as f:
f.write(contents)
return {"filename": file.filename}
@app.get("/images/")
async def show_image():
return db[0]```
As a response I get:
{
"filename": "70188bdc-923c-4bd3-be15-8e71966cab31.jpg",
"content_type": "image/jpeg",
"file": {}
}
I would like to use: return FileResponse(some_file_path)
and in the file path put the filename from above. Is it right way of thinking?
我相信您需要了解一下 HTTP methods 的用途。
POST
The POST method is used to submit an entity (your image) to the specified resource, often causing a change in state or side effects on the server.
因此,使用 POST 将图像上传到您的后端。
得到
The GET method requests a representation of the specified (image) resource. Requests using GET should only retrieve data.
GET 是使用浏览器导航到站点时的默认方法。因此,如果您想在导航到 http://127.0.0.1:8000/images/
时看到图像,则需要为该端点定义一个函数(使用 FastAPI 的装饰器)。
了解这些,您可以定义实现目标所需的端点。
编辑
作为参考,这里是上传和保存图像的有效实现。我使用 Postman 来执行实际请求。
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import FileResponse
import uuid
app = FastAPI()
@app.post("/images/")
async def create_upload_file(file: UploadFile = File(...)):
file.filename = f"{uuid.uuid4()}.jpg"
contents = await file.read() # <-- Important!
# example of how you can save the file
with open(file.filename, "wb") as f:
f.write(contents)
return {"filename": file.filename}
这会将上传的图片以随机ID作为文件名保存在应用程序所在的目录中运行。当然,您应该使用适当的数据存储,但我认为它应该让您朝着正确的方向前进。
参考资料
https://www.starlette.io/requests/#request-files