streamlit

Форк
0
/
st_image.py 
102 строки · 3.3 Кб
1
# Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022-2024)
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
# you may not use this file except in compliance with the License.
5
# You may obtain a copy of the License at
6
#
7
#     http://www.apache.org/licenses/LICENSE-2.0
8
#
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
# See the License for the specific language governing permissions and
13
# limitations under the License.
14

15
import io
16
from typing import TYPE_CHECKING, Any
17

18
import numpy as np
19
from PIL import Image, ImageDraw
20

21
import streamlit as st
22

23
if TYPE_CHECKING:
24
    import numpy.typing as npt
25

26

27
def create_gif(size, frames=1):
28
    # Create grayscale image.
29
    im = Image.new("L", (size, size), "white")
30

31
    images = []
32

33
    # Make circle of a constant size with a number of frames, moving across the
34
    # principal diagonal of a 64x64 image. The GIF will not loop and stops
35
    # animating after frames x 100ms.
36
    for i in range(0, frames):
37
        frame = im.copy()
38
        draw = ImageDraw.Draw(frame)
39
        pos = (i, i)
40
        circle_size = size / 2
41
        draw.ellipse([pos, tuple(p + circle_size for p in pos)], "black")
42
        images.append(frame.copy())
43

44
    # Save the frames as an animated GIF
45
    data = io.BytesIO()
46
    images[0].save(
47
        data,
48
        format="GIF",
49
        save_all=True,
50
        append_images=images[1:],
51
        duration=1,
52
    )
53

54
    return data.getvalue()
55

56

57
img = np.repeat(0, 10000).reshape(100, 100)
58
img800 = np.repeat(0, 640000).reshape(800, 800)
59
gif = create_gif(64, frames=32)
60

61
st.image(img, caption="Black Square as JPEG", output_format="JPEG", width=100)
62

63
st.image(img, caption="Black Square as PNG", output_format="PNG", width=100)
64

65
st.image(img, caption="Black Square with no output format specified", width=100)
66

67
transparent_img: "npt.NDArray[Any]" = np.zeros((100, 100, 4), dtype=np.uint8)
68
st.image(transparent_img, caption="Transparent Black Square", width=100)
69

70
col1, col2, col3 = st.columns(3)
71
col2.image(img)  # 100
72
col2.image(img, use_column_width="auto")  # 100
73

74
col2.image(img, use_column_width="never")  # 100
75
col2.image(img, use_column_width=False)  # 100
76

77
col2.image(img, use_column_width="always")  # column
78
col2.image(img, use_column_width=True)  # column
79

80
col2.image(img800, use_column_width="auto")  # column
81

82
st.image(
83
    """
84
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="500" height="100">
85
<text x="0" y="50">"I am a quote" - https://avatars.githubusercontent.com/karriebear</text>
86
</svg>
87
"""
88
)
89

90
st.image(
91
    """<?xml version="1.0" encoding="utf-8"?>
92
    <!-- Generator: Adobe Illustrator 17.1.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
93
    <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
94
    <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="500" height="100">
95
    <text x="0" y="50">"I am prefixed with some meta tags</text>
96
    </svg>
97
"""
98
)
99

100
st.image(gif, width=100)
101
st.image(create_gif(64), caption="Black Circle as GIF", width=100)
102
st.image(gif, caption="GIF as PNG", output_format="PNG", width=100)
103

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.