File size: 1,180 Bytes
3ce4ca8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from pathlib import Path

import streamlit as st
import numpy as np


@st.experimental_singleton  # type: ignore
def get_image_list():

    image_dir = Path(__file__).parent.resolve() / "images"
    return list(image_dir.glob("*.png"))


def overflow_text(text: str, max_width: int):
    return text if len(text) <= max_width - 3 else f"{text[:max_width - 3]}..."


def read_png(path: Path):
    from png import Reader

    with path.open("rb") as f:
        reader = Reader(file=f)
        width, height, pixels, metadata = reader.read_flat()
        data = np.array(pixels).reshape(height, width, metadata["planes"])
        parameters = (metadata["text"] or {}).get("parameters")
        return width, height, data, parameters


image_list = get_image_list()

with st.sidebar:
    image_path = st.radio(
        "Select an image", image_list, format_func=lambda p: overflow_text(p.name, 30)
    )

if image_path:
    width, height, image, parameters = read_png(image_path)
    st.image(
        image, use_column_width="auto", caption=f"{image_path.name} ({width}x{height})"
    )
    if parameters:
        st.write("Parameters:")
        st.code(parameters, language="text")