Exemplo n.º 1
0
    def from_pil(img: PILImage,
                 uri: Union[str, Path],
                 format: str = None,
                 **kwargs) -> Image:
        """Create an image in memory from a :py:class:`PIL.Image`.

        Parameters
        ----------
        img : :py:class:`PIL.Image`
            An PIL Image instance
        uri : str or Path
            The URI to store the image externally.
        format : str, optional
            The image format to save as. See
            `supported formats <https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.save>`_ for details.
        kwargs : dict, optional
            Optional arguments to pass to `PIL.Image.save <https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.save>`_.
        """  # noqa: E501
        parsed = urlparse(normalize_uri(uri))
        if parsed.scheme == "file":
            img.save(uri, format=format, **kwargs)
        else:
            with NamedTemporaryFile() as fobj:
                img.save(fobj, format=format, **kwargs)
                fobj.flush()
                copy(fobj.name, uri)
        return Image(uri)
Exemplo n.º 2
0
    def from_pil(img: PILImage, uri: Union[str, Path]) -> Image:
        """Create an image in memory from a :py:class:`PIL.Image`.

        Parameters
        ----------
        img : :py:class:`PIL.Image`
            An PIL Image instance
        uri : str or Path
            The URI to store the image externally.
        """
        parsed = urlparse(normalize_uri(uri))
        if parsed.scheme == "file":
            img.save(uri)
        else:
            with NamedTemporaryFile() as fobj:
                img.save(fobj)
                fobj.flush()
                copy(fobj.name, uri)
        return Image(uri)