mirror of
https://github.com/tiqi-group/pydase.git
synced 2026-02-14 14:28:40 +01:00
updating backend Image component
- using matplotlib typing instead of Figure class - added some checks and logs
This commit is contained in:
@@ -1,19 +1,16 @@
|
|||||||
import base64
|
import base64
|
||||||
import io
|
import io
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from typing import TYPE_CHECKING, Optional
|
||||||
|
from urllib.request import urlopen
|
||||||
|
|
||||||
import PIL.Image
|
import PIL.Image
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
from urllib.request import urlopen
|
|
||||||
|
|
||||||
from pydase.data_service.data_service import DataService
|
from pydase.data_service.data_service import DataService
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
class Figure:
|
from matplotlib.figure import Figure
|
||||||
"""Mock class for matplotlib.Figure"""
|
|
||||||
|
|
||||||
def savefig(self, format="png"):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class Image(DataService):
|
class Image(DataService):
|
||||||
@@ -36,19 +33,24 @@ class Image(DataService):
|
|||||||
with PIL.Image.open(path) as image:
|
with PIL.Image.open(path) as image:
|
||||||
self._load_from_PIL(image)
|
self._load_from_PIL(image)
|
||||||
|
|
||||||
def load_from_matplotlib_figure(self, fig: Figure, format_: str = "png") -> None:
|
def load_from_matplotlib_figure(self, fig: "Figure", format_: str = "png") -> None:
|
||||||
buffer = io.BytesIO()
|
buffer = io.BytesIO()
|
||||||
fig.savefig(buffer, format=format_)
|
fig.savefig(buffer, format=format_) # type: ignore
|
||||||
value_ = base64.b64encode(buffer.getvalue())
|
value_ = base64.b64encode(buffer.getvalue())
|
||||||
self._load_from_base64(value_, format_)
|
self._load_from_base64(value_, format_)
|
||||||
|
|
||||||
def load_from_url(self, url: str):
|
def load_from_url(self, url: str) -> None:
|
||||||
image = PIL.Image.open(urlopen(url))
|
image = PIL.Image.open(urlopen(url))
|
||||||
self._load_from_PIL(image)
|
self._load_from_PIL(image)
|
||||||
|
|
||||||
def load_from_base64(self, value_: bytes, format_: str | None = None) -> None:
|
def load_from_base64(self, value_: bytes, format_: Optional[str] = None) -> None:
|
||||||
if format_ is None:
|
if format_ is None:
|
||||||
format_ = self._get_image_format_from_bytes(value_)
|
format_ = self._get_image_format_from_bytes(value_)
|
||||||
|
if format_ is None:
|
||||||
|
logger.warning(
|
||||||
|
"Format of passed byte string could not be determined. Skipping..."
|
||||||
|
)
|
||||||
|
return
|
||||||
self._load_from_base64(value_, format_)
|
self._load_from_base64(value_, format_)
|
||||||
|
|
||||||
def _load_from_base64(self, value_: bytes, format_: str) -> None:
|
def _load_from_base64(self, value_: bytes, format_: str) -> None:
|
||||||
@@ -66,10 +68,9 @@ class Image(DataService):
|
|||||||
else:
|
else:
|
||||||
logger.error("Image format is 'None'. Skipping...")
|
logger.error("Image format is 'None'. Skipping...")
|
||||||
|
|
||||||
def _get_image_format_from_bytes(self, value_: bytes):
|
def _get_image_format_from_bytes(self, value_: bytes) -> str | None:
|
||||||
image_data = base64.b64decode(value_)
|
image_data = base64.b64decode(value_)
|
||||||
# Create a writable memory buffer for the image
|
# Create a writable memory buffer for the image
|
||||||
image_buffer = io.BytesIO(image_data)
|
image_buffer = io.BytesIO(image_data)
|
||||||
# Read the image from the buffer
|
# Read the image from the buffer and return format
|
||||||
image = PIL.Image.open(image_buffer)
|
return PIL.Image.open(image_buffer).format
|
||||||
return image.format
|
|
||||||
|
|||||||
Reference in New Issue
Block a user