|
from pathlib import Path |
|
|
|
import streamlit as st |
|
import numpy as np |
|
|
|
|
|
@st.experimental_singleton |
|
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") |
|
|