awacke1's picture
Update app.py
a184f51
raw
history blame
4.05 kB
import os
from io import BytesIO
import streamlit as st
import svgwrite
from PIL import Image
DEFAULT_CARD_WIDTH = 75
DEFAULT_CARD_HEIGHT = 100
PARTS = {"background": ["white", "black", "red", "blue", "green", "yellow"],
"suit": ["clubs", "diamonds", "hearts", "spades"],
"value": ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]}
LEVELS = ["common", "uncommon", "rare", "epic", "legendary", "mythic", "godlike"]
def draw_card(background, suit, value, level, card_width, card_height):
dwg = svgwrite.Drawing(size=(f"{card_width}px", f"{card_height}px"))
dwg.add(dwg.rect((0, 0), (card_width, card_height), rx=10, ry=10, fill=background, stroke="black", stroke_width=2))
suit = svgwrite.text.Text(suit.upper(), insert=(5, 15), fill="black", font_size="16px", font_weight="bold")
dwg.add(suit)
value = svgwrite.text.Text(value, insert=(5, card_height - 10), fill="black", font_size="16px", font_weight="bold")
dwg.add(value)
level = svgwrite.text.Text(level.upper(), insert=(card_width - 50, card_height - 10), fill="black", font_size="12px", font_weight="bold")
dwg.add(level)
return dwg.tostring()
def display_parts_selection():
selected_parts = {}
for part, options in PARTS.items():
selected_parts[part] = st.sidebar.selectbox(f"Select {part}", options)
return selected_parts
def display_level_selection():
selected_level = st.sidebar.selectbox("Select Level", LEVELS)
return selected_level
def display_size_selection(default_width, default_height):
card_width = st.sidebar.slider("Card Width", min_value=50, max_value=150, value=default_width, step=5)
card_height = st.sidebar.slider("Card Height", min_value=75, max_value=200, value=default_height, step=5)
return card_width, card_height
def display_image_upload(card_width, card_height):
uploaded_file = st.sidebar.file_uploader("Upload Card Background Image", type=["png", "jpg", "jpeg"])
if uploaded_file is not None:
image = Image.open(uploaded_file)
image = image.resize((card_width, card_height))
img_bytes = BytesIO()
image.save(img_bytes, format="PNG")
img_data = img_bytes.getvalue()
img_href = "data:image/png;base64," + img_data.hex()
return img_href
else:
return None
def display_card(selected_parts, selected_level, card_width, card_height, background):
if background and os.path.exists(background):
background_name = os.path.basename(background)
st.sidebar.markdown(f"**Background Image:** {background_name}")
background_color = "white"
else:
st.sidebar.markdown(f"**Background Image:** {selected_parts['background'].capitalize()}")
background_color = selected_parts["background"]
card_svg = draw_card(background_color,
selected_parts["suit"],
selected_parts["value"],
selected_level,
card_width,
card_height)
# Add background
if background and os.path.exists(background):
img = svgwrite.image.Image(href=background, size=(f"{card_width}px", f"{card_height}px"))
dwg = svgwrite.fromstring(card_svg)
dwg.add(img)
card_svg = dwg.tostring()
st.write(f'<svg viewBox="0 0 {card_width} {card_height}">{card_svg}</svg>', unsafe_allow_html=True)
# Create a download link for the card SVG
svg_content = '<?xml version="1.0" encoding="utf-8" ?>' + card_svg
st.download_button("Download Card as SVG", svg_content, "card.svg", "text/plain")
# Create button to level up card
if selected_level != "godlike":
if st.button("Level Up"):
current_level_index = LEVELS.index(selected_level)
new_level = LEVELS[current_level_index + 1]
selected_level = new_level
st.sidebar.warning(f"Card has been leveled up to {selected_level}")
else:
st.sidebar.warning("This is the highest level")