Reviewed-on: #10 Co-authored-by: Mathias Scheider <mathias2784@web.de>
This commit was merged in pull request #10.
This commit is contained in:
+21
-8
@@ -1,15 +1,28 @@
|
||||
import base64
|
||||
from io import BytesIO
|
||||
from pathlib import Path
|
||||
|
||||
from PIL import Image
|
||||
|
||||
|
||||
class Photo:
|
||||
|
||||
def __init__(self): ...
|
||||
def __init__(self, path: Path):
|
||||
self.path = path
|
||||
self.image = Image.open(path)
|
||||
|
||||
@classmethod
|
||||
def from_file(cls, path: Path) -> Photo:
|
||||
# TODO
|
||||
return cls()
|
||||
|
||||
def resize(self) -> Photo:
|
||||
# TODO
|
||||
def resize(self, image_max_size=1024) -> Photo:
|
||||
"""Limiting max dimensions while maintaining aspect ratio"""
|
||||
self.image.thumbnail((image_max_size, image_max_size), Image.Resampling.LANCZOS)
|
||||
return self
|
||||
|
||||
def to_rgb(self):
|
||||
if self.image.mode != "RGB":
|
||||
self.image = self.image.convert("RGB")
|
||||
return self
|
||||
|
||||
def get_encoded(self):
|
||||
buffer = BytesIO()
|
||||
self.image.save(buffer, format="JPEG", quality=85)
|
||||
image_data = base64.b64encode(buffer.getvalue()).decode("utf-8")
|
||||
return image_data
|
||||
|
||||
Reference in New Issue
Block a user