Compare commits
2 commits
6a8eed7a07
...
ba4050d65e
Author | SHA1 | Date | |
---|---|---|---|
ba4050d65e | |||
d2db021c95 |
2 changed files with 19 additions and 6 deletions
22
app/main.py
22
app/main.py
|
@ -2,11 +2,20 @@ import random
|
||||||
import shutil
|
import shutil
|
||||||
import string
|
import string
|
||||||
import os
|
import os
|
||||||
|
from io import BytesIO
|
||||||
|
|
||||||
from fastapi import FastAPI, UploadFile, Query
|
import numpy as np
|
||||||
|
from PIL import Image
|
||||||
|
from fastapi import FastAPI, UploadFile
|
||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
from pydantic import BaseModel
|
|
||||||
|
|
||||||
|
def find_average_hex_color(image: Image.Image):
|
||||||
|
np_image = np.array(image)
|
||||||
|
average_color = np.mean(np_image, axis=(0, 1))
|
||||||
|
average_color_hex = '#{:02x}{:02x}{:02x}'.format(int(average_color[0]), int(average_color[1]), int(average_color[2]))
|
||||||
|
return average_color_hex
|
||||||
|
|
||||||
|
|
||||||
def generate_random_string(length=16):
|
def generate_random_string(length=16):
|
||||||
|
@ -41,11 +50,16 @@ app.mount("/static", StaticFiles(directory="app/static"), name="static")
|
||||||
|
|
||||||
@app.post('/upload_avatar', response_model=dict[str, str])
|
@app.post('/upload_avatar', response_model=dict[str, str])
|
||||||
async def upload_avatar(file: UploadFile):
|
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)
|
||||||
|
average_color_hex = find_average_hex_color(resized_image)
|
||||||
name = generate_random_string()
|
name = generate_random_string()
|
||||||
image_url = f'static/images/avatars/{name}_avatar.png'
|
image_url = f'static/images/avatars/{name}_avatar.png'
|
||||||
with open('app/' + image_url, 'wb+') as file_object:
|
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}
|
return {'image_url': image_url, 'hex_color': average_color_hex}
|
||||||
|
|
||||||
|
|
||||||
@app.post('/upload_image', response_model=dict[str, str])
|
@app.post('/upload_image', response_model=dict[str, str])
|
||||||
|
|
|
@ -47,6 +47,7 @@ mccabe==0.7.0
|
||||||
multidict==6.0.4
|
multidict==6.0.4
|
||||||
mypy-extensions==1.0.0
|
mypy-extensions==1.0.0
|
||||||
nodeenv==1.8.0
|
nodeenv==1.8.0
|
||||||
|
numpy==1.26.4
|
||||||
orjson==3.9.12
|
orjson==3.9.12
|
||||||
packaging==23.2
|
packaging==23.2
|
||||||
passlib==1.7.4
|
passlib==1.7.4
|
||||||
|
@ -98,5 +99,3 @@ WTForms==3.1.2
|
||||||
yarl==1.9.4
|
yarl==1.9.4
|
||||||
zope.event==5.0
|
zope.event==5.0
|
||||||
zope.interface==6.1
|
zope.interface==6.1
|
||||||
pywin32==305; platform_system=="Windows"
|
|
||||||
bcrypt==4.1.2; platform_system=="Windows"
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue