awacke1's picture
Update app.py
1349f96
raw
history blame
4.12 kB
import os
from io import BytesIO
import streamlit as st
import svgwrite
from PIL import Image
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))
dwg.add(svgwrite.text.Text(suit.upper(), insert=(5, 15), fill="black", font_size="16px", font_weight="bold"))
dwg.add(svgwrite.text.Text(value, insert=(5, card_height - 10), fill="black", font_size="16px", font_weight="bold"))
dwg.add(svgwrite.text.Text(level.upper(), insert=(card_width - 50, card_height - 10), fill="black", font_size="12px", font_weight="bold"))
return dwg.tostring()
def display_parts_selection():
return {part: st.sidebar.selectbox(f"Select {part}", options) for part, options in PARTS.items()}
def display_level_selection():
return st.sidebar.selectbox("Select Level", LEVELS)
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)
if background:
img = svgwrite.image.Image(href=background, size=(f"{card_width}px", f"{card_height}px"))
dwg = svgwrite.fromstring(card_svg)
dwg.add(img)
st.write(f'<svg viewBox="0 0 {card_width} {card_height}">{dwg.tostring()}</svg>', unsafe_allow_html=True)
else:
st.write(f'<svg viewBox="0 0 {card_width} {card_height}">{card_svg.decode("utf-8")}</svg>', unsafe_allow_html=True)
# Create a download link for the card SVG
svg_content = '<?xml version="1.0" encoding="utf-8" ?>' + card_svg.decode("utf-8")
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)
selected_level = LEVELS[current_level_index + 1]
st.sidebar.warning(f"Card has been leveled up to {selected_level}")
def main():
st.set_page_config(page_title="Card Evolution Game", page_icon=":hearts:")
selected_parts = display_parts_selection()
selected_level = display_level_selection()
card_width, card_height = display_size_selection(75, 100)
background = display_image_upload(card_width, card_height)
display_card(selected_parts, selected_level, card_width, card_height, background)
if __name__ == '__main__':
main()