awacke1 commited on
Commit
c9a7bd9
·
1 Parent(s): 53aff91

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -33
app.py CHANGED
@@ -1,40 +1,31 @@
1
  import streamlit as st
2
  import svgwrite
3
- from PIL import Image
4
- from io import BytesIO
5
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
- def draw_card(card_width, card_height, background):
8
- dwg = svgwrite.Drawing(size=(f"{card_width}px", f"{card_height}px"))
9
- dwg.add(dwg.rect((0, 0), (card_width, card_height), rx=10, ry=10, fill=background, stroke="black", stroke_width=2))
10
 
11
- # Create a mask
12
- mask = dwg.defs.add(dwg.mask(id="bg_wrapper"))
13
- mask.add(dwg.circle(center=(card_width/2, card_height/2), r=card_width/2, fill="white"))
14
- mask.add(dwg.image(href=background, size=(card_width, card_height)))
15
 
16
- # Add an image using the mask
17
- dwg.add(dwg.image(href=background, size=(card_width, card_height), mask="url(#bg_wrapper)"))
18
 
19
- return dwg.tostring()
20
-
21
- def main():
22
- st.set_page_config(page_title="Card Evolution Game", page_icon=":hearts:")
23
- card_width, card_height = 100, 150
24
- uploaded_file = st.sidebar.file_uploader("Upload Card Background Image", type=["png", "jpg", "jpeg"])
25
- if uploaded_file is not None:
26
- image = Image.open(uploaded_file)
27
- image = image.resize((card_width, card_height))
28
- img_bytes = BytesIO()
29
- image.save(img_bytes, format="PNG")
30
- img_data = img_bytes.getvalue()
31
- img_href = "data:image/png;base64," + img_data.hex()
32
- st.write(f'<img src="{img_href}">', unsafe_allow_html=True)
33
- card_svg = draw_card(card_width, card_height, img_href)
34
- svg_content = '<?xml version="1.0" encoding="utf-8" ?>' + card_svg
35
- st.download_button("Download Card as SVG", svg_content, "card.svg", "text/plain")
36
- else:
37
- st.warning("Please upload a card background image")
38
-
39
- if __name__ == '__main__':
40
- main()
 
1
  import streamlit as st
2
  import svgwrite
 
 
3
 
4
+ def apply_circle_mask(image_path):
5
+ # Load image
6
+ image = svgwrite.Drawing('output.svg')
7
+ image.add(image.image(href=image_path, size=("100%", "100%")))
8
+
9
+ # Add circular mask
10
+ width, height = image.viewbox[2:]
11
+ half_x, half_y = width/2, height/2
12
+ line_color = "white"
13
+ mask = image.defs.add(image.mask(id="bg_wrapper"))
14
+ mask.add(image.circle(center=(half_x, half_y), r=335, fill=line_color, opacity=".4"))
15
+ image.add(image.image(href=image_path, size=("100%", "100%"), mask="url(#bg_wrapper)"))
16
 
17
+ return image.tostring()
 
 
18
 
19
+ # Streamlit app
20
+ st.title("Circular Image Mask")
 
 
21
 
22
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
 
23
 
24
+ if uploaded_file is not None:
25
+ # Save uploaded image to file
26
+ with open("image.jpg", "wb") as f:
27
+ f.write(uploaded_file.getbuffer())
28
+
29
+ # Apply circular mask and display result
30
+ output_svg = apply_circle_mask("image.jpg")
31
+ st.image(output_svg, output_format="SVG")