    def _from_sample(self, sample: ImageType | str) -> ImageType:
        """
        Create a :class:`~bentoml._internal.io_descriptors.image.Image` IO Descriptor from given inputs.

        Args:
            sample: Given File-like object, or a path to a file.
            pilmode: Optional color mode for PIL. Default to ``RGB``.
            mime_type: The MIME type of the file type that this descriptor should return.
                       If not specified, then ``from_sample`` will try to infer the MIME type
                       from file extension.
            allowed_mime_types: An optional list of MIME types to restrict input to.

        Returns:
            :class:`~bentoml._internal.io_descriptors.image.Image`: IODescriptor from given users inputs.

        Example:

        .. code-block:: python
           :caption: `service.py`

           from __future__ import annotations

           import bentoml
           from typing import Any
           from bentoml.io import Image
           import numpy as np

           input_spec = Image.from_sample("/path/to/image.jpg")
           @svc.api(input=input_spec, output=Image())
           async def predict(input: t.IO[t.Any]) -> t.IO[t.Any]:
               return await runner.async_run(input)

        Raises:
            :class:`InvalidArgument`: If the given sample is not a valid image type.
            :class:`MissingDependencyException`: If ``filetype`` is not installed.
            :class:`BadInput`: Given sample from file can't be parsed with Pillow.
        """
        try:
            from filetype.match import image_match
        except ModuleNotFoundError:
            raise MissingDependencyException(
                "'filetype' is required to use 'from_sample'. Install it with 'pip install bentoml[io-image]'."
            )

        img_type = image_match(sample)
        if img_type is None:
            raise InvalidArgument(f"{sample} is not a valid image file type.")

        if LazyType["ext.NpNDArray"]("numpy.ndarray").isinstance(sample):
            sample = PIL.Image.fromarray(sample)
        elif isinstance(sample, str):
            p = resolve_user_filepath(sample, ctx=None)
            try:
                with open(p, "rb") as f:
                    sample = PIL.Image.open(f)
            except PIL.UnidentifiedImageError as err:
                raise BadInput(f"Failed to parse sample image file: {err}") from None
        self._mime_type = img_type.mime
        return sample
Create a :class:`~bentoml._internal.io_descriptors.image.Image` IO Descriptor from given inputs.

Args:
sample: Given File-like object, or a path to a file.
pilmode: Optional color mode for PIL. Default to ``RGB``.
mime_type: The MIME type of the file type that this descriptor should return.
If not specified, then ``from_sample`` will try to infer the MIME type
from file extension.
allowed_mime_types: An optional list of MIME types to restrict input to.

Returns:
:class:`~bentoml._internal.io_descriptors.image.Image`: IODescriptor from given users inputs.

Example:

.. code-block:: python
:caption: `service.py`

from __future__ import annotations

import bentoml
from typing import Any
from bentoml.io import Image
import numpy as np

input_spec = Image.from_sample("/path/to/image.jpg")
@svc.api(input=input_spec, output=Image())
async def predict(input: t.IO[t.Any]) -> t.IO[t.Any]:
return await runner.async_run(input)

Raises:
:class:`InvalidArgument`: If the given sample is not a valid image type.
:class:`MissingDependencyException`: If ``filetype`` is not installed.
:class:`BadInput`: Given sample from file can't be parsed with Pillow.
src/bentoml/_internal/io_descriptors/image.py