Spaces:
Runtime error
Runtime error
import base64 | |
import graphviz as gv | |
import streamlit as st | |
# Define a function that renders the card using Graphviz | |
def render_card(image_path, title, subtitle): | |
# Define the Graphviz graph | |
g = gv.Graph(format='png') | |
g.attr(bgcolor='white', margin='0') | |
# Add the background image to the graph | |
with open(image_path, 'rb') as f: | |
data = f.read() | |
b64_image = base64.b64encode(data).decode() | |
g.node('background', image=f'data:image/png;base64,{b64_image}', shape='box', width='3.5', height='5') | |
# Add the title and subtitle to the graph | |
g.node('title', label=title, fontname='Arial', fontsize='16', shape='none', width='2') | |
g.node('subtitle', label=subtitle, fontname='Arial', fontsize='12', shape='none', width='2') | |
# Add edges between the nodes | |
g.edge('background', 'title', color='white', style='invis') | |
g.edge('background', 'subtitle', color='white', style='invis') | |
# Render the graph and return the PNG data | |
return g.pipe().decode('utf-8') | |
# Define the Streamlit app | |
def app(): | |
st.title('Card Builder Evolver') | |
# Define the file uploader | |
uploaded_file = st.file_uploader('Upload an image', type=['jpg', 'jpeg', 'png']) | |
# Define the card title and subtitle inputs | |
title = st.text_input('Title') | |
subtitle = st.text_input('Subtitle') | |
# Render the card if an image has been uploaded and a title has been provided | |
if uploaded_file and title: | |
image_path = f'tmp/{uploaded_file.name}' | |
with open(image_path, 'wb') as f: | |
f.write(uploaded_file.getbuffer()) | |
card = render_card(image_path, title, subtitle) | |
st.image(card, use_column_width=True) | |
# Define the download button | |
b64_card = base64.b64encode(card.encode()).decode() | |
href = f'<a href="data:image/png;base64,{b64_card}" download="card.png">Download card</a>' | |
st.markdown(href, unsafe_allow_html=True) | |
# Run the Streamlit app | |
if __name__ == '__main__': | |
app() | |