canvas-demo / helper.py
eagle0504's picture
Create helper.py
47632c6 verified
raw
history blame contribute delete
981 Bytes
import pandas as pd
import streamlit as st
from PIL import Image
def load_background_image(bg_image):
"""
Loads the background image from the uploaded file if provided.
Args:
bg_image: The file uploaded by the user for background.
Returns:
PIL Image object or None if no file is uploaded.
"""
return Image.open(bg_image) if bg_image else None
def display_canvas_data(canvas_result):
"""
Display the image data and JSON object data from the canvas result.
Args:
canvas_result: Result object returned by st_canvas.
"""
if canvas_result.image_data is not None:
st.image(canvas_result.image_data)
if canvas_result.json_data is not None:
objects = pd.json_normalize(canvas_result.json_data["objects"]) # Flatten JSON data
for col in objects.select_dtypes(include=['object']).columns:
objects[col] = objects[col].astype("str")
st.dataframe(objects)