awacke1 commited on
Commit
23630d2
·
1 Parent(s): 7914c58

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -40
app.py CHANGED
@@ -1,60 +1,54 @@
1
  import streamlit as st
2
  import svgwrite
 
3
 
4
- # Define the size of the cards
5
- CARD_WIDTH = 75
6
- CARD_HEIGHT = 100
7
 
8
- # Define the size of the SVG canvas
9
- CANVAS_WIDTH = CARD_WIDTH * 5
10
- CANVAS_HEIGHT = CARD_HEIGHT
11
 
12
- # Define the parts that can be added to the card
13
- PARTS = {
14
- "background": ["white", "black", "red", "blue", "green", "yellow"],
15
- "suit": ["clubs", "diamonds", "hearts", "spades"],
16
- "value": ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"],
17
- }
18
-
19
- # Function to draw the card
20
- def draw_card(background_color, suit, value):
21
- # Create a new SVG drawing
22
- dwg = svgwrite.Drawing(size=(f"{CARD_WIDTH}px", f"{CARD_HEIGHT}px"))
23
-
24
- # Draw the card border
25
- dwg.add(dwg.rect((0, 0), (CARD_WIDTH, CARD_HEIGHT), rx=10, ry=10, fill=background_color, stroke="black", stroke_width=2))
26
-
27
- # Draw the card suit symbol
28
  suit = svgwrite.text.Text(suit.upper(), insert=(5, 15), fill="black", font_size="16px", font_weight="bold")
29
  dwg.add(suit)
30
-
31
- # Draw the card value
32
- value = svgwrite.text.Text(value, insert=(5, CARD_HEIGHT - 10), fill="black", font_size="16px", font_weight="bold")
33
  dwg.add(value)
34
-
35
- # Convert the SVG drawing to a string
36
  svg_string = dwg.tostring()
37
-
38
  return svg_string
39
 
40
- # Function to display the parts selection sidebar
41
  def display_parts_selection():
42
  selected_parts = {}
43
  for part, options in PARTS.items():
44
- selected_option = st.sidebar.selectbox(f"Select {part}", options)
45
- selected_parts[part] = selected_option
46
  return selected_parts
47
 
48
- # Function to display the resulting card
49
- def display_card(selected_parts):
50
- card_svg = draw_card(selected_parts["background"], selected_parts["suit"], selected_parts["value"])
51
- st.write(f'<svg viewBox="0 0 {CARD_WIDTH} {CARD_HEIGHT}">{card_svg}</svg>', unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
- # Set the page title and icon
54
  st.set_page_config(page_title="Card Crafting Game", page_icon=":spades:")
55
 
56
- # Display the parts selection sidebar
57
  selected_parts = display_parts_selection()
58
-
59
- # Display the resulting card
60
- display_card(selected_parts)
 
1
  import streamlit as st
2
  import svgwrite
3
+ from PIL import Image
4
 
5
+ DEFAULT_CARD_WIDTH = 75
6
+ DEFAULT_CARD_HEIGHT = 100
 
7
 
8
+ PARTS = {"background": ["white", "black", "red", "blue", "green", "yellow"],
9
+ "suit": ["clubs", "diamonds", "hearts", "spades"],
10
+ "value": ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]}
11
 
12
+ def draw_card(background, suit, value, card_width, card_height):
13
+ dwg = svgwrite.Drawing(size=(f"{card_width}px", f"{card_height}px"))
14
+ dwg.add(dwg.rect((0, 0), (card_width, card_height), rx=10, ry=10, fill="white", stroke="black", stroke_width=2))
15
+ if background:
16
+ img = svgwrite.image.Image(href=background, size=(f"{card_width}px", f"{card_height}px"))
17
+ dwg.add(img)
 
 
 
 
 
 
 
 
 
 
18
  suit = svgwrite.text.Text(suit.upper(), insert=(5, 15), fill="black", font_size="16px", font_weight="bold")
19
  dwg.add(suit)
20
+ value = svgwrite.text.Text(value, insert=(5, card_height - 10), fill="black", font_size="16px", font_weight="bold")
 
 
21
  dwg.add(value)
 
 
22
  svg_string = dwg.tostring()
 
23
  return svg_string
24
 
 
25
  def display_parts_selection():
26
  selected_parts = {}
27
  for part, options in PARTS.items():
28
+ selected_parts[part] = st.sidebar.selectbox(f"Select {part}", options)
 
29
  return selected_parts
30
 
31
+ def display_size_selection(default_width, default_height):
32
+ card_width = st.sidebar.slider("Card Width", min_value=50, max_value=150, value=default_width, step=5)
33
+ card_height = st.sidebar.slider("Card Height", min_value=75, max_value=200, value=default_height, step=5)
34
+ return card_width, card_height
35
+
36
+ def display_image_upload():
37
+ uploaded_file = st.sidebar.file_uploader("Upload Card Background Image", type=["png", "jpg", "jpeg"])
38
+ if uploaded_file is not None:
39
+ image = Image.open(uploaded_file)
40
+ image.save("background.png")
41
+ return "background.png"
42
+ else:
43
+ return None
44
+
45
+ def display_card(selected_parts, card_width, card_height, background):
46
+ card_svg = draw_card(background, selected_parts["suit"], selected_parts["value"], card_width, card_height)
47
+ st.write(f'<svg viewBox="0 0 {card_width} {card_height}">{card_svg}</svg>', unsafe_allow_html=True)
48
 
 
49
  st.set_page_config(page_title="Card Crafting Game", page_icon=":spades:")
50
 
 
51
  selected_parts = display_parts_selection()
52
+ card_width, card_height = display_size_selection(DEFAULT_CARD_WIDTH, DEFAULT_CARD_HEIGHT)
53
+ background = display_image_upload()
54
+ display_card(selected_parts, card_width, card_height, background)