Tests / test_and_lint (push) Successful in 14s
Reviewed-on: #14 Co-authored-by: Mathias Scheider <mathias2784@web.de>
38 lines
980 B
Python
38 lines
980 B
Python
import base64
|
|
from io import BytesIO
|
|
from pathlib import Path
|
|
|
|
from PIL import Image
|
|
|
|
from photools3.models import transform_to_image_dict
|
|
|
|
|
|
def _exist_guard(path: Path):
|
|
pass
|
|
|
|
|
|
class Photo:
|
|
|
|
def __init__(self, path: Path):
|
|
self.path = path
|
|
self.image = Image.open(path)
|
|
|
|
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
|
|
|
|
def to_llm_image_url(self):
|
|
return transform_to_image_dict(self.resize().to_rgb().get_encoded())
|