Spaces:
Runtime error
Runtime error
File size: 1,995 Bytes
fea62a9 1dc4501 fea62a9 |
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 45 46 47 48 49 50 51 52 53 54 55 |
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()
|