diff --git a/nicegui/elements/image.py b/nicegui/elements/image.py index 59caab08b..9968c3234 100644 --- a/nicegui/elements/image.py +++ b/nicegui/elements/image.py @@ -10,6 +10,7 @@ class Image(SourceElement, component='image.js'): + PIL_CONVERT_FORMAT = 'PNG' def __init__(self, source: Union[str, Path, PIL_Image] = '') -> None: """Image @@ -23,7 +24,7 @@ def __init__(self, source: Union[str, Path, PIL_Image] = '') -> None: def _set_props(self, source: Union[str, Path]) -> None: if isinstance(source, PIL_Image): - source = image_to_base64(source) + source = pil_to_base64(source, self.PIL_CONVERT_FORMAT) super()._set_props(source) def force_reload(self) -> None: @@ -32,11 +33,11 @@ def force_reload(self) -> None: self.update() -def image_to_base64(pil_image: PIL_Image, image_format: str = 'PNG') -> str: +def pil_to_base64(pil_image: PIL_Image, image_format: str) -> str: """Convert a PIL image to a base64 string which can be used as image source. :param pil_image: the PIL image - :param image_format: the image format (default: 'PNG') + :param image_format: the image format :return: the base64 string """ buffer = io.BytesIO() diff --git a/nicegui/elements/interactive_image.py b/nicegui/elements/interactive_image.py index 0039d9a0f..50fd240d0 100644 --- a/nicegui/elements/interactive_image.py +++ b/nicegui/elements/interactive_image.py @@ -7,13 +7,14 @@ from PIL.Image import Image as PIL_Image from ..events import GenericEventArguments, MouseEventArguments, handle_event -from .image import image_to_base64 +from .image import pil_to_base64 from .mixins.content_element import ContentElement from .mixins.source_element import SourceElement class InteractiveImage(SourceElement, ContentElement, component='interactive_image.js'): CONTENT_PROP = 'content' + PIL_CONVERT_FORMAT = 'PNG' def __init__(self, source: Union[str, Path] = '', *, @@ -62,7 +63,7 @@ def handle_mouse(e: GenericEventArguments) -> None: def _set_props(self, source: Union[str, Path]) -> None: if isinstance(source, PIL_Image): - source = image_to_base64(source) + source = pil_to_base64(source, self.PIL_CONVERT_FORMAT) super()._set_props(source) def force_reload(self) -> None: