Skip to content

Commit

Permalink
Add pillow and using it for image transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
silentworks committed Sep 29, 2023
1 parent c6ea153 commit 5384054
Show file tree
Hide file tree
Showing 5 changed files with 337 additions and 12 deletions.
23 changes: 20 additions & 3 deletions app/notes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import base64
import io
from flask import Blueprint, flash, redirect, render_template, request, url_for
from PIL import Image, ImageOps
from urllib.request import urlopen
from postgrest.exceptions import APIError
from app.forms import NoteForm
from app.supabase import (
Expand Down Expand Up @@ -89,11 +91,26 @@ def edit(note_id):
form = NoteForm(data=note)
image = None
if note["featured_image"] is not None:
# A Supabase PRO plan is required to use image transforms
# r = supabase.storage.from_("featured_image").get_public_url(
# note["featured_image"],
# options={"transform": {"width": 200}},
# )

# Alternatively we can use Pillow to handle our image transform but
# the burden will be on our own server rather than Supabase's and we
# have to write a lot more code to accomplish the transform, you may
# also want to cache this as this action will be performed everytime
# you load an image
r = supabase.storage.from_("featured_image").get_public_url(
note["featured_image"],
options={"transform": {"width": 200}},
note["featured_image"]
)
image = r
url_to_stream = urlopen(r)
img = Image.open(url_to_stream)
img = ImageOps.contain(img, (200, 200))
image_stream = io.BytesIO()
img.save(image_stream, format="png")
image = f"data:image/png;base64, {base64.b64encode(image_stream.getvalue()).decode('utf-8')}"
else:
path = note["featured_image"]

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
"tailwindcss": "^3.3.3"
},
"devDependencies": {
"supabase": "^1.93.0"
"supabase": "^1.99.5"
}
}
Loading

0 comments on commit 5384054

Please sign in to comment.