1
# Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022-2024)
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
7
# http://www.apache.org/licenses/LICENSE-2.0
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.
16
from typing import TYPE_CHECKING, Any
19
from PIL import Image, ImageDraw
24
import numpy.typing as npt
27
def create_gif(size, frames=1):
28
# Create grayscale image.
29
im = Image.new("L", (size, size), "white")
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):
38
draw = ImageDraw.Draw(frame)
40
circle_size = size / 2
41
draw.ellipse([pos, tuple(p + circle_size for p in pos)], "black")
42
images.append(frame.copy())
44
# Save the frames as an animated GIF
50
append_images=images[1:],
54
return data.getvalue()
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)
61
st.image(img, caption="Black Square as JPEG", output_format="JPEG", width=100)
63
st.image(img, caption="Black Square as PNG", output_format="PNG", width=100)
65
st.image(img, caption="Black Square with no output format specified", width=100)
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)
70
col1, col2, col3 = st.columns(3)
72
col2.image(img, use_column_width="auto") # 100
74
col2.image(img, use_column_width="never") # 100
75
col2.image(img, use_column_width=False) # 100
77
col2.image(img, use_column_width="always") # column
78
col2.image(img, use_column_width=True) # column
80
col2.image(img800, use_column_width="auto") # column
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>
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>
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)