Slugy, webp, hromada zmen

This commit is contained in:
Michal Kváček
2024-01-13 23:29:24 +01:00
parent 3b1fd0351a
commit dabed327dd
26 changed files with 517 additions and 180 deletions
+25 -3
View File
@@ -1,3 +1,5 @@
import os.path
from time import time
from database import models
from database.transaction import get_session
from utils.image import PhotoEditor
@@ -6,14 +8,34 @@ from utils.image import PhotoEditor
async def resize_photo(path: str, filename: str, photo_id: int):
editor = PhotoEditor(path, filename)
editor.resize(new_width=2500)
editor.write_to_file(quality=85)
name, _ = os.path.splitext(filename)
editor.write_to_file(quality=85, format_="webp", dest_filename=f"{name}.webp")
editor.write_to_file(quality=85) # toto je potreba pro prvni nacteni nahledu ihned po nahrani, pripadne pro
# vygenerovani nahledu (async, muze se delat pred/behem zmensovani fotky -> v tu dobu jeste neexistuje webp)
width, height = editor.img_size
# TODO: doresit uklid JPGu -> jsou zbytecne
async with get_session() as db:
await models.Photo.update(db, {"width": width, "height": height}, id=photo_id)
await models.Photo.update(
db,
id=photo_id,
data={
"width": width,
"height": height,
"filename_extension": "webp",
"cache_key": int(time())
})
async def generate_thumbnail(path: str, filename: str):
editor = PhotoEditor(path, filename)
editor.resize(new_width=300)
editor.write_to_file(quality=85, dest_path=f"{path}/thumbs")
name, _ = os.path.splitext(filename)
editor.write_to_file(
quality=85,
dest_path=f"{path}/thumbs",
dest_filename=f"{name}.webp",
format_="webp")
+1
View File
@@ -13,6 +13,7 @@ async def download_weather(date_time: datetime, flight_id: int, airport_id: int,
try:
weather = await weather_api.get_weather_for_hour(date_time.astimezone(), gps=gps)
log.warning(weather)
except Exception as e:
log.error(f"Error in downloading weather: {e}")
return None