Skip to content

MkBinaryImage

Show source on GitHub

Image carrying the data by itself.

Description

This node basically is a regular image link, but carries the image data by itself. The data will get written to the "virtual" folder at the end of the process. It can hold either str or bytes as data.

Example: From data

Jinja

{% set svg = '<svg width="90pt" height="90pt" version="1.1" viewBox="0 0 15.875 10.583" xmlns="http://www.w3.org/2000/svg">
                  <g fill="none" stroke="#F00" stroke-width=".3">
                      <path d="m6.1295 3.6601 3.2632 3.2632z"/>
                      <path d="m9.3927 3.6601-3.2632 3.2632z"/>
                  </g>
              </svg>' %}

{{ svg | MkBinaryImage(path="some_image.svg", caption="A simple cross") }}

Python

MkBinaryImage(
    '<svg width="90pt" height="90pt" version="1.1" viewBox="0 0 15.875 10.583" xmlns="http://www.w3.org/2000/svg">\n                  <g fill="none" stroke="#F00" stroke-width=".3">\n                      <path d="m6.1295 3.6601 3.2632 3.2632z"/>\n                      <path d="m9.3927 3.6601-3.2632 3.2632z"/>\n                  </g>\n              </svg>',
    "some_image.svg",
    caption="A simple cross",
)

A simple cross

<figure markdown>
  ![](some_image.svg)
  <figcaption>A simple cross</figcaption>
</figure>
<figure>
<p><img alt="" src="some_image.svg">
  </p>
<figcaption>A simple cross</figcaption>
</figure>

Example: From icon

Jinja

{{ mk.MkBinaryImage.for_icon("file-image", width=200) }}

Python

MkBinaryImage(
    '<svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 24 24"><path fill="currentColor" d="M13 9h5.5L13 3.5zM6 2h8l6 6v12a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V4c0-1.11.89-2 2-2m0 18h12v-8l-4 4l-2-2zM8 9a2 2 0 0 0-2 2a2 2 0 0 0 2 2a2 2 0 0 0 2-2a2 2 0 0 0-2-2"/></svg>',
    "file_image.svg",
    width=200,
)

![](file_image.svg){ width="200" }
<p><img alt="" src="file_image.svg" width="200"></p>

Bases: MkImage

__init__

__init__(
    data: bytes | str,
    path: str,
    *,
    target: LinkableType | None = None,
    caption: str = "",
    title: str = "",
    align: Literal["left", "right"] | None = None,
    width: int | None = None,
    **kwargs: Any
)

Parameters:

Name Type Description Default
data bytes | str

Image data

required
path str

path for the image (including extension)

required
target LinkableType | None

Optional URL or node the image should link to

None
caption str

Image caption

''
title str

Image title

''
align Literal['left', 'right'] | None

Image alignment

None
width int | None

Image width in pixels

None
kwargs Any

Keyword arguments passed to parent

{}

for_file classmethod

for_file(
    path: str | PathLike[str], storage_options: dict | None = None, **kwargs: Any
) -> Self

Parameters:

Name Type Description Default
path str | PathLike[str]

Path to an image (also takes fsspec protocol URLs)

required
storage_options dict | None

Options for fsspec backend

None
kwargs Any

Keyword arguments passed to constructor

{}

for_icon classmethod

for_icon(icon: str, **kwargs: Any) -> Self

Parameters:

Name Type Description Default
icon str

Icon to get a MkBinaryImage for (example: material/file-image)

required
kwargs Any

Keyword arguments passed to constructor

{}
Name Children Inherits
MkImage
mknodes.basenodes.mkimage
Image including optional caption.
graph TD
  94721311910464["mkbinaryimage.MkBinaryImage"]
  94721307729824["mkimage.MkImage"]
  94721308848336["mknode.MkNode"]
  94721311766592["node.Node"]
  140564252373184["builtins.object"]
  94721307729824 --> 94721311910464
  94721308848336 --> 94721307729824
  94721311766592 --> 94721308848336
  140564252373184 --> 94721311766592
/home/runner/work/mknodes/mknodes/mknodes/basenodes/mkbinaryimage/metadata.toml
[metadata]
icon = "mdi:file-image"
name = "MkBinaryImage"
group = "image"

[examples.from_data]
title = "From data"
jinja = """
{% set svg = '<svg width="90pt" height="90pt" version="1.1" viewBox="0 0 15.875 10.583" xmlns="http://www.w3.org/2000/svg">
                  <g fill="none" stroke="#F00" stroke-width=".3">
                      <path d="m6.1295 3.6601 3.2632 3.2632z"/>
                      <path d="m9.3927 3.6601-3.2632 3.2632z"/>
                  </g>
              </svg>' %}

{{ svg | MkBinaryImage(path="some_image.svg", caption="A simple cross") }}
"""

[examples.from_icon]
title = "From icon"
jinja = """
{{ mk.MkBinaryImage.for_icon("file-image", width=200) }}
"""
mknodes.basenodes.mkbinaryimage.MkBinaryImage
class MkBinaryImage(mkimage.MkImage):
    """Image carrying the data by itself.

    This node basically is a regular image link, but carries the image data by itself.
    The data will get written to the "virtual" folder at the end of the process.
    It can hold either str or bytes as data.
    """

    ICON = "material/file-image"

    def __init__(
        self,
        data: bytes | str,
        path: str,
        *,
        target: linkprovider.LinkableType | None = None,
        caption: str = "",
        title: str = "",
        align: Literal["left", "right"] | None = None,
        width: int | None = None,
        **kwargs: Any,
    ):
        """Constructor.

        Args:
            data: Image data
            path: path for the image (including extension)
            target: Optional URL or node the image should link to
            caption: Image caption
            title: Image title
            align: Image alignment
            width: Image width in pixels
            kwargs: Keyword arguments passed to parent
        """
        super().__init__(
            path=path,
            target=target,
            caption=caption,
            title=title,
            align=align,
            width=width,
            **kwargs,
        )
        self.data = data

    @property
    def files(self) -> dict[str, str | bytes]:
        path = "/".join(self.resolved_parts) + "/" + self.path
        return {path: self.data} | self._files

    @classmethod
    def for_icon(cls, icon: str, **kwargs: Any) -> Self:
        """Return a MkBinaryImage with data for given icon.

        Args:
            icon: Icon to get a MkBinaryImage for (example: material/file-image)
            kwargs: Keyword arguments passed to constructor
        """
        content = iconfilters.get_icon_svg(icon)
        path = f"{textfilters.slugify(icon)}.svg"
        return cls(data=content, path=path, **kwargs)

    @classmethod
    def for_file(
        cls,
        path: str | os.PathLike[str],
        storage_options: dict | None = None,
        **kwargs: Any,
    ) -> Self:
        """Return a MkBinaryImage with data for given icon.

        Args:
            path: Path to an image (also takes fsspec protocol URLs)
            storage_options: Options for fsspec backend
            kwargs: Keyword arguments passed to constructor
        """
        opts = storage_options or {}
        file = upath.UPath(path, **opts)
        return cls(data=file.read_bytes(), path=file.name, **kwargs)