awacke1 commited on
Commit
3de7286
·
1 Parent(s): 1349f96

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -66
app.py CHANGED
@@ -1,36 +1,16 @@
1
- import os
2
- from io import BytesIO
3
-
4
  import streamlit as st
5
  import svgwrite
6
  from PIL import Image
 
7
 
8
- PARTS = {"background": ["white", "black", "red", "blue", "green", "yellow"],
9
- "suit": ["clubs", "diamonds", "hearts", "spades"],
10
- "value": ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]}
11
-
12
- LEVELS = ["common", "uncommon", "rare", "epic", "legendary", "mythic", "godlike"]
13
-
14
- def draw_card(background, suit, value, level, card_width, card_height):
15
  dwg = svgwrite.Drawing(size=(f"{card_width}px", f"{card_height}px"))
16
- dwg.add(dwg.rect((0, 0), (card_width, card_height), rx=10, ry=10, fill=background, stroke="black", stroke_width=2))
17
- dwg.add(svgwrite.text.Text(suit.upper(), insert=(5, 15), fill="black", font_size="16px", font_weight="bold"))
18
- dwg.add(svgwrite.text.Text(value, insert=(5, card_height - 10), fill="black", font_size="16px", font_weight="bold"))
19
- dwg.add(svgwrite.text.Text(level.upper(), insert=(card_width - 50, card_height - 10), fill="black", font_size="12px", font_weight="bold"))
20
  return dwg.tostring()
21
 
22
- def display_parts_selection():
23
- return {part: st.sidebar.selectbox(f"Select {part}", options) for part, options in PARTS.items()}
24
-
25
- def display_level_selection():
26
- return st.sidebar.selectbox("Select Level", LEVELS)
27
-
28
- def display_size_selection(default_width, default_height):
29
- card_width = st.sidebar.slider("Card Width", min_value=50, max_value=150, value=default_width, step=5)
30
- card_height = st.sidebar.slider("Card Height", min_value=75, max_value=200, value=default_height, step=5)
31
- return card_width, card_height
32
-
33
- def display_image_upload(card_width, card_height):
34
  uploaded_file = st.sidebar.file_uploader("Upload Card Background Image", type=["png", "jpg", "jpeg"])
35
  if uploaded_file is not None:
36
  image = Image.open(uploaded_file)
@@ -39,47 +19,12 @@ def display_image_upload(card_width, card_height):
39
  image.save(img_bytes, format="PNG")
40
  img_data = img_bytes.getvalue()
41
  img_href = "data:image/png;base64," + img_data.hex()
42
- return img_href
 
 
 
43
  else:
44
- return None
45
-
46
- def display_card(selected_parts, selected_level, card_width, card_height, background):
47
- if background and os.path.exists(background):
48
- background_name = os.path.basename(background)
49
- st.sidebar.markdown(f"**Background Image:** {background_name}")
50
- background_color = "white"
51
- else:
52
- st.sidebar.markdown(f"**Background Image:** {selected_parts['background'].capitalize()}")
53
- background_color = selected_parts["background"]
54
-
55
- card_svg = draw_card(background_color, selected_parts["suit"], selected_parts["value"], selected_level, card_width, card_height)
56
- if background:
57
- img = svgwrite.image.Image(href=background, size=(f"{card_width}px", f"{card_height}px"))
58
- dwg = svgwrite.fromstring(card_svg)
59
- dwg.add(img)
60
- st.write(f'<svg viewBox="0 0 {card_width} {card_height}">{dwg.tostring()}</svg>', unsafe_allow_html=True)
61
- else:
62
- st.write(f'<svg viewBox="0 0 {card_width} {card_height}">{card_svg.decode("utf-8")}</svg>', unsafe_allow_html=True)
63
-
64
- # Create a download link for the card SVG
65
- svg_content = '<?xml version="1.0" encoding="utf-8" ?>' + card_svg.decode("utf-8")
66
- st.download_button("Download Card as SVG", svg_content, "card.svg", "text/plain")
67
-
68
- # Create button to level up card
69
- if selected_level != "godlike":
70
- if st.button("Level Up"):
71
- current_level_index = LEVELS.index(selected_level)
72
- selected_level = LEVELS[current_level_index + 1]
73
- st.sidebar.warning(f"Card has been leveled up to {selected_level}")
74
-
75
- def main():
76
- st.set_page_config(page_title="Card Evolution Game", page_icon=":hearts:")
77
- selected_parts = display_parts_selection()
78
- selected_level = display_level_selection()
79
- card_width, card_height = display_size_selection(75, 100)
80
- background = display_image_upload(card_width, card_height)
81
- display_card(selected_parts, selected_level, card_width, card_height, background)
82
 
83
  if __name__ == '__main__':
84
  main()
85
-
 
 
 
 
1
  import streamlit as st
2
  import svgwrite
3
  from PIL import Image
4
+ from io import BytesIO
5
 
6
+ def draw_card(card_width, card_height, background):
 
 
 
 
 
 
7
  dwg = svgwrite.Drawing(size=(f"{card_width}px", f"{card_height}px"))
8
+ dwg.add(svgwrite.image.Image(href=background, size=(f"{card_width}px", f"{card_height}px")))
 
 
 
9
  return dwg.tostring()
10
 
11
+ def main():
12
+ st.set_page_config(page_title="Card Evolution Game", page_icon=":hearts:")
13
+ card_width, card_height = 100, 150
 
 
 
 
 
 
 
 
 
14
  uploaded_file = st.sidebar.file_uploader("Upload Card Background Image", type=["png", "jpg", "jpeg"])
15
  if uploaded_file is not None:
16
  image = Image.open(uploaded_file)
 
19
  image.save(img_bytes, format="PNG")
20
  img_data = img_bytes.getvalue()
21
  img_href = "data:image/png;base64," + img_data.hex()
22
+ st.write(f'<img src="{img_href}">', unsafe_allow_html=True)
23
+ card_svg = draw_card(card_width, card_height, img_href)
24
+ svg_content = '<?xml version="1.0" encoding="utf-8" ?>' + card_svg.decode("utf-8")
25
+ st.download_button("Download Card as SVG", svg_content, "card.svg", "text/plain")
26
  else:
27
+ st.warning("Please upload a card background image")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
  if __name__ == '__main__':
30
  main()