awacke1 commited on
Commit
fea62a9
·
1 Parent(s): ff8be31

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -31
app.py CHANGED
@@ -1,32 +1,54 @@
 
 
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
- viewbox = image.viewbox()
11
- width, height = viewbox[2], viewbox[3]
12
- half_x, half_y = width/2, height/2
13
- line_color = "white"
14
- mask = image.defs.add(image.mask(id="bg_wrapper"))
15
- mask.add(image.circle(center=(half_x, half_y), r=335, fill=line_color, opacity=".4"))
16
- image.add(image.image(href=image_path, size=("100%", "100%"), mask="url(#bg_wrapper)"))
17
-
18
- return image.tostring()
19
-
20
- # Streamlit app
21
- st.title("Circular Image Mask")
22
-
23
- uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
24
-
25
- if uploaded_file is not None:
26
- # Save uploaded image to file
27
- with open("image.jpg", "wb") as f:
28
- f.write(uploaded_file.getbuffer())
29
-
30
- # Apply circular mask and display result
31
- output_svg = apply_circle_mask("image.jpg")
32
- st.image(output_svg, output_format="SVG")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import base64
2
+ import graphviz as gv
3
  import streamlit as st
4
+
5
+ # Define a function that renders the card using Graphviz
6
+ def render_card(image_path, title, subtitle):
7
+ # Define the Graphviz graph
8
+ g = gv.Graph(format='png')
9
+ g.attr(bgcolor='white', margin='0')
10
+
11
+ # Add the background image to the graph
12
+ with open(image_path, 'rb') as f:
13
+ data = f.read()
14
+ b64_image = base64.b64encode(data).decode()
15
+ g.node('background', image=f'data:image/png;base64,{b64_image}', shape='box', width='3.5', height='5')
16
+
17
+ # Add the title and subtitle to the graph
18
+ g.node('title', label=title, fontname='Arial', fontsize='16', shape='none', width='2')
19
+ g.node('subtitle', label=subtitle, fontname='Arial', fontsize='12', shape='none', width='2')
20
+
21
+ # Add edges between the nodes
22
+ g.edge('background', 'title', color='white', style='invis')
23
+ g.edge('background', 'subtitle', color='white', style='invis')
24
+
25
+ # Render the graph and return the PNG data
26
+ return g.pipe().decode('utf-8')
27
+
28
+ # Define the Streamlit app
29
+ def app():
30
+ st.title('Card Builder Evolver')
31
+
32
+ # Define the file uploader
33
+ uploaded_file = st.file_uploader('Upload an image', type=['jpg', 'jpeg', 'png'])
34
+
35
+ # Define the card title and subtitle inputs
36
+ title = st.text_input('Title')
37
+ subtitle = st.text_input('Subtitle')
38
+
39
+ # Render the card if an image has been uploaded and a title has been provided
40
+ if uploaded_file and title:
41
+ image_path = f'tmp/{uploaded_file.name}'
42
+ with open(image_path, 'wb') as f:
43
+ f.write(uploaded_file.getbuffer())
44
+ card = render_card(image_path, title, subtitle)
45
+ st.image(card, use_column_width=True)
46
+
47
+ # Define the download button
48
+ b64_card = base64.b64encode(card.encode()).decode()
49
+ href = f'<a href="data:image/png;base64,{b64_card}" download="card.png">Download card</a>'
50
+ st.markdown(href, unsafe_allow_html=True)
51
+
52
+ # Run the Streamlit app
53
+ if __name__ == '__main__':
54
+ app()