updated to use new functions
This commit is contained in:
@@ -10,6 +10,7 @@ from pathlib import Path
|
|||||||
from minio import Minio
|
from minio import Minio
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
from pymongo.collection import Collection
|
||||||
|
|
||||||
from shared.data_store import get
|
from shared.data_store import get
|
||||||
from shared.data_store import put
|
from shared.data_store import put
|
||||||
@@ -34,23 +35,60 @@ class VisualCommunication(BaseModel):
|
|||||||
return cls.__name__
|
return cls.__name__
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_file(cls, path: Path, minio_client: Minio) -> VisualCommunication:
|
def upload_image_to_minio(
|
||||||
"""Instantiate from file."""
|
cls,
|
||||||
assert isinstance(path, Path)
|
image: Image.Image,
|
||||||
|
minio_client: Minio,
|
||||||
|
) -> str:
|
||||||
|
"""Upload image to MinIO and return MD5 checksum of hashed image."""
|
||||||
|
assert isinstance(image, Image.Image)
|
||||||
assert isinstance(minio_client, Minio)
|
assert isinstance(minio_client, Minio)
|
||||||
# determine name
|
|
||||||
name = path.stem
|
|
||||||
# open and upload image to minio
|
|
||||||
image = Image.open(path)
|
|
||||||
image.load()
|
|
||||||
buffer = BytesIO()
|
buffer = BytesIO()
|
||||||
image.save(buffer, 'png')
|
image.save(buffer, 'png')
|
||||||
object_name = put(
|
object_name = put(
|
||||||
client=minio_client,
|
client=minio_client,
|
||||||
buffer=buffer,
|
buffer=buffer,
|
||||||
)
|
)
|
||||||
|
return object_name
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_name_and_image(
|
||||||
|
cls,
|
||||||
|
name: str,
|
||||||
|
image: Image.Image,
|
||||||
|
minio_client: Minio,
|
||||||
|
) -> VisualCommunication:
|
||||||
|
"""
|
||||||
|
Instantiate from filename and image
|
||||||
|
that is automatically uploaded to MinIO.
|
||||||
|
"""
|
||||||
|
assert isinstance(name, str)
|
||||||
|
assert isinstance(image, Image.Image)
|
||||||
|
assert isinstance(minio_client, Minio)
|
||||||
|
# upload file to minio
|
||||||
|
object_name = VisualCommunication.upload_image_to_minio(
|
||||||
|
image=image,
|
||||||
|
minio_client=minio_client,
|
||||||
|
)
|
||||||
return VisualCommunication(name=name, object_name=object_name)
|
return VisualCommunication(name=name, object_name=object_name)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_file(cls, path: Path, minio_client: Minio) -> VisualCommunication:
|
||||||
|
"""Instantiate from file."""
|
||||||
|
assert isinstance(path, Path)
|
||||||
|
assert isinstance(minio_client, Minio)
|
||||||
|
# determine name
|
||||||
|
name = path.stem
|
||||||
|
# open image
|
||||||
|
image = Image.open(path)
|
||||||
|
image.load()
|
||||||
|
# instantiate object
|
||||||
|
return VisualCommunication.from_name_and_image(
|
||||||
|
name=name,
|
||||||
|
image=image,
|
||||||
|
minio_client=minio_client,
|
||||||
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def decode_image(cls, content: str) -> Image.Image:
|
def decode_image(cls, content: str) -> Image.Image:
|
||||||
"""Extract image from webencoded content."""
|
"""Extract image from webencoded content."""
|
||||||
@@ -69,6 +107,13 @@ class VisualCommunication(BaseModel):
|
|||||||
im = Image.open(buffer)
|
im = Image.open(buffer)
|
||||||
return im
|
return im
|
||||||
|
|
||||||
|
def save_to_mongo(self, collection: Collection) -> None:
|
||||||
|
"""Save self as document in MongoDB."""
|
||||||
|
res = collection.insert_one(
|
||||||
|
document=self.model_dump(),
|
||||||
|
)
|
||||||
|
assert res.acknowledged
|
||||||
|
|
||||||
def webencoded_image(self, minio_client: Minio) -> str:
|
def webencoded_image(self, minio_client: Minio) -> str:
|
||||||
"""Convert image to be displayed on webpage."""
|
"""Convert image to be displayed on webpage."""
|
||||||
assert isinstance(minio_client, Minio)
|
assert isinstance(minio_client, Minio)
|
||||||
|
|||||||
Reference in New Issue
Block a user