Skip to content

Commit

Permalink
compress images on serverside
Browse files Browse the repository at this point in the history
  • Loading branch information
Highfire1 committed Nov 28, 2024
1 parent 0886404 commit 7fed77d
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ __pycache__/
*$py.class

**/data
**/venv
**/.venv
*.env

.github/workflows/main.yml
Binary file modified .venv/Scripts/fastapi.exe
Binary file not shown.
5 changes: 4 additions & 1 deletion Dockerfile-api
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
FROM python:3.13
FROM python:3.13-slim


# install requirements
COPY requirements.txt requirements.txt
# this is awful but the build time inprovements
RUN sed -i 's/fastapi\[standard\]/fastapi/g' requirements.txt

RUN pip install --no-cache-dir --upgrade -r requirements.txt


Expand Down
4 changes: 2 additions & 2 deletions api.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ async def executives_image(filename):
return 404


response = FileResponse(path, headers={"Accept-Encoding": "gzip"})
response = FileResponse(path)
return response

@app.get(
Expand All @@ -149,7 +149,7 @@ async def event_image(filename):
if not os.path.isfile(path):
return 404

response = FileResponse(path, headers={"Accept-Encoding": "gzip"})
response = FileResponse(path)
return response

if __name__ == "__main__":
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ notion-client
fastapi[standard]
pydantic
requests
schedule
schedule
pillow
7 changes: 5 additions & 2 deletions sdk/fetch_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from notion_client import Client
from pydantic import BaseModel
import requests
from sdk.helpers import create_human_readable_date, image_filename_to_url, propTextExtractor, multiplePropTextExtractor, create_folder_if_not_existing
from sdk.helpers import attempt_compress_image, create_human_readable_date, image_filename_to_url, propTextExtractor, multiplePropTextExtractor, create_folder_if_not_existing
import logging

from sdk.models import LCSCEvent
Expand Down Expand Up @@ -110,9 +110,12 @@ def updateDataFromNotion(writeLocation="data/"):
assert file_name.split(".")[-1].lower() in ["webp", "jpg", "png", "jpeg", ".gif"]

if page_updated:
with open(f"data/event_images/{page_id}.{file_extension}", "wb") as fi:
file = f"data/event_images/{page_id}.{file_extension}"
with open(file, "wb") as fi:
r = requests.get(file_url)
fi.write(r.content)

attempt_compress_image(file)

event_images[page_id] = image_filename_to_url("events/images", f"{page_id}.{file_extension}")
else:
Expand Down
7 changes: 5 additions & 2 deletions sdk/fetch_execs.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from notion_client import Client
from pydantic import BaseModel

from sdk.helpers import create_human_readable_date, image_filename_to_url, propTextExtractor, multiplePropTextExtractor
from sdk.helpers import attempt_compress_image, create_human_readable_date, image_filename_to_url, propTextExtractor, multiplePropTextExtractor

logger = logging.getLogger("lcsc.execs")

Expand Down Expand Up @@ -145,11 +145,14 @@ def updateDataFromNotion(writeLocation="data/"):

# don't actually request the image if we know it hasn't changed
if not stale_data:
with open(f"data/exec_images/{student_id}.{file_extension}", "wb") as fi:
file = f"data/exec_images/{student_id}.{file_extension}"
with open(file, "wb") as fi:

r = requests.get(file_url)
fi.write(r.content)

attempt_compress_image(file)

executive_images[student_id] = image_filename_to_url("executives/images", f"{student_id}.{file_extension}")
else:
executive_images[student_id] = None
Expand Down
24 changes: 23 additions & 1 deletion sdk/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from os import makedirs
import os
from os.path import exists
from PIL import Image
Image.MAX_IMAGE_PIXELS = None # pil is paranoid and thinks large images are a zip bomb

# Iterate over Notion pages and extract data
def propTextExtractor(property:dict) -> str | None:
Expand Down Expand Up @@ -91,4 +93,24 @@ def image_filename_to_url(api_route:str, filename:str):
path += "/"

return f"{path}{api_route}/{filename}"


# take care to only call this once per image (ie right after you save it)
def attempt_compress_image(filepath:str):
image = Image.open(filepath)

i_width, i_height = image.size

if i_width > 2000 or i_height > 2000:
# Calculate the new size while maintaining the aspect ratio
if i_width > i_height:
new_width = 2000
new_height = int((i_height * new_width) / i_width)
else:
new_height = 2000
new_width = int((i_width * new_height) / i_height)

# Resize the image
image = image.resize((new_width, new_height), Image.LANCZOS)

# Save the image with optimized settings
image.save(filepath, optimize=True, quality=95)

0 comments on commit 7fed77d

Please sign in to comment.