Output image :param src: Source of image. It can be a string specifying image URL, a bytes-like object specifying the binary content of an image or an instance of ``PIL.Image.Image`` :param str title: Image description. :param str width: The width of image. It can be CSS pixels
(src: Union[str, bytes, PILImage], format: str = None, title: str = '', width: str = None,
height: str = None,
scope: str = None, position: int = OutputPosition.BOTTOM)
| 880 | |
| 881 | |
| 882 | def put_image(src: Union[str, bytes, PILImage], format: str = None, title: str = '', width: str = None, |
| 883 | height: str = None, |
| 884 | scope: str = None, position: int = OutputPosition.BOTTOM) -> Output: |
| 885 | """Output image |
| 886 | |
| 887 | :param src: Source of image. It can be a string specifying image URL, a bytes-like object specifying |
| 888 | the binary content of an image or an instance of ``PIL.Image.Image`` |
| 889 | :param str title: Image description. |
| 890 | :param str width: The width of image. It can be CSS pixels (like `'30px'`) or percentage (like `'10%'`). |
| 891 | :param str height: The height of image. It can be CSS pixels (like `'30px'`) or percentage (like `'10%'`). |
| 892 | If only one value of ``width`` and ``height`` is specified, the browser will scale image according to its original size. |
| 893 | :param str format: Image format, optinoal. e.g.: ``png``, ``jpeg``, ``gif``, etc. Only available when `src` is non-URL |
| 894 | :param int scope, position: Those arguments have the same meaning as for `put_text()` |
| 895 | |
| 896 | Example: |
| 897 | |
| 898 | .. exportable-codeblock:: |
| 899 | :name: put_image |
| 900 | :summary: `put_image()` usage |
| 901 | |
| 902 | from pywebio import STATIC_PATH # ..demo-only |
| 903 | img = open(STATIC_PATH + '/image/favicon_open_32.png', 'rb').read() # ..demo-only |
| 904 | img = open('/path/to/some/image.png', 'rb').read() # ..doc-only |
| 905 | put_image(img, width='50px') |
| 906 | |
| 907 | ## ---- |
| 908 | put_image('https://www.python.org/static/img/python-logo.png') |
| 909 | """ |
| 910 | if isinstance(src, PILImage): |
| 911 | format = format or src.format or 'JPEG' |
| 912 | imgByteArr = io.BytesIO() |
| 913 | src.save(imgByteArr, format=format) |
| 914 | src = imgByteArr.getvalue() |
| 915 | |
| 916 | if isinstance(src, (bytes, bytearray)): |
| 917 | b64content = b64encode(src).decode('ascii') |
| 918 | format = '' if format is None else ('image/%s' % format) |
| 919 | format = html.escape(format, quote=True) |
| 920 | src = "data:{format};base64, {b64content}".format(format=format, b64content=b64content) |
| 921 | |
| 922 | width = 'width="%s"' % html.escape(width, quote=True) if width is not None else '' |
| 923 | height = 'height="%s"' % html.escape(height, quote=True) if height is not None else '' |
| 924 | |
| 925 | tag = r'<img src="{src}" alt="{title}" {width} {height}/>'.format(src=src, title=html.escape(title, quote=True), |
| 926 | height=height, width=width) |
| 927 | return put_html(tag, scope=scope, position=position) |
| 928 | |
| 929 | |
| 930 | def put_file(name: str, content: bytes, label: str = None, scope: str = None, |
no test coverage detected
searching dependent graphs…