15
"""St.image example."""
21
from PIL import Image, ImageDraw
26
class StreamlitImages(object):
27
def __init__(self, size=200, step=10):
30
self._half = self._size / 2
34
self.generate_image_types()
35
self.generate_image_channel_data()
36
self.generate_bgra_image()
38
self.generate_pseudorandom_image()
39
self.generate_gray_image()
42
def create_image(self):
44
self._image = Image.new("RGB", (self._size, self._size))
45
d = ImageDraw.Draw(self._image)
50
(self._step, self._step),
51
(self._half - self._step, self._half - self._step),
61
(self._half + self._step, self._step),
62
(self._size - self._step, self._half - self._step),
72
(self._half / 2, self._half + self._step),
73
(self._half - self._step, self._size - self._step),
74
(self._step, self._size - self._step),
81
alpha = Image.new("L", self._image.size, "white")
82
d = ImageDraw.Draw(alpha)
85
(self._step * 3, self._step * 3),
86
(self._size - self._step, self._size - self._step),
94
self._image.putalpha(alpha)
96
def generate_image_types(self):
97
for fmt in ("jpeg", "png"):
98
i = self._image.copy()
100
d.text((self._step, self._step), fmt, fill=(0xFF, 0xFF, 0xFF, 0xFF))
105
i.save(data, format=fmt.upper())
106
self._data["image.%s" % fmt] = data.getvalue()
108
def generate_image_channel_data(self):
116
array = np.array(self._image).transpose((2, 0, 1))
118
for idx, name in zip(range(0, 4), ["red", "green", "blue", "alpha"]):
120
img = Image.fromarray(array[idx].astype(np.uint8))
121
img.save(data, format="PNG")
122
self._data["%s.png" % name] = data.getvalue()
124
def generate_bgra_image(self):
126
array = np.array(self._image).transpose((2, 0, 1))
130
np.stack((array[2], array[1], array[0], array[3]))
136
Image.fromarray(bgra).save(data, format="PNG")
137
self._data["bgra.png"] = data.getvalue()
139
def generate_gif(self):
141
im = Image.new("L", (self._size, self._size), "white")
147
for i in range(0, 10):
149
draw = ImageDraw.Draw(frame)
150
pos = (random.randrange(0, self._size), random.randrange(0, self._size))
151
circle_size = random.randrange(10, self._size / 2)
152
draw.ellipse([pos, tuple(p + circle_size for p in pos)], "black")
153
images.append(frame.copy())
161
append_images=images[1:],
166
self._data["circle.gif"] = data.getvalue()
168
def generate_pseudorandom_image(self):
169
w, h = self._size, self._size
170
r = np.array([255 * np.sin(x / w * 2 * np.pi) for x in range(0, w)])
171
g = np.array([255 * np.cos(x / w * 2 * np.pi) for x in range(0, w)])
172
b = np.array([255 * np.tan(x / w * 2 * np.pi) for x in range(0, w)])
174
r = np.tile(r, h).reshape(w, h).astype("uint8")
175
g = np.tile(g, h).reshape(w, h).astype("uint8")
176
b = np.tile(b, h).reshape(w, h).astype("uint8")
178
rgb = np.stack((r, g, b)).transpose(1, 2, 0)
181
Image.fromarray(rgb).save(data, format="PNG")
182
self._data["pseudorandom.png"] = data.getvalue()
184
def generate_gray_image(self):
186
np.tile(np.arange(self._size) / self._size * 255, self._size)
187
.reshape(self._size, self._size)
192
Image.fromarray(gray).save(data, format="PNG")
193
self._data["gray.png"] = data.getvalue()
196
for name, data in self._data.items():
197
Image.open(io.BytesIO(data)).save("/tmp/%s" % name)
199
def get_images(self):
204
si = StreamlitImages()
207
st.header("individual image bytes")
208
filename = "image.png"
209
data = si.get_images().get(filename)
210
st.image(data, caption=filename, output_format="PNG")
213
st.header("list images")
216
for filename, data in si.get_images().items():
218
captions.append(filename)
219
st.image(images, caption=captions, output_format="PNG")
221
st.header("PIL Image")
225
image = Image.open(io.BytesIO(si.get_images()["image.png"]))
226
data.append((image, "PIL Image.open('image.png')"))
227
image = Image.open(io.BytesIO(si.get_images()["image.jpeg"]))
228
data.append((image, "PIL Image.open('image.jpeg')"))
230
(Image.new("RGB", (200, 200), color="red"), "Image.new('RGB', color='red')")
238
st.image(images, caption=captions, output_format="PNG")
240
st.header("Bytes IO Image")
241
image = io.BytesIO(si.get_images()["image.png"])
242
st.image(image, caption=str(type(image)), output_format="PNG")
244
st.header("From a file")
245
st.image("/tmp/image.png", caption="/tmp/image.png", output_format="PNG")
247
st.header("From open")
248
st.image(open("/tmp/image.png", "rb").read(), caption="from read", output_format="PNG")
250
st.header("Numpy arrays")
251
image = Image.open(io.BytesIO(si.get_images()["image.png"]))
252
rgba = np.array(image)
256
data.append((rgba, str(rgba.shape)))
258
data.append((rgba[:, :, 1], str(rgba[:, :, 1].shape)))
261
(np.expand_dims(rgba[:, :, 2], 2), str(np.expand_dims(rgba[:, :, 2], 2).shape))
264
data.append((rgba[:, :, :3], str(rgba[:, :, :3].shape)))
271
st.image(images, caption=captions, output_format="PNG")
277
image = np.fromstring(si.get_images()["image.png"], np.uint8)
279
img = cv2.imdecode(image, cv2.IMREAD_UNCHANGED)
280
st.image(img, output_format="PNG")
285
url = "https://farm1.staticflickr.com/9/12668460_3e59ce4e61.jpg"
286
st.image(url, caption=url, width=200)
291
g = np.zeros((200, 200))
292
b = np.zeros((200, 200))
294
a = (np.random.ranf(size=200)) * 255
295
r = np.array(np.gradient(a)) / 255.0 + 0.5
297
a = np.tile(r, 200).reshape((200, 200))
299
rgb = np.stack((a, g, b)).transpose(1, 2, 0)
300
data.append((rgb, "clamp: rgb image - 0.3"))
302
a = np.tile(r, 200).reshape((200, 200))
303
rgb = np.stack((a, g, b)).transpose(1, 2, 0)
304
data.append((rgb, "rgb image"))
306
a = np.tile(r, 200).reshape((200, 200))
308
rgb = np.stack((a, g, b)).transpose(1, 2, 0)
309
data.append((rgb, "clamp: rgb image + 0.5"))
316
st.image(images, caption=captions, clamp=True, output_format="PNG")