File size: 2,700 Bytes
f7cbd13
 
23630d2
3da36e1
f7cbd13
23630d2
 
f7cbd13
23630d2
 
 
f7cbd13
23630d2
 
 
 
 
 
f7cbd13
 
23630d2
f7cbd13
 
 
 
 
 
 
23630d2
f7cbd13
 
23630d2
 
 
 
 
 
 
 
 
 
 
 
 
 
3da36e1
 
 
 
 
 
 
 
f7cbd13
 
 
23630d2
 
 
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
56
57
58
59
60
import streamlit as st
import svgwrite
from PIL import Image
import urllib.request

DEFAULT_CARD_WIDTH = 75
DEFAULT_CARD_HEIGHT = 100

PARTS = {"background": ["white", "black", "red", "blue", "green", "yellow"],
         "suit": ["clubs", "diamonds", "hearts", "spades"],
         "value": ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]}

def draw_card(background, suit, value, 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="white", stroke="black", stroke_width=2))
    if background:
        img = svgwrite.image.Image(href=background, size=(f"{card_width}px", f"{card_height}px"))
        dwg.add(img)
    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)
    svg_string = dwg.tostring()
    return svg_string

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_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():
    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.save("background.png")
        return "background.png"
    else:
        return None

def display_card(selected_parts, card_width, card_height, background):
    card_svg = draw_card(background, selected_parts["suit"], selected_parts["value"], card_width, card_height)
    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")

st.set_page_config(page_title="Card Crafting Game", page_icon=":spades:")

selected_parts = display_parts_selection()
card_width, card_height = display_size_selection(DEFAULT_CARD_WIDTH, DEFAULT_CARD_HEIGHT)
background = display_image_upload()
display_card(selected_parts, card_width, card_height, background)