Аватарки теперь ресайзятся на 512x512
This commit is contained in:
parent
6a8eed7a07
commit
d2db021c95
1 changed files with 9 additions and 3 deletions
12
app/main.py
12
app/main.py
|
@ -2,11 +2,13 @@ import random
|
|||
import shutil
|
||||
import string
|
||||
import os
|
||||
from io import BytesIO
|
||||
|
||||
from fastapi import FastAPI, UploadFile, Query
|
||||
from PIL import Image
|
||||
from fastapi import FastAPI, UploadFile
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
|
||||
def generate_random_string(length=16):
|
||||
|
@ -41,10 +43,14 @@ app.mount("/static", StaticFiles(directory="app/static"), name="static")
|
|||
|
||||
@app.post('/upload_avatar', response_model=dict[str, str])
|
||||
async def upload_avatar(file: UploadFile):
|
||||
content = await file.read()
|
||||
image = Image.open(BytesIO(content))
|
||||
new_size = (512, 512)
|
||||
resized_image = image.resize(new_size)
|
||||
name = generate_random_string()
|
||||
image_url = f'static/images/avatars/{name}_avatar.png'
|
||||
with open('app/' + image_url, 'wb+') as file_object:
|
||||
shutil.copyfileobj(file.file, file_object)
|
||||
resized_image.save(file_object, format="PNG")
|
||||
return {'image_url': image_url}
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue